Commit 232d6a5
committed
Performance improvements to CEL rule evaluation
Three improvements to hot-path validation, inspired by protovalidate-go.
Guard `_validate_cel` when `self._cel` is empty (bufbuild/protovalidate-go#261)
Adds an early return in `CelRules._validate_cel` when there are no CEL
runners, skipping activation dict creation and `datetime.now()` entirely.
Also guards the `_msg_to_cel` call in `MessageRules.validate()` so the
message-to-CEL conversion is skipped when there are no CEL rules to run.
Benchmark (required-only field, 0 CEL runners): -37%.
Skip `now` computation when unused (bufbuild/protovalidate-go#289)
Adds `_uses_now` to `CelRules`, set at compile time in `add_rule` by
checking whether `"now"` appears in the expression string. Only
`timestamp.gt_now`, `timestamp.lt_now`, `timestamp.within`, and custom
expressions referencing `now` will call `datetime.datetime.now()`.
Benchmarks: int32.gt (5 CEL runners) -25%, timestamp.gt_now -6%.
Early-exit loop in `cel_unique` (bufbuild/protovalidate-go#289)
Replaces `len(val) == len(set(val))` with a loop that returns `False` on
the first duplicate, avoiding building the full set unnecessarily.1 parent a4d0988 commit 232d6a5
2 files changed
Lines changed: 17 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
328 | 328 | | |
329 | 329 | | |
330 | 330 | | |
331 | | - | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
332 | 337 | | |
333 | 338 | | |
334 | 339 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
342 | 342 | | |
343 | 343 | | |
344 | 344 | | |
| 345 | + | |
345 | 346 | | |
346 | 347 | | |
347 | 348 | | |
| |||
357 | 358 | | |
358 | 359 | | |
359 | 360 | | |
| 361 | + | |
| 362 | + | |
360 | 363 | | |
361 | 364 | | |
362 | 365 | | |
363 | 366 | | |
364 | | - | |
| 367 | + | |
| 368 | + | |
365 | 369 | | |
366 | 370 | | |
367 | 371 | | |
| |||
409 | 413 | | |
410 | 414 | | |
411 | 415 | | |
| 416 | + | |
| 417 | + | |
412 | 418 | | |
413 | 419 | | |
414 | 420 | | |
| |||
463 | 469 | | |
464 | 470 | | |
465 | 471 | | |
466 | | - | |
467 | | - | |
468 | | - | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
469 | 476 | | |
470 | 477 | | |
471 | 478 | | |
| |||
0 commit comments