Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lit-html] Fix ref bug when auto-bound class method used as as callback #2691

Merged
merged 6 commits into from
Apr 1, 2022

Conversation

kevinpschaaf
Copy link
Member

The WeakMap used to store the last element called to a given callback is keyed on the callback function. Since unbound LitElement class methods passed as callbacks will all have the same references between class instances, this will result in incorrect tracking of the last element per unique callback, given that callbacks called on different context's should be considered unique.

The change here uses nested WeakMaps to effectively double-key the lookup on both function and context.

Fixes #2677.

@changeset-bot
Copy link

changeset-bot bot commented Mar 31, 2022

🦋 Changeset detected

Latest commit: e915bde

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Mar 31, 2022

📊 Tachometer Benchmark Results

Summary

nop-update

  • lit-html-kitchen-sink: unsure 🔍 -5% - +9% (-1.72ms - +3.28ms)
    this-change vs tip-of-tree

render

  • lit-element-list: unsure 🔍 -3% - +2% (-3.10ms - +2.45ms)
    this-change vs tip-of-tree
  • lit-html-kitchen-sink: unsure 🔍 -3% - +4% (-1.31ms - +1.70ms)
    this-change vs tip-of-tree
  • lit-html-repeat: unsure 🔍 -3% - +12% (-0.38ms - +1.59ms)
    this-change vs tip-of-tree
  • lit-html-template-heavy: unsure 🔍 -3% - +3% (-2.11ms - +1.96ms)
    this-change vs tip-of-tree
  • reactive-element-list: unsure 🔍 -2% - +2% (-1.78ms - +1.36ms)
    this-change vs tip-of-tree

update

  • lit-element-list: unsure 🔍 -2% - +2% (-24.33ms - +22.57ms)
    this-change vs tip-of-tree
  • lit-html-kitchen-sink: unsure 🔍 -4% - +2% (-5.11ms - +2.13ms)
    this-change vs tip-of-tree
  • lit-html-repeat: unsure 🔍 -3% - +6% (-11.33ms - +20.52ms)
    this-change vs tip-of-tree
  • lit-html-template-heavy: unsure 🔍 -1% - +5% (-1.72ms - +7.39ms)
    this-change vs tip-of-tree
  • reactive-element-list: unsure 🔍 -2% - +3% (-18.70ms - +28.15ms)
    this-change vs tip-of-tree

update-reflect

  • lit-element-list: unsure 🔍 -2% - +2% (-23.76ms - +21.29ms)
    this-change vs tip-of-tree
  • reactive-element-list: unsure 🔍 -1% - +3% (-14.42ms - +32.54ms)
    this-change vs tip-of-tree

Results

lit-element-list

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
104.27ms - 108.05ms-unsure 🔍
-3% - +2%
-3.10ms - +2.45ms
faster ✔
19% - 24%
24.72ms - 33.20ms
tip-of-tree
tip-of-tree
104.45ms - 108.51msunsure 🔍
-2% - +3%
-2.45ms - +3.10ms
-faster ✔
19% - 24%
24.33ms - 32.94ms
previous-release
previous-release
131.32ms - 138.92msslower ❌
23% - 32%
24.72ms - 33.20ms
slower ❌
23% - 31%
24.33ms - 32.94ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
995.62ms - 1027.85ms-unsure 🔍
-2% - +2%
-24.33ms - +22.57ms
faster ✔
6% - 10%
61.75ms - 109.42ms
tip-of-tree
tip-of-tree
995.59ms - 1029.65msunsure 🔍
-2% - +2%
-22.57ms - +24.33ms
-faster ✔
6% - 10%
60.24ms - 109.17ms
previous-release
previous-release
1079.76ms - 1114.88msslower ❌
6% - 11%
61.75ms - 109.42ms
slower ❌
6% - 11%
60.24ms - 109.17ms
-

