Skip to content

Conversation

@andreyyudin
Copy link

@andreyyudin andreyyudin commented Feb 10, 2026

Summary

  • Added config.WithLazyContextTracking() for on-demand context-variable tracking.
  • Preserved backward compatibility by keeping eager tracking as the default.
  • Optimized descendant traversal using iterative descendApply to reduce allocations.
  • Added reproducible benchmarks for descendant traversal and lazy tracking.

Benchmark Prerequisites

Install benchstat once:

go install golang.org/x/perf/cmd/benchstat@latest

If benchstat is not on your PATH, invoke it as:

$(go env GOPATH)/bin/benchstat ...

How To Benchmark (Reproducible)

1) Descendant traversal (descendApply) before/after (n=15)

# feature branch
git checkout feature/lazy-context-tracking
go test ./pkg/jsonpath -bench BenchmarkDescendantQuery -benchmem -run '^$' -benchtime=5s -count=15 > /tmp/feat_desc.txt

# main baseline (reuse benchmark file from feature branch)
git checkout main
git checkout feature/lazy-context-tracking -- pkg/jsonpath/yaml_query_test.go
go test ./pkg/jsonpath -bench BenchmarkDescendantQuery -benchmem -run '^$' -benchtime=5s -count=15 > /tmp/main_desc.txt
git restore --source=HEAD --staged --worktree pkg/jsonpath/yaml_query_test.go

# compare
$(go env GOPATH)/bin/benchstat /tmp/main_desc.txt /tmp/feat_desc.txt

2) Lazy tracking on/off (n=15)

git checkout feature/lazy-context-tracking

LAZY_TRACKING_MODE=eager go test ./pkg/jsonpath \
  -bench BenchmarkLazyContextTrackingStress -benchmem -run '^$' \
  -benchtime=1x -count=15 -tags 'lazytracking stress' > /tmp/lazy_eager.txt

LAZY_TRACKING_MODE=lazy go test ./pkg/jsonpath \
  -bench BenchmarkLazyContextTrackingStress -benchmem -run '^$' \
  -benchtime=1x -count=15 -tags 'lazytracking stress' > /tmp/lazy_lazy.txt

$(go env GOPATH)/bin/benchstat /tmp/lazy_eager.txt /tmp/lazy_lazy.txt

Performance

Descendant traversal (BenchmarkDescendantQuery, n=15)

  • time: -20.69% (8.723ms -> 6.918ms, p=0.000)
  • bytes: -38.55% (6.128MiB -> 3.766MiB, p=0.000)
  • allocs: -49.90% (50.38k -> 25.24k, p=0.000)

Lazy tracking stress (BenchmarkLazyContextTrackingStress, n=15)

  • time: -75.18% (433.2ms -> 107.5ms, p=0.000)
  • bytes: -74.39% (158.62MiB -> 40.63MiB, p=0.000)
  • allocs: -20.30% (1.658M -> 1.321M, p=0.000)

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.

1 participant