Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@AndrewJakubowicz
Copy link
Contributor

Issue: #189
RFC: lit/rfcs#21
Full prototype PR: #3984

Context

This is part of handling all the edge cases we test in lit-html.

We specifically throw a dev warning if someone uses a deprecated octal escape sequence in their template. Unfortunately the compiler doesn't use this code path, and the prepared template will also skip this code path.

This is important because a TTL containing one of these escape sequences will get an undefined entry in the TTL's static strings array in whichever index contained that deprecated escape.

Without this change, the following template:

html`<p>Hi!</p>\2`

Will render nothing at all with an uncompiled template (and throw a DEV_MODE warning).

However when compiled, it instead renders the text undefined.

Fix

Hand roll detection of these deprecated octal escape sequences, and do not compile those templates. This would then allow the runtime lit DEV_MODE warning to inform the user.

We could also add a warning to the compiler detection.

Risk

The main risk of this change is a bug in the regex resulting in deoptimizations due to false positives preventing template optimization. This behavior is more correct than what currently happens with these sequences.

Alternatives

  • Maybe do nothing? Is this simply invalid code?
  • A more robust detection mechanism. The following code: "\9" throws natively. Could we somehow construct that from the ast raw string and catch the error? I couldn't think of any way that doesn't result in eval or Function exploits.

@changeset-bot
Copy link

changeset-bot bot commented Aug 11, 2023

🦋 Changeset detected

Latest commit: dd57252

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

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

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 Aug 11, 2023

📊 Tachometer Benchmark Results

Summary

nop-update

  • this-change, tip-of-tree, previous-release: unsure 🔍 -10% - +2% (-2.12ms - +0.52ms)
    this-change vs tip-of-tree

