Skip to content

Optimize lexer, parser, and interpreter for speed#362

Open
marksmayo wants to merge 2 commits into
jmespath:developfrom
marksmayo:develop
Open

Optimize lexer, parser, and interpreter for speed#362
marksmayo wants to merge 2 commits into
jmespath:developfrom
marksmayo:develop

Conversation

@marksmayo

Copy link
Copy Markdown

Summary

Reduces per-expression overhead across the compile and evaluation pipeline with no change in behavior. Benchmarks in tests/compliance/benchmarks.json improve 8–97% across lex/parse/search with zero regressions.

Changes

  • lexer: scan with a local index and slice identifier/number/string runs instead of char-by-char accumulation; consume whitespace runs; find-based scanning for quoted strings and literals.
  • parser: precompute per-class nud/led dispatch tables, inline token access and AST node construction on the hot path, and look up the compile cache without raising on a miss.
  • interpreter: share a pre-warmed default TreeInterpreter for default-options searches, cache resolved visit_* handlers, use a fixed two-argument visit signature, and short-circuit field access on None.
  • functions: resolve signature types to Python types once at registration and reuse the cached result during validation.

Compatibility

Public API (search, compile, Options) and subclass extension points are preserved: overriding TreeInterpreter.visit/_is_false, Functions._validate_arguments, base Visitor instance-level/staticmethod handlers, and Parser._error_nud_token all continue to work. The shared default interpreter's method cache is pre-warmed so concurrent default searches only read it (safe on free-threaded CPython builds).

Testing

  • Full test suite: 991 passed, 1 skipped
  • Hypothesis property tests: 7 passed
  • A/B benchmarks vs. base: 0 regressions, all metrics faster

marksmayo added 2 commits July 6, 2026 21:50
Reduce per-expression overhead across the compile and evaluation
pipeline without changing behavior:

- lexer: scan with a local index and slice identifier/number/string
  runs instead of char-by-char accumulation; consume whitespace runs;
  find-based scanning for quoted strings and literals.
- parser: precompute per-class nud/led dispatch tables, inline token
  access and AST node construction on the hot path, and look up the
  compile cache without raising on a miss.
- interpreter: share a pre-warmed default TreeInterpreter for
  default-options searches, cache resolved visit_* handlers, use a
  fixed two-argument visit signature, and short-circuit field access
  on None.
- functions: resolve signature types to Python types once at
  registration and reuse the cached result during validation.

Benchmarks (tests/compliance/benchmarks.json) improve 8-97% across
lex/parse/search with no regressions; full test suite and hypothesis
property tests pass.
Optimize lexer, parser, and interpreter for speed
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.

1 participant