analyzer

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package analyzer implements the chanopt static analysis tool.

chanopt detects Go channel patterns replaceable with faster synchronization primitives (atomic, mutex, sync.Once).

Index

Constants

This section is empty.

Variables

View Source
var Analyzer = &analysis.Analyzer{
	Name:     "chanopt",
	Doc:      "detect channel patterns replaceable with mutex/atomic (8-127x faster)",
	Run:      run,
	Requires: []*analysis.Analyzer{inspect.Analyzer},
}

Analyzer is the exported analysis.Analyzer for chanopt.

Usage:

go vet -vettool=$(which chanopt) ./...
View Source
var Registry = map[Pattern]PatternSpec{
	IDGenerator: {
		"atomic.AddInt64",
		"~38x",
		"counter in infinite loop needs only an atomic increment",
	},
	RoundRobin: {
		"sync.Mutex + index",
		"~10x",
		"modular index cycling needs only a guarded counter",
	},
	RateLimiter: {
		"sync.Mutex + token bucket",
		"~8x",
		"ticker-refilled token slot needs only mutex-guarded math",
	},
	ConfigBroadcaster: {
		"atomic.Pointer / atomic.Value",
		"~80x",
		"latest-value store needs only an atomic pointer swap",
	},
	BoundedIterator: {
		"range-over-func (Go 1.23+) or Next() iterator",
		"~40x",
		"finite iteration needs no goroutine or channel",
	},
	CircuitBreaker: {
		"atomic.Int32",
		"~127x",
		"state enum in buffered chan(1) needs only an atomic int",
	},
	ChanSemaphore: {
		"x/sync/semaphore.Weighted",
		"~8x",
		"concurrency limiting chan struct{} is slower than semaphore",
	},
	Singleton: {
		"sync.Once + value field",
		"~19x",
		"one-time value served via channel needs only sync.Once",
	},
	FixedFanIn: {
		"sync.WaitGroup + append to slice",
		"~8x",
		"merging 2-3 fixed goroutines doesn't need a shared channel",
	},
	ChanTicker: {
		"time.NewTicker directly",
		"~15x",
		"wrapping time.Sleep in goroutine+channel duplicates time.Ticker",
	},
}

Registry is the single source of truth for all pattern metadata.

Functions

This section is empty.

Types

type Pattern

type Pattern int

Pattern represents a detected channel usage anti-pattern.

const (
	Unknown Pattern = iota
	IDGenerator
	RoundRobin
	RateLimiter
	ConfigBroadcaster
	BoundedIterator
	CircuitBreaker
	ChanSemaphore
	Singleton
	FixedFanIn
	ChanTicker
)

func (Pattern) String

func (p Pattern) String() string

type PatternSpec

type PatternSpec struct {
	Replacement string // e.g. "sync/atomic.AddInt64"
	Speedup     string // e.g. "~38x"
	Rationale   string // one-line explanation
}

PatternSpec holds the replacement metadata for a detected pattern.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL