feat: add optional caching of params and constants#25
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR introduces optional caching of evaluation stages for parameters and constants to reduce memory usage when multiple govaluate expressions share similar values. The caching is enabled through a build tag and requires Go 1.24+ to leverage weak pointers.
- Refactored stage planning to use cached stages for parameters and constants
- Implemented build-tag conditional caching using weak references to avoid memory leaks
- Added defensive copying to prevent mutation of shared cached stages
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| stagePlanner.go | Modified to use new caching functions and added defensive copying for shared stages |
| noCache.go | Fallback implementation that maintains original behavior without caching |
| cache.go | Cache implementation using weak pointers and sync.Map for Go 1.24+ |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| @@ -0,0 +1,75 @@ | |||
| //go:build go1.24 && cache | |||
There was a problem hiding this comment.
The build constraint 'go1.24' may not work as expected. Go build tags typically use version formats like 'go1.24.0' or semantic versioning. Consider using a more standard build tag format.
| //go:build go1.24 && cache | |
| //go:build go1.24.0 && cache |
| @@ -0,0 +1,18 @@ | |||
| //go:build !go1.24 || !cache | |||
There was a problem hiding this comment.
The build constraint '!go1.24' should match the format used in cache.go. If cache.go uses 'go1.24', this constraint should be '!go1.24', but consider using a more standard build tag format consistently.
| //go:build !go1.24 || !cache | |
| //go:build !(go1.24 && cache) |
|
🎉 This PR is included in version 1.10.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
If you have a bunch of govaluate expressions kept in memory with similar parameters and/or similar constants this allows you to turn on a cache with a build tag (assuming you have golang 1.24 or greater). This reduces the amount of memory the expressions consume by sharing stages across expressions.
Benchmarks without cache:
Benchmarks with cache: