Files
go-telegram/.benchstats/after_step3.txt
T
lukaszraczylo 0ee539e991 perf(dispatch): typed Context.Command/CommandArgs/RegexMatch fields
Move the three conventional Values keys ("command", "command_args", "regex_match") to typed fields on Context. Router and group routing write the fields directly; the Values map is allocated lazily via the new Set method and reserved for user-defined custom keys.

Allocation impact (M4 Max, b.Loop()):

  DispatchCommand:   5 allocs/op -> 1, 153ns -> 69ns (-55%)

  DispatchTextRegex: 5 allocs/op -> 2, 181ns -> 107ns (-41%)

  DispatchFilter:    2 allocs/op -> 1, 32ns -> 19ns (-41%)

  NewContext:        5.79ns -> 1.60ns

Trade-off: Context struct grew from ~48B to ~96B (three new fields), so filter-only paths pay ~50B more per dispatch. Command/regex paths save ~320B + 4 allocs each, which dominates for typical bot workloads.

Handlers reading c.Values["command"], c.Values["command_args"], or c.Values["regex_match"] now get nil; the typed fields c.Command, c.CommandArgs, c.RegexMatch are the new accessors. Custom keys still work via c.Set(k, v) and c.Values[k].
2026-05-10 02:35:24 +01:00

28 lines
1.6 KiB
Plaintext

After: static-headers + bool-fast-path + lazy-Values + typed-fields
goos: darwin
goarch: arm64
cpu: Apple M4 Max
pkg: github.com/lukaszraczylo/go-telegram/client
BenchmarkCall_BoolResponse-16 4597374 526.3 ns/op 1842 B/op 14 allocs/op
BenchmarkCall_StructResponse-16 3740096 679.6 ns/op 1973 B/op 16 allocs/op
BenchmarkEncodeJSONBody-16 41997352 58.35 ns/op 96 B/op 2 allocs/op
BenchmarkDecodeResult_Bool-16 863273641 2.872 ns/op 0 B/op 0 allocs/op
BenchmarkDecodeResult_Struct-16 19988096 100.3 ns/op 144 B/op 2 allocs/op
pkg: github.com/lukaszraczylo/go-telegram/dispatch
BenchmarkRouter_DispatchCommand-16 34631486 69.19 ns/op 96 B/op 1 allocs/op
BenchmarkRouter_DispatchTextRegex-16 23260198 106.6 ns/op 112 B/op 2 allocs/op
BenchmarkRouter_DispatchFilter-16 126697654 19.03 ns/op 96 B/op 1 allocs/op
BenchmarkRouter_NewContext-16 1000000000 1.600 ns/op 0 B/op 0 allocs/op
BenchmarkExtractCommand-16 27345622 87.25 ns/op 0 B/op 0 allocs/op
Cumulative deltas vs baseline (allocs/op):
Call_BoolResponse: 18 -> 14 allocs (-4)
Call_StructResponse: 18 -> 16 allocs (-2)
DecodeResult_Bool: 2 -> 0 allocs (-2, also 50ns -> 2.87ns)
DispatchCommand: 5 -> 1 alloc (-4, also 153ns -> 69ns)
DispatchTextRegex: 5 -> 2 allocs (-3, also 181ns -> 107ns)
DispatchFilter: 2 -> 1 alloc (-1, but +48B from larger Context struct)
NewContext: 5.79ns -> 1.60ns (-72%)