Skip to content

Compute the 'now' CEL variable lazily - #187

Merged
sudorandom merged 2 commits into
mainfrom
kmcd/lazy-cel-now
Sep 4, 2026
Merged

Compute the 'now' CEL variable lazily#187
sudorandom merged 2 commits into
mainfrom
kmcd/lazy-cel-now

Conversation

@sudorandom

Copy link
Copy Markdown
Contributor

time.Now() was being called for each call to validate(). With this change it will lazily call time.Now() and cache it for the duration of that validate() call, so multiple rules that need to know the time will only trigger time.Now() a single time.

packages/protovalidate-bench runs 2–8% faster on the lighter tasks (Scalar −6.8%, Map −7.5%).

@jonbodner-buf jonbodner-buf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good. My only question is if maybe a test should be added that shows the same time being read twice or a test showing the time not being set at all if there's no time check? I'm pretty sure they will pass, so maybe not necessary for validation, but might be good for documentation.

@sudorandom
sudorandom force-pushed the kmcd/lazy-cel-now branch 3 times, most recently from 809662d to 4c5389e Compare September 3, 2026 19:20
Comment thread packages/protovalidate/src/cel.ts Outdated
@sudorandom sudorandom changed the title Compute the CEL variable now lazily Compute the 'now' CEL variable lazily Sep 4, 2026
@sudorandom
sudorandom merged commit 46a40ab into main Sep 4, 2026
15 checks passed
@sudorandom
sudorandom deleted the kmcd/lazy-cel-now branch September 4, 2026 10:45
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.

3 participants