Skip to content

Cache Normalization results in Expression Normalizer - #9

Merged
ezkimo merged 1 commit into
mainfrom
issue-3-cache-normalized-output-in-expression-normalizer
Oct 16, 2026
Merged

ezkimo merged 1 commit into
mainfrom
issue-3-cache-normalized-output-in-expression-normalizer

Conversation

@ezkimo

@ezkimo ezkimo commented Oct 16, 2026

Copy link
Copy Markdown
Owner

Summary

ExpressionNormalizer::normalize() runs several preg_replace passes over the expression on every call, before ExpressionCompiler even consults its own cahces. Since normalization is deterministic, repeated invocations wthe the same raw expression redo identical regex wirk every time.

This adds in-memory cache keyed by the raw expression, so each distinct expression is normalized exactly once per process. As a result ExpressionCompiler::compile() and compileNodeSet() skip redundant normalization for recurring expressions.

Changes

  • src/Schematron/Compiler/ExpressionNormalizer.php
    • Added protected array $normalizationCache (raw expression ⇒ normalized expression)
    • normalize() return cached results immediately; otherwise it runs the existing normalization passes and stores the result
    • Added clearCache(): void to reset the cache
  • src/Schematron/compiler/ExpressionCompiler.php
    • clearCache() now also clears the normalizer cache, keeping the two caches consistent
    • Translated the german docblocks / comments to english
  • tests/Compiler/ExpressionNormalizerCacheTest.php (new)
    • Uses a counting subclass of ExpressionNormalizer to observe how often the normalization passes run:
      • A repeated raw expression triggers noamlization exactly once (subsequent calls are served from the cache)
      • Different raw expressions each get their own cache entry
      • Equivalent expessions (a = b va a=b) produce identical normalized output (acceptance criterion)
      • After clearCache(), the expression is normalized again

Notes

  • The normalizer cache is keyed be the raw expression (the variant that delivers the performance win). Expressions that are merely differently formatted but normalize identically are trated as separate cache entries - this matches the proposal in the issue.
  • All pre-existing tests remain passing unchanged (identical normalized output for existing inputs)

@ezkimo ezkimo added enhancement New feature or request performance labels Oct 16, 2026
@ezkimo
ezkimo merged commit 1699716 into main Oct 16, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant