#53440·beats

[performance-profiler] Lazily allocate filestream scan error maps in shared-dir hot path

Author: github-actions[bot]Created Sep 28, 2026Updated Sep 28, 2026
Labelsneeds_team

Hot Path

filebeat/input/filestream/fscanner.go:318-380 in scan-state construction and result assembly (newScanState, recordUnobservable, recordVanished, results).

The current path eagerly allocates and sorts error-tracking collections on every scan, even when there are no path errors:

  • eager map init in newScanState (unobservable, vanished)
  • unconditional sorted key extraction in results()

Profiling Data

Before:

go test -run '^$' -bench '^BenchmarkGetFilesSharedDir/inputs400$' -benchmem -count=5 ./filebeat/input/filestream

BenchmarkGetFilesSharedDir/inputs400-4                56  20648141 ns/op  1028769 B/op  9618 allocs/op
BenchmarkGetFilesSharedDir/inputs400-4                56  20498608 ns/op  1028624 B/op  9618 allocs/op
BenchmarkGetFilesSharedDir/inputs400-4                56  20509263 ns/op  1028620 B/op  9618 allocs/op
BenchmarkGetFilesSharedDir/inputs400-4                50  20637638 ns/op  1028961 B/op  9620 allocs/op
BenchmarkGetFilesSharedDir/inputs400-4                58  20820442 ns/op  1028573 B/op  9617 allocs/op

Proposed Change

Use lazy allocation/materialization in the no-error fast path:

  • remove eager unobservable / vanished map allocation in newScanState
  • allocate these maps only on first insert in recordUnobservable and recordVanished
  • in results(), only call slices.Sorted(maps.Keys(...)) when map length is non-zero

Patch location: filebeat/input/filestream/fscanner.go:318-380

Results

After:

go test -run '^$' -bench '^BenchmarkGetFilesSharedDir/inputs400$' -benchmem -count=5 ./filebeat/input/filestream

BenchmarkGetFilesSharedDir/inputs400-4                54  20197647 ns/op   939237 B/op  6419 allocs/op
BenchmarkGetFilesSharedDir/inputs400-4                49  20552935 ns/op   939233 B/op  6421 allocs/op
BenchmarkGetFilesSharedDir/inputs400-4                58  20497305 ns/op   938975 B/op  6417 allocs/op
BenchmarkGetFilesSharedDir/inputs400-4                58  20294427 ns/op   939116 B/op  6417 allocs/op
BenchmarkGetFilesSharedDir/inputs400-4                55  20356245 ns/op   939199 B/op  6418 allocs/op

Improvement (mean across 5 runs):

  • allocs/op: 9618.2 -> 6418.4 (33.27% fewer allocations)
  • B/op: 1028709.4 -> 939152.0 (8.71% less memory)
  • ns/op: 20622818.4 -> 20379711.8 (1.18% faster)

This is mainly an allocation/GC optimization on a high-frequency scan path.

Verification

  • Ran package tests: go test ./filebeat/input/filestream → ok
  • Behavior preserved: nil maps/slices are treated equivalently to empty ones for this result contract; only allocation/sorting timing changed.

Evidence

Commands run:

  • go test -run '^$' -bench '^BenchmarkGetFilesSharedDir/inputs400$' -benchmem -count=5 ./filebeat/input/filestream (before and after, identical command)
  • go test ./filebeat/input/filestream

Code references:

  • filebeat/input/filestream/fscanner.go:318-380

What is this? | From workflow: Performance Profiler

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

  • expires on Oct 5, 2026, 2:18 PM UTC