Skip to content

perf(core-go): zero-allocation memo normalization and routing extraction#276

Merged
codeZe-us merged 1 commit into
Boxkit-Labs:mainfrom
TheWeirdDee:issue_265
Jun 28, 2026
Merged

perf(core-go): zero-allocation memo normalization and routing extraction#276
codeZe-us merged 1 commit into
Boxkit-Labs:mainfrom
TheWeirdDee:issue_265

Conversation

@TheWeirdDee

Copy link
Copy Markdown
Contributor

Resolves #265

Replace regexp.MustCompile and big.Int with allocation-free alternatives on the hot path in the Go routing package:

  • memo.go: remove digitsOnly regex and uint64Max big.Int package-level vars; add isAllDigits (byte-scan loop) and fitsUint64 (string length + lexicographic compare against uint64MaxStr constant). NormalizeMemoTextID now returns 0 allocs/op on canonical inputs (proved by benchmarks).

  • extract.go: remove duplicate digitsOnlyRegex var; rewrite normalizeUnsupportedMemoType with strings.Builder to avoid three intermediate string allocations from chained ReplaceAll/ToLower; use pre-computed string literals for known memo-type warning messages; use strings.Builder for the dynamic unrecognized-type message; pre-allocate warning slices with make([]Warning, 0, cap) instead of appending into a zero-capacity slice.

  • bench_test.go: add 11 benchmarks (BenchmarkNormalizeMemoTextID_, BenchmarkIsAllDigits, BenchmarkFitsUint64, BenchmarkExtractRouting_, parallel variants) with b.ReportAllocs(). Results on i7-10510U:
    NormalizeMemoTextID_Canonical 12.7 ns/op 0 allocs/op
    NormalizeMemoTextID_MaxUint64 18.3 ns/op 0 allocs/op
    IsAllDigits 11.3 ns/op 0 allocs/op
    FitsUint64 0.27 ns/op 0 allocs/op
    ExtractRouting_GAddr_MemoID 757 ns/op 6 allocs/op (parse-dominated)
    ExtractRouting_GAddr_Parallel 322 ns/op 6 allocs/op

Resolves Boxkit-Labs#265

Replace regexp.MustCompile and big.Int with allocation-free alternatives
on the hot path in the Go routing package:

- memo.go: remove digitsOnly regex and uint64Max big.Int package-level
  vars; add isAllDigits (byte-scan loop) and fitsUint64 (string length +
  lexicographic compare against uint64MaxStr constant). NormalizeMemoTextID
  now returns 0 allocs/op on canonical inputs (proved by benchmarks).

- extract.go: remove duplicate digitsOnlyRegex var; rewrite
  normalizeUnsupportedMemoType with strings.Builder to avoid three
  intermediate string allocations from chained ReplaceAll/ToLower; use
  pre-computed string literals for known memo-type warning messages; use
  strings.Builder for the dynamic unrecognized-type message; pre-allocate
  warning slices with make([]Warning, 0, cap) instead of appending into a
  zero-capacity slice.

- bench_test.go: add 11 benchmarks (BenchmarkNormalizeMemoTextID_*,
  BenchmarkIsAllDigits, BenchmarkFitsUint64, BenchmarkExtractRouting_*,
  parallel variants) with b.ReportAllocs(). Results on i7-10510U:
    NormalizeMemoTextID_Canonical   12.7 ns/op   0 allocs/op
    NormalizeMemoTextID_MaxUint64   18.3 ns/op   0 allocs/op
    IsAllDigits                     11.3 ns/op   0 allocs/op
    FitsUint64                       0.27 ns/op  0 allocs/op
    ExtractRouting_GAddr_MemoID    757   ns/op   6 allocs/op  (parse-dominated)
    ExtractRouting_GAddr_Parallel  322   ns/op   6 allocs/op
@drips-wave

drips-wave Bot commented Jun 28, 2026

Copy link
Copy Markdown

@TheWeirdDee Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@codeZe-us
codeZe-us self-requested a review June 28, 2026 13:28
@codeZe-us
codeZe-us merged commit 7152aa2 into Boxkit-Labs:main Jun 28, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Zero-Allocation Memory Optimizations in core-go

2 participants