Skip to content

perf: build matcher functions only when compiling expressions - #1775

Closed
kamalyes wants to merge 1 commit into
apache:masterfrom
kamalyes:perf/lazy-matcher-functions
Closed

kamalyes wants to merge 1 commit into
apache:masterfrom
kamalyes:perf/lazy-matcher-functions

Conversation

@kamalyes

Copy link
Copy Markdown

Every enforcement currently copies the function map and allocates role-function closures before checking the compiled matcher cache. When an ordinary matcher is already compiled, those newly built functions are discarded.

This change constructs the function map and role closures only in the expression-compilation branch. eval() expressions continue to bind the current request on each enforcement, so cached expressions never capture a previous request's parameters. Existing matcher invalidation and role-function cache behavior are preserved. No public API, dependency, or minimum Go version changes.

Validation

  • go vet ./...
  • go test -race ./... with this patch applied independently
  • Added regressions for alternating subjects in eval() requests and a custom matcher function registered after warmup, including repeated use of the compiled custom matcher.

Engine-only benchmarks on Apple M4, macOS arm64, Go 1.26.2, using identical one-rule workloads on base c92cc6a and the candidate. Decision caching is not enabled; the compiled matcher is warmed before timing. Five samples, -benchtime=500ms -count=5 -cpu=10; figures are medians:

Workload Before ns/op After ns/op Before B/op After B/op Before allocs/op After allocs/op
ACL enforcement 1571 1107 1478 758 17 12
RBAC enforcement 1744 1263 1648 830 22 15

These show approximately 28–30% lower enforcement latency for the measured small-policy workloads. They do not predict end-to-end application latency or gains for large policy sets where scanning dominates.

Reproduce with the added benchmarks on both revisions:

go test -run='^$' -bench='BenchmarkReviewEnforce' -benchmem -benchtime=500ms -count=5 -cpu=10 .

Avoid copying the function map and generating role closures on compiled matcher cache hits. Keep eval expressions bound to the current request.

Add request-binding regressions and ACL/RBAC benchmarks. Verified with go vet ./... and go test -race ./... on this branch.
@hsluoyz

hsluoyz commented Nov 15, 2026

Copy link
Copy Markdown
Member

Thanks for spotting this. The optimization has been applied in 3bf20dc with a slightly different shape: the function map construction is moved into a shared getMatcherFunctions() helper (also reused by GetFilteredNamedPolicyWithMatcher), and getAndStoreMatcherExpression() returns early on a cache hit, which also fixes the nestif lint failure. The existing benchmarks in model_b_test.go already cover this path, so no new test files are needed. Closing this PR in favor of that commit.

@hsluoyz hsluoyz closed this Nov 15, 2026
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.

2 participants