Skip to content

perf(optimizer): skip redundant hash invalidation in simplify's pointer-reset loop#7725

Open
tobymao wants to merge 1 commit into
mainfrom
perf/simplify-speedups
Open

perf(optimizer): skip redundant hash invalidation in simplify's pointer-reset loop#7725
tobymao wants to merge 1 commit into
mainfrom
perf/simplify-speedups

Conversation

@tobymao

@tobymao tobymao commented Jun 10, 2026

Copy link
Copy Markdown
Owner

_simplify's post-order pass re-anchors child parent/arg_key/index pointers via original.set(k, v). Besides fixing pointers, set() clears cached structural hashes up the ancestor chain — but this loop never changes a value, and real mutations already invalidate through set/replace/pop. The wasted invalidation forced while_changing to rehash entire condition subtrees on every (mostly no-op) pass just to detect the fix point.

Fix pointers directly with _set_parent, preserving set(k, None)'s arg-removal semantics. Safe because __hash__ only covers key + args values, which this loop doesn't touch.

Simplify step is ~5-12% faster (condition and TPC fixtures), full optimize() ~2%. Optimized output is byte-identical on all fixtures; pure and compiled suites green.

…er-reset loop

_simplify's post-order pass re-anchors every child's parent/arg_key/index
pointers via set(), which also clears cached structural hashes up the
ancestor chain. The loop never changes values (it rewrites args[k] with
itself), and actual mutations already invalidate at mutation time through
set/replace/pop, so the only effect of the extra invalidation was forcing
while_changing to rehash entire condition subtrees after every (mostly
no-op) pass to detect the fix point.

Fix pointers directly with _set_parent, preserving set(k, None)'s
arg-removal semantics. Simplify step ~5-12% faster across condition and
TPC fixtures; output is byte-identical.
@github-actions

Copy link
Copy Markdown
Contributor

SQLGlot Integration Test Results

✅ All tests passed

Comparing:

  • this branch (sqlglot:perf/simplify-speedups @ sqlglot c9cee62)
  • baseline (main @ sqlglot a76439c)

Overall

main: 192441 total, 153536 passed (pass rate: 79.8%)

sqlglot:perf/simplify-speedups: 180247 total, 142391 passed (pass rate: 79.0%)

Transitions:
No change

Dialect pair changes: 0 previous results not found, 3 current results not found

✅ All tests passed

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.

2 participants