render

  • this-change: 66.39ms - 76.31ms
  • this-change, tip-of-tree, previous-release: unsure 🔍 -4% - +7% (-0.95ms - +1.72ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -2% - +1% (-0.89ms - +0.50ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -2% - +1% (-0.93ms - +0.56ms)
    this-change vs tip-of-tree

update

  • this-change: 648.91ms - 659.74ms
  • this-change, tip-of-tree, previous-release: unsure 🔍 -2% - +7% (-1.62ms - +4.83ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -2% - +2% (-1.66ms - +1.64ms)
    this-change vs tip-of-tree
  • this-change, tip-of-tree, previous-release: unsure 🔍 -0% - +1% (-2.33ms - +3.71ms)
    this-change vs tip-of-tree

update-reflect

  • this-change: 625.92ms - 634.83ms
  • this-change, tip-of-tree, previous-release: unsure 🔍 -1% - +0% (-4.60ms - +1.62ms)
    this-change vs tip-of-tree

Results

this-change

render

VersionAvg timevs
66.39ms - 76.31ms-

update

VersionAvg timevs
648.91ms - 659.74ms-

update-reflect

VersionAvg timevs
625.92ms - 634.83ms-
this-change, tip-of-tree, previous-release

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
25.74ms - 27.68ms-unsure 🔍
-4% - +7%
-0.95ms - +1.72ms
unsure 🔍
-7% - +3%
-1.78ms - +0.75ms
tip-of-tree
tip-of-tree
25.40ms - 27.24msunsure 🔍
-6% - +4%
-1.72ms - +0.95ms
-unsure 🔍
-8% - +1%
-2.13ms - +0.32ms
previous-release
previous-release
26.41ms - 28.04msunsure 🔍
-3% - +7%
-0.75ms - +1.78ms
unsure 🔍
-1% - +8%
-0.32ms - +2.13ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
69.19ms - 73.61ms-unsure 🔍
-2% - +7%
-1.62ms - +4.83ms
unsure 🔍
-4% - +5%
-2.77ms - +3.86ms
tip-of-tree
tip-of-tree
67.45ms - 72.15msunsure 🔍
-7% - +2%
-4.83ms - +1.62ms
-unsure 🔍
-6% - +3%
-4.47ms - +2.35ms
previous-release
previous-release
68.38ms - 73.33msunsure 🔍
-5% - +4%
-3.86ms - +2.77ms
unsure 🔍
-3% - +6%
-2.35ms - +4.47ms
-

nop-update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
18.54ms - 20.22ms-unsure 🔍
-10% - +2%
-2.12ms - +0.52ms
unsure 🔍
-10% - +2%
-2.05ms - +0.39ms
tip-of-tree
tip-of-tree
19.17ms - 21.20msunsure 🔍
-3% - +11%
-0.52ms - +2.12ms
-unsure 🔍
-7% - +7%
-1.37ms - +1.32ms
previous-release
previous-release
19.32ms - 21.09msunsure 🔍
-2% - +11%
-0.39ms - +2.05ms
unsure 🔍
-7% - +7%
-1.32ms - +1.37ms
-
this-change, tip-of-tree, previous-release

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
47.06ms - 48.09ms-unsure 🔍
-2% - +1%
-0.89ms - +0.50ms
unsure 🔍
-3% - +1%
-1.36ms - +0.37ms
tip-of-tree
tip-of-tree
47.30ms - 48.23msunsure 🔍
-1% - +2%
-0.50ms - +0.89ms
-unsure 🔍
-2% - +1%
-1.13ms - +0.53ms
previous-release
previous-release
47.38ms - 48.76msunsure 🔍
-1% - +3%
-0.37ms - +1.36ms
unsure 🔍
-1% - +2%
-0.53ms - +1.13ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
97.33ms - 99.67ms-unsure 🔍
-2% - +2%
-1.66ms - +1.64ms
unsure 🔍
-2% - +1%
-2.05ms - +1.28ms
tip-of-tree
tip-of-tree
97.35ms - 99.68msunsure 🔍
-2% - +2%
-1.64ms - +1.66ms
-unsure 🔍
-2% - +1%
-2.04ms - +1.29ms
previous-release
previous-release
97.69ms - 100.08msunsure 🔍
-1% - +2%
-1.28ms - +2.05ms
unsure 🔍
-1% - +2%
-1.29ms - +2.04ms
-
this-change, tip-of-tree, previous-release

render

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
42.66ms - 43.74ms-unsure 🔍
-2% - +1%
-0.93ms - +0.56ms
unsure 🔍
-3% - +0%
-1.33ms - +0.13ms
tip-of-tree
tip-of-tree
42.86ms - 43.90msunsure 🔍
-1% - +2%
-0.56ms - +0.93ms
-unsure 🔍
-3% - +1%
-1.13ms - +0.30ms
previous-release
previous-release
43.30ms - 44.29msunsure 🔍
-0% - +3%
-0.13ms - +1.33ms
unsure 🔍
-1% - +3%
-0.30ms - +1.13ms
-

update

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
634.07ms - 638.79ms-unsure 🔍
-0% - +1%
-2.33ms - +3.71ms
unsure 🔍
-1% - +0%
-5.39ms - +1.90ms
tip-of-tree
tip-of-tree
633.86ms - 637.61msunsure 🔍
-1% - +0%
-3.71ms - +2.33ms
-unsure 🔍
-1% - +0%
-5.78ms - +0.91ms
previous-release
previous-release
635.41ms - 640.94msunsure 🔍
-0% - +1%
-1.90ms - +5.39ms
unsure 🔍
-0% - +1%
-0.91ms - +5.78ms
-

update-reflect

VersionAvg timevs this-change
vs tip-of-tree
tip-of-tree
vs previous-release
previous-release
this-change
641.82ms - 646.41ms-unsure 🔍
-1% - +0%
-4.60ms - +1.62ms
unsure 🔍
-1% - +0%
-4.34ms - +1.84ms
tip-of-tree
tip-of-tree
643.51ms - 647.70msunsure 🔍
-0% - +1%
-1.62ms - +4.60ms
-unsure 🔍
-0% - +0%
-2.70ms - +3.19ms
previous-release
previous-release
643.30ms - 647.43msunsure 🔍
-0% - +1%
-1.84ms - +4.34ms
unsure 🔍
-0% - +0%
-3.19ms - +2.70ms
-

tachometer-reporter-action v2 for Benchmarks

Copy link
Collaborator

@justinfagnani justinfagnani left a comment

Choose a reason for hiding this comment

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

Just a couple style nits. Looks great!

// - Chromium octal escape sequence scanner:
// https://source.chromium.org/chromium/chromium/src/+/181fa1f62f501c27ed19fcb69206a0e2e1eff513:v8/src/parsing/scanner.cc;l=452-460
if (/^([^\\|\s]|\\.)*?\\(0)*[1-9]/gm.test(staticString)) {
return {shouldCompile: false};
Copy link
Collaborator

Choose a reason for hiding this comment

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

If this will cause a warning at runtime, why don't we error or log a warning here?

Thinking ahead, ultimately we'd like to be able to use the compiler to drop the template prep code from the client bundle. We'll need to compile every template for that.

Copy link
Contributor Author

@AndrewJakubowicz AndrewJakubowicz Aug 12, 2023

Choose a reason for hiding this comment

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

That is a great point. Currently the compiler has been implemented in a conservative manner where any prepare phase DEV_MODE warnings simply don't compile the template – deferring it to runtime.

E.g.:

  • dynamic tag binding
  • bindings in <template> | <textarea>
  • octal escape

This technique feels like it should be the easiest for adoption, because we are very specific about what to compile. But, I agree that we need a strategy into informing and helping developers fix the issues that deoptimize templates so we can get to that world with complete removal of prepare in certain projects.

Do you think adding console.warn is a reasonable starting goal?

Copy link
Collaborator

@rictic rictic left a comment

Choose a reason for hiding this comment

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

Awesome work, and great comments!

@AndrewJakubowicz AndrewJakubowicz merged commit e363e2c into main Aug 14, 2023
@AndrewJakubowicz AndrewJakubowicz deleted the compiler-dev-warning-handling branch August 14, 2023 21:28
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.

3 participants