update-reflect

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
1085.25ms - 1114.24ms-unsure 🔍
-2% - +2%
-23.76ms - +21.29ms
faster ✔
3% - 7%
39.87ms - 85.80ms
tip-of-tree
tip-of-tree
1083.74ms - 1118.22msunsure 🔍
-2% - +2%
-21.29ms - +23.76ms
-faster ✔
3% - 7%
36.82ms - 86.39ms
previous-release
previous-release
1144.77ms - 1180.39msslower ❌
4% - 8%
39.87ms - 85.80ms
slower ❌
3% - 8%
36.82ms - 86.39ms
-
lit-html-kitchen-sink

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
42.33ms - 44.77ms-unsure 🔍
-3% - +4%
-1.31ms - +1.70ms
faster ✔
10% - 17%
4.70ms - 8.49ms
tip-of-tree
tip-of-tree
42.47ms - 44.24msunsure 🔍
-4% - +3%
-1.70ms - +1.31ms
-faster ✔
10% - 17%
5.09ms - 8.49ms
previous-release
previous-release
48.70ms - 51.60msslower ❌
11% - 20%
4.70ms - 8.49ms
slower ❌
12% - 20%
5.09ms - 8.49ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
111.04ms - 115.97ms-unsure 🔍
-4% - +2%
-5.11ms - +2.13ms
faster ✔
1% - 8%
1.41ms - 9.40ms
tip-of-tree
tip-of-tree
112.34ms - 117.64msunsure 🔍
-2% - +5%
-2.13ms - +5.11ms
-unsure 🔍
-7% - +0%
-8.03ms - +0.19ms
previous-release
previous-release
115.77ms - 122.06msslower ❌
1% - 8%
1.41ms - 9.40ms
unsure 🔍
-0% - +7%
-0.19ms - +8.03ms
-

nop-update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
33.77ms - 38.59ms-unsure 🔍
-5% - +9%
-1.72ms - +3.28ms
unsure 🔍
-12% - +1%
-4.81ms - +0.31ms
tip-of-tree
tip-of-tree
34.74ms - 36.06msunsure 🔍
-9% - +5%
-3.28ms - +1.72ms
-faster ✔
5% - 11%
1.94ms - 4.13ms
previous-release
previous-release
37.56ms - 39.30msunsure 🔍
-1% - +14%
-0.31ms - +4.81ms
slower ❌
5% - 12%
1.94ms - 4.13ms
-
lit-html-repeat

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
13.73ms - 15.30ms-unsure 🔍
-3% - +12%
-0.38ms - +1.59ms
faster ✔
2% - 13%
0.24ms - 2.00ms
tip-of-tree
tip-of-tree
13.32ms - 14.50msunsure 🔍
-11% - +2%
-1.59ms - +0.38ms
-faster ✔
7% - 15%
1.01ms - 2.44ms
previous-release
previous-release
15.24ms - 16.04msslower ❌
1% - 14%
0.24ms - 2.00ms
slower ❌
7% - 18%
1.01ms - 2.44ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
358.98ms - 385.28ms-unsure 🔍
-3% - +6%
-11.33ms - +20.52ms
faster ✔
29% - 34%
154.23ms - 187.27ms
tip-of-tree
tip-of-tree
358.55ms - 376.51msunsure 🔍
-5% - +3%
-20.52ms - +11.33ms
-faster ✔
30% - 34%
161.91ms - 188.79ms
previous-release
previous-release
532.88ms - 552.88msslower ❌
40% - 52%
154.23ms - 187.27ms
slower ❌
43% - 52%
161.91ms - 188.79ms
-
lit-html-template-heavy

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
68.78ms - 71.33ms-unsure 🔍
-3% - +3%
-2.11ms - +1.96ms
faster ✔
14% - 18%
11.65ms - 15.20ms
tip-of-tree
tip-of-tree
68.55ms - 71.72msunsure 🔍
-3% - +3%
-1.96ms - +2.11ms
-faster ✔
14% - 18%
11.34ms - 15.36ms
previous-release
previous-release
82.25ms - 84.72msslower ❌
16% - 22%
11.65ms - 15.20ms
slower ❌
16% - 22%
11.34ms - 15.36ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
151.56ms - 158.16ms-unsure 🔍
-1% - +5%
-1.72ms - +7.39ms
faster ✔
9% - 14%
16.16ms - 25.68ms
tip-of-tree
tip-of-tree
148.88ms - 155.16msunsure 🔍
-5% - +1%
-7.39ms - +1.72ms
-faster ✔
11% - 16%
19.10ms - 28.40ms
previous-release
previous-release
172.35ms - 179.20msslower ❌
10% - 17%
16.16ms - 25.68ms
slower ❌
12% - 19%
19.10ms - 28.40ms
-
reactive-element-list

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
71.53ms - 73.85ms-unsure 🔍
-2% - +2%
-1.78ms - +1.36ms
unsure 🔍
-3% - +2%
-2.05ms - +1.11ms
tip-of-tree
tip-of-tree
71.84ms - 73.97msunsure 🔍
-2% - +2%
-1.36ms - +1.78ms
-unsure 🔍
-2% - +2%
-1.77ms - +1.25ms
previous-release
previous-release
72.09ms - 74.24msunsure 🔍
-2% - +3%
-1.11ms - +2.05ms
unsure 🔍
-2% - +2%
-1.25ms - +1.77ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
1039.42ms - 1071.51ms-unsure 🔍
-2% - +3%
-18.70ms - +28.15ms
unsure 🔍
-2% - +2%
-18.49ms - +26.11ms
tip-of-tree
tip-of-tree
1033.67ms - 1067.81msunsure 🔍
-3% - +2%
-28.15ms - +18.70ms
-unsure 🔍
-2% - +2%
-23.97ms - +22.13ms
previous-release
previous-release
1036.17ms - 1067.15msunsure 🔍
-2% - +2%
-26.11ms - +18.49ms
unsure 🔍
-2% - +2%
-22.13ms - +23.97ms
-

