perf: low-latency optimizations for hot paths and memory allocation#21
Merged
Merged
Conversation
Apply HFT-inspired low-latency best practices to reduce observer effect
and improve melisai's own performance during system profiling.
Hot path optimizations:
- Pre-compile regex patterns at package level (parsers.go)
- Pre-allocate slices with capacity hints in all parsers
- Pre-lowercase headers once instead of per-event in ParseTabularEvents
- Replace fmt.Sprintf("%v") with type-switch formatKey() in aggregation
- Make DefaultThresholds a package-level singleton (avoid 37 closure allocs)
Memory/IO optimizations:
- Add sync.Pool for bytes.Buffer reuse across 67+ BCC tool executions
- Switch diff.LoadReport to streaming json.NewDecoder (halve peak memory)
- Single-pass category scan in AI prompt generation (3 loops → 1)
- Manual binary.LittleEndian parsing in eBPF tcpretrans (avoid reflection)
Observability:
- Add --pprof flag for CPU profiling of melisai itself
- Add 11 benchmark tests for parsers, aggregation, and anomaly detection
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- NATIVE_EBPF_MIGRATION.md — full plan with phases, patterns, validation - PROMPT_NATIVE_EBPF.md — reusable prompt template for AI-assisted porting Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fmt.Sprintf("%v")with type-switchformatKey()in aggregation (20ns vs ~500ns per key, zero allocs for common types)defaultThresholds— avoid 37 closure + struct allocations on everyDetectAnomalies()call (0.3ns access vs ~100ns)sync.Poolforbytes.Bufferreuse across 67+ BCC tool executionsjson.NewDecoderindiff.LoadReport()— halves peak memory for large reportsbinary.LittleEndianparsing in eBPF tcpretrans collector (avoid reflection-heavybinary.Read)--pprofflag for CPU profiling melisai itself (go tool pprof melisai_cpu.prof)Benchmark Results (Apple M2 Pro)
Test plan
go test ./...go test -bench=. -benchmemmelisai collect --pprof melisai_cpu.profto verify real-world profile🤖 Generated with Claude Code