Skip to content

test: pin identity-function optimization behavior (#815)#888

Open
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:audit/identity-function-815
Open

test: pin identity-function optimization behavior (#815)#888
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:audit/identity-function-815

Conversation

@He-Pin
Copy link
Copy Markdown
Contributor

@He-Pin He-Pin commented May 31, 2026

Motivation

sjsonnet has several aggressive identity-function fast paths:

  • apply1 elides calls to (effectively) identity functions (Val.Func.isEffectivelyIdentity);
  • StaticOptimizer statically classifies self-composition chains (staticIdentityShape);
  • Val.Func caches an effective-identity state;
  • set/sort/uniq/setUnion/setInter/setDiff short-circuit keyF=id/default, plus a function(x) -x fast path.

These were not tested as a group, so a future change could silently break an elision — e.g. start treating a non-identity lookalike as identity, or drop laziness. Issue #815 asked for an audit + regression coverage.

Modification

  • Audit: verified every identity fast path against official Jsonnet v0.22.0 — all correct, including the safety-critical cases (non-identity lookalikes are not elided; laziness and traces preserved).
  • identity_function_optimization.jsonnet (new_test_suite) pins: direct identity elision; self-composition over an identity g (depth 2 and 3, plus cached repeat calls); rejection of lookalikes (g(g(x)) with g=x+1; captured-var body; two-param function(x, y) x); keyF=id in sort/uniq/set/setUnion/setInter/setDiff; the function(x) -x sort fast path; and preserved laziness (identity map over an array containing error does not force it).
  • EvaluatorTests."identityFunctionTraces": the elision forces the argument exactly once (direct and self-composition), and a lazy identity map does not force unused elements (no spurious trace).

Result

No behavior change — the audit found the optimizations sound; these tests lock in their semantics so the aggressive elisions can't silently regress. Full JVM test suite green (EvaluatorTests runs both the old and new evaluator variants).

Addresses #815.

Motivation:
sjsonnet has several aggressive identity-function fast paths: apply1 elides calls to
(effectively) identity functions, StaticOptimizer statically classifies self-composition chains
(staticIdentityShape), Val.Func caches an effective-identity state, and set/sort/uniq short-circuit
keyF=id/default. These were untested as a group, so a future change could silently break an elision
(e.g. start treating a non-identity lookalike as identity, or drop laziness). Issue databricks#815 asked for
an audit + regression coverage.

Modification:
- Audit: verified every identity fast path against official Jsonnet v0.22.0 — all correct, including
  the safety-critical cases (non-identity lookalikes are NOT elided; laziness/traces preserved).
- Add identity_function_optimization.jsonnet (new_test_suite) pinning: direct identity elision,
  self-composition over an identity g (depth 2 and 3, plus cached repeat calls), rejection of
  lookalikes (g(g(x)) with g=x+1; captured-var body; two-param function(x,y) x), keyF=id in
  sort/uniq/set/setUnion/setInter/setDiff, the function(x) -x sort fast path, and preserved laziness
  (identity map over an array containing `error` does not force it).
- Add EvaluatorTests."identityFunctionTraces": the elision forces the argument exactly once (direct
  and self-composition) and a lazy identity map does not force unused elements (no spurious trace).

Result:
No behavior change — the audit found the optimizations sound; these tests lock in their semantics.
Full JVM test suite green (EvaluatorTests runs both evaluator variants).

References: databricks#815
@He-Pin He-Pin marked this pull request as draft May 31, 2026 11:16
@He-Pin He-Pin marked this pull request as ready for review May 31, 2026 11:16
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