update-reflect

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
1145.64ms - 1181.92ms-unsure 🔍
-1% - +3%
-14.42ms - +32.54ms
unsure 🔍
-1% - +3%
-14.50ms - +31.96ms
tip-of-tree
tip-of-tree
1139.81ms - 1169.63msunsure 🔍
-3% - +1%
-32.54ms - +14.42ms
-unsure 🔍
-2% - +2%
-21.14ms - +20.48ms
previous-release
previous-release
1140.53ms - 1169.57msunsure 🔍
-3% - +1%
-31.96ms - +14.50ms
unsure 🔍
-2% - +2%
-20.48ms - +21.14ms
-

tachometer-reporter-action v2 for Benchmarks

// has already been rendered to a new spot. It is double-keyed on both the
// callback and the context (options.host), since we auto-bind class methods to
// options.host.
const lastElementForCallback: WeakMap<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the WeakMap setup would make more sense if it was keyed off the host first.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Co-authored-by: Andrew Jakubowicz <spyr1014@gmail.com>
Copy link
Contributor

@AndrewJakubowicz AndrewJakubowicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed - although primarily syntax and logic surrounding the issue, since lack expertise in this domain.
This is awesome! TIL! Test is fantastic, and only comments are nits and clarifying my own understanding.

packages/lit-html/src/directives/ref.ts Outdated Show resolved Hide resolved
packages/lit-html/src/directives/ref.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@AndrewJakubowicz AndrewJakubowicz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brilliant!

@kevinpschaaf kevinpschaaf merged commit 171143b into main Apr 1, 2022
@kevinpschaaf kevinpschaaf deleted the ref-bound-callback-fix branch April 1, 2022 17:11
@lit-robot lit-robot mentioned this pull request Apr 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[lit-html] Ref callbacks incorrect when unbound class methods used as callbacks
3 participants