gradle-baseline icon indicating copy to clipboard operation
gradle-baseline copied to clipboard

Safety propagation avoids recursive analysis for pattern match variables

Open kevinychen opened this issue 1 year ago • 1 comments

Fixes #2850

  return switch (parent) {
      case Child1 child -> child.id();
      case Child2 child -> child.id();
  };

In a pattern matching switch, we never seem to add the pattern-matched variables (child) to the AccessPathStore. For example, for the pattern-matching instance of, we update the store here. (It might be possible to update visitSwitchExpressionNode in a similar way, but I think that requires Java 17+ compiler APIs.)

This means that when we visit child.id(), we don't have a safety value for child and we enter this code path, triggering a recursive analysis.

After this PR

In this PR, every time we need to lazily compute a safety value in visitLocalVariable, we also add it to the store so downstream nodes don't need to recompute it. The first time we visit child, it's in the pattern binding (case Child1 child) and we compute the safety quickly here. The subsequent times we visit child (child.id()), we now get the safety from the store instead of triggering a recursive analysis.

==COMMIT_MSG== Fix slow compilation for large pattern matching switch ==COMMIT_MSG==

Possible downsides?

It's probably weird to add a safety value to the store in visitLocalVariable, since semantically it should be added higher up (e.g. for instance of, it's correctly added in visitInstanceOf. But I think the entire if (safety == null) { ... } block in visitLocalVariable is intended to handle these weird cases where we didn't properly add a safety value to the store, so this change at least feels consistent.

kevinychen avatar Aug 28 '24 07:08 kevinychen

Generate changelog in changelog/@unreleased

What do the change types mean?
  • feature: A new feature of the service.
  • improvement: An incremental improvement in the functionality or operation of the service.
  • fix: Remedies the incorrect behaviour of a component of the service in a backwards-compatible way.
  • break: Has the potential to break consumers of this service's API, inclusive of both Palantir services and external consumers of the service's API (e.g. customer-written software or integrations).
  • deprecation: Advertises the intention to remove service functionality without any change to the operation of the service itself.
  • manualTask: Requires the possibility of manual intervention (running a script, eyeballing configuration, performing database surgery, ...) at the time of upgrade for it to succeed.
  • migration: A fully automatic upgrade migration task with no engineer input required.

Note: only one type should be chosen.

How are new versions calculated?
  • ❗The break and manual task changelog types will result in a major release!
  • 🐛 The fix changelog type will result in a minor release in most cases, and a patch release version for patch branches. This behaviour is configurable in autorelease.
  • ✨ All others will result in a minor version release.

Type

  • [ ] Feature
  • [x] Improvement
  • [ ] Fix
  • [ ] Break
  • [ ] Deprecation
  • [ ] Manual task
  • [ ] Migration

Description Fix slow compilation for large pattern matching switch

Check the box to generate changelog(s)

  • [x] Generate changelog entry

changelog-app[bot] avatar Aug 28 '24 07:08 changelog-app[bot]

Released 5.71.0

autorelease3[bot] avatar Oct 18 '24 13:10 autorelease3[bot]