Skip to content

[performance-profiler] Optimize add_locale offset formatting in Format hot path #50322

@github-actions

Description

@github-actions

Hot Path

addLocale.Format in libbeat/processors/add_locale/add_locale.go:101-125 is on the benchmarked path for BenchmarkConstruct and currently spends avoidable time/allocations in offset-string formatting.

Profiling Data

Before:

go test ./libbeat/processors/add_locale -run '^$' -bench 'BenchmarkConstruct$' -benchmem -count=5

BenchmarkConstruct-4   3204235   362.2 ns/op   104 B/op   4 allocs/op
BenchmarkConstruct-4   3529683   382.7 ns/op   104 B/op   4 allocs/op
BenchmarkConstruct-4   3214898   349.1 ns/op   104 B/op   4 allocs/op
BenchmarkConstruct-4   3503536   352.6 ns/op   104 B/op   4 allocs/op
BenchmarkConstruct-4   3225561   362.3 ns/op   104 B/op   4 allocs/op

Proposed Change

Replace fmt.Sprintf("%s%02d:%02d", sign, h, m) in addLocale.Format with fixed-width byte-buffer formatting:

- sign := "+"
+ sign := byte('+')
  if offset < 0 {
-   sign = "-"
+   sign = '-'
    offset *= -1
  }
  h := offset / hour
  m := (offset - (h * hour)) / min
- ft = fmt.Sprintf("%s%02d:%02d", sign, h, m)
+ var buf [6]byte
+ buf[0] = sign
+ buf[1] = byte('0' + h/10)
+ buf[2] = byte('0' + h%10)
+ buf[3] = ':'
+ buf[4] = byte('0' + m/10)
+ buf[5] = byte('0' + m%10)
+ ft = string(buf[:])

Results

After:

go test ./libbeat/processors/add_locale -run '^$' -bench 'BenchmarkConstruct$' -benchmem -count=5

BenchmarkConstruct-4   5605015   200.5 ns/op   88 B/op   3 allocs/op
BenchmarkConstruct-4   5342575   209.8 ns/op   88 B/op   3 allocs/op
BenchmarkConstruct-4   5872142   198.6 ns/op   88 B/op   3 allocs/op
BenchmarkConstruct-4   5794014   199.4 ns/op   88 B/op   3 allocs/op
BenchmarkConstruct-4   6136920   196.1 ns/op   88 B/op   3 allocs/op

Improvement:

  • Time: 361.78 ns/op → 200.88 ns/op (44.48% faster)
  • Memory: 104 B/op → 88 B/op (15.38% lower)
  • Allocations: 4 → 3 allocs/op (25% fewer allocations)

Verification

  • go test ./libbeat/processors/add_locale -run '^$' -bench 'BenchmarkConstruct$' -benchmem -count=5 (before/after with same command)
  • go test ./libbeat/processors/add_locale (passes)
  • Behavior preserved: change only affects string construction mechanics for the same ±HH:MM output format.

Evidence

  • Hot-path code: libbeat/processors/add_locale/add_locale.go:101-125
  • Duplicate checks:
    • /tmp/previous-findings.json: no existing open finding for add_locale/BenchmarkConstruct
    • Open issue searches returned no matches:
      • repo:elastic/beats is:issue is:open performance-profiler add_locale BenchmarkConstruct
      • repo:elastic/beats is:issue is:open add_locale timezone offset performance

Note

🔒 Integrity filter blocked 9 items

The following items were blocked because they don't meet the GitHub integrity level.

  • #33512 search_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".
  • #30073 search_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".
  • #49814 search_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".
  • #35229 search_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".
  • #43660 search_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".
  • #47666 search_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".
  • #30677 search_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".
  • #14917 search_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".
  • #44780 search_issues: has lower integrity than agent requires. The agent cannot read data with integrity below "approved".

To allow these resources, lower min-integrity in your GitHub frontmatter:

tools:
  github:
    min-integrity: approved  # merged | approved | unapproved | none

What is this? | From workflow: Performance Profiler

Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not.

  • expires on May 1, 2026, 2:49 PM UTC

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions