elastic / elastic/beats

[performance-profiler] Optimize prefixNumDate digit matching hot path

Open
#52,144 1 comment 0 reactions 0 assignees View on GitHub
needs_team
Dominant language
Go
Stars
12.7k
Forks
5k
Avg merge
2d 15m
Merged PRs (30d)
385

Description

## Hot Path
`libbeat/common/match/matchers.go:164-209`, specifically `(*prefixNumDate).Match`, which validates each date digit with repeated range comparisons. The CPU profile identified this function at **65.02s flat / 79.14s cumulative (8.98%)**.

## Profiling Data
The same benchmark command was run before and after a temporary, behavior-preserving digit-loop optimization:

```text
go test -run '^$' -bench='BenchmarkPatterns/Name=startsWithDate2,_Matcher=Match' -benchmem -count=5

Before median: 203.3 ns/op
After median: 178.8 ns/op
Both: 0 B/op, 0 allocs/op
```

## Proposed Change
Optimize the digit-validation loop in `(*prefixNumDate).Match` while preserving the current prefix, separator, suffix, and ASCII digit semantics. The temporary implementation used for measurement reduced the per-digit loop overhead without changing the matcher API or behavior.

## Results
**Improvement:** 12.0% lower median latency on the targeted `startsWithDate2` matcher benchmark, with allocations unchanged at zero.

## Verification
- `go test ./...` passed after the temporary optimization.
- The existing matcher benchmark and behavior tests passed.
- The temporary change was removed after measurement; the repository worktree is unchanged.
- Duplicate search found no existing issue or open PR for `prefixNumDate` or this date-matcher optimization.

## Evidence
- Hot path: `libbeat/common/match/matchers.go:164-209`, `(*prefixNumDate).Match`.
- Benchmark harness: `libbeat/common/match/matcher_bench_test.go:98-141`; the `startsWithDate2` pattern is defined at line 108.
- CPU profile command: `go test -bench=. -benchmem -cpuprofile=cpu.prof -memprofile=mem.prof -count=5`.
- Targeted before/after command: `go test -run '^$' -bench='BenchmarkPatterns/Name=startsWithDate2,_Matcher=Match' -benchmem -count=5`.

- [ ] Replace the repeated digit checks in `(*prefixNumDate).Match` with the measured optimized loop.
- [ ] Retain and run matcher behavior tests and the targeted benchmark to confirm the improvement on CI hardware.

---
[What is this?](https://ela.st/github-ai-tools) | [From workflow: Performance Profiler](https://github.com/elastic/beats/actions/runs/29840068323)

Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not.
> - [x] expires on Jul 28, 2026, 3:03 PM UTC

Contributor guide

Open the contributing guide

Research direction

Start in libbeat/common/match/matchers.go:164-209 at (*prefixNumDate).Match, then read the startsWithDate2 benchmark in libbeat/common/match/matcher_bench_test.go:98-141. Replace the repeated digit checks while preserving prefix, separator, suffix, and ASCII digit behavior. Run the matcher behavior tests, the targeted benchmark, and go test ./...; done means the tests pass and the benchmark improvement is confirmed on CI hardware.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
performance
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.