Skip to content

Implement HTM without eval - #53

Closed
jviide wants to merge 14 commits into
developit:masterfrom
jviide:no-eval
Closed

jviide wants to merge 14 commits into
developit:masterfrom
jviide:no-eval

Conversation

@jviide

@jviide jviide commented Dec 24, 2018 •

Copy link
Copy Markdown
Collaborator

As pointed out in #51, the current version of HTM uses code evaluation. This prohibits setting a strict Content-Security Policy.

This pull request implements HTM without code generation. Instead the parsing phase generates a list of "instructions", which the result can be evaluated from. For each instruction we append to the list FIELD, VALUE and OP, and for one special instruction (PROPS_SET) we append an additional KEY after the OP. Possible OPs are:

  • TAG_SET: set the tag to FIELD ? fields[FIELD] : VALUE
  • PROPS_SET: set prop KEY to FIELD ? fields[FIELD] : VALUE
  • PROPS_ASSIGN: Object.assign FIELD ? fields[FIELD] : VALUE to props
  • CHILD_RECURSE: evaluate FIELD ? fields[FIELD] : VALUE and append to children
  • CHILD_APPEND: append FIELD ? fields[FIELD] : VALUE to children

For some of these OPs for example VALUE is not relevant, but they're there anyway (set to 0) to keep the evaluation code compact.

To compress the parsing logic MODE_SKIP is now replaced by MODE_SLASH, signifying "we're still parsing a tag, but have encountered a slash, so ignore everything until a TAG_END".

The .gz/.br code size is smaller compared to the current master branch version. The "creation" benchmark numbers are higher, but the "usage" benchmark is lower. Note that the benchmarks were run without Jest.

Before change

Build "htm" to dist:
        649 B: htm.mjs.gz
        596 B: htm.mjs.br
        714 B: htm.umd.js.gz
        645 B: htm.umd.js.br
Creation: 37,783/s, average: 26µs
Usage: 520,896/s, average: 1µs

After change

Build "htm" to dist:
        602 B: htm.mjs.gz
        544 B: htm.mjs.br
        669 B: htm.umd.js.gz
        594 B: htm.umd.js.br
Creation: 43,598/s, average: 22µs
Usage: 364,405/s, average: 2µs

The Babel plugin needed to be modified, as the props assignment pattern changed.

@jviide jviide mentioned this pull request Dec 24, 2018
@jviide

jviide commented Dec 26, 2018 •

Copy link
Copy Markdown
Collaborator Author

Pushed an update that brings down the compressed sizes without affecting the benchmarks:

Build "htm" to dist:
        597 B: htm.mjs.gz
        531 B: htm.mjs.br
        664 B: htm.umd.js.gz
        583 B: htm.umd.js.br

@jviide
jviide requested a review from developit December 26, 2018 19:21
@jviide

jviide commented Dec 27, 2018 •

Copy link
Copy Markdown
Collaborator Author

Pushed three suggested commits that can be rolled back if needed:

These changes together make the code size smaller, but with some performance loss (mostly from multi-root support, creation benchmark still being faster than the master version):

Build "htm" to dist:
        579 B: htm.mjs.gz
        521 B: htm.mjs.br
        645 B: htm.umd.js.gz
        581 B: htm.umd.js.br
Creation: 39,616/s, average: 25µs
Usage: 344,077/s, average: 2µs

@jviide

jviide commented Dec 27, 2018

Copy link
Copy Markdown
Collaborator Author

It should be mentioned that at least one @mikesherov's size optimization from #48 is also included in this PR.

@mikesherov

Copy link
Copy Markdown

Ha, beat me to the punch. I was literally fiddling around with getting rid of charAt and charCodeAt myself.

@mikesherov mikesherov left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Just a couple of more potential size opts.

Comment thread src/build.mjs Outdated
// char is a quote && (quote === char || quote is empty)
quote = quote ? '' : char;
}
else if (idx === 2) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

You could attempt < 3 here (if perf is a non-issue in this case), same for the other else ifs.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Huge thanks for the review! Seems that compression is a fickle beast - <s brought some file sizes up, some down. So maybe === in this case?

Comment thread src/build.mjs Outdated
current = [current];
mode = MODE_TAGNAME;
}
else if (mode !== MODE_TEXT && (idx = '\'">=/\t\n\r '.indexOf(char)) >= 0 && char.indexOf(quote) >= 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can replace the >= 0 with prefix ~ if it helps.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Also, maybe you did it for compressibility, but char === quote instead of char.indexOf(quote) >= 0, no?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

~ brought some file sizes up, some down, so I kept the >= as it's probably a bit more explicit (YMMV).

The char.indexOf part was there as a shorthand for char === quote || quote === ''. But now that you mention it, char === quote || !quote seems be just as good for compressibility (some files up, some files down) and quite a bit clearer in intent 😅

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

File sizes after removing char.indexOf:

Build "htm" to dist:
        583 B: htm.mjs.gz
        519 B: htm.mjs.br
        649 B: htm.umd.js.gz
        579 B: htm.umd.js.br

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

the explicit checks should also improve performance a tiny bit!

Comment thread src/build.mjs Outdated
current = [current];
mode = MODE_TAGNAME;
}
else if (mode !== MODE_TEXT && (char === quote || !quote) && (idx = '\'">=/\t\n\r '.indexOf(char)) >= 0) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This might be quite a bit more expensive than char.charCodeAt(0) + integer comparisons. Any idea if it affected the benchmarks much?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Interestingly, in my environment, the char = statics[i][j] approach seems to be the fastest:

Build "htm" to dist:
        604 B: htm.mjs.gz
        537 B: htm.mjs.br
        672 B: htm.umd.js.gz
        591 B: htm.umd.js.br
Creation: 41,385/s, average: 24µs
Usage: 348,137/s, average: 2µs

.indexOf makes it a bit slower, but more compact:

Build "htm" to dist:
        583 B: htm.mjs.gz
        519 B: htm.mjs.br
        649 B: htm.umd.js.gz
        579 B: htm.umd.js.br
Creation: 40,209/s, average: 24µs
Usage: 342,160/s, average: 2µs

The perf of the .charCodeAt approach is pretty much the same as of .indexOf, but with increased size:

Build "htm" to dist:
        617 B: htm.mjs.gz
        556 B: htm.mjs.br
        684 B: htm.umd.js.gz
        601 B: htm.umd.js.br
Creation: 39,666/s, average: 25µs
Usage: 344,306/s, average: 2µs

Comment thread src/build.mjs
}

for (let j=0; j<statics[i].length; j++) {
char = statics[i][j];

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

may be benefit to using = statics[i][j|0]:

https://esbench.com/bench/5c181d294cd7e6009ef61e48

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This doesn't seem to have a notable effect (in my environment).

@jviide

jviide commented Dec 27, 2018

Copy link
Copy Markdown
Collaborator Author

Rolling back the multi-root support but keeping everything else in this PR gives the following numbers:

Build "htm" to dist:
        560 B: htm.mjs.gz
        500 B: htm.mjs.br
        624 B: htm.umd.js.gz
        554 B: htm.umd.js.br
Creation: 42,794/s, average: 23µs
Usage: 357,357/s, average: 2µs

@jviide

jviide commented Jan 16, 2019 •

Copy link
Copy Markdown
Collaborator Author

After the latest code changes the code sizes are (note that this version includes the multi-root support):

Build "htm" to dist:
        548 B: htm.mjs.gz
        488 B: htm.mjs.br
        607 B: htm.umd.js.gz
        545 B: htm.umd.js.br

And the perf-test-harness benchmarks:

  performance
    ✅  creation
       Creation: 88,647/s, average: 11µs
    ✅  usage
       Usage: 440,037/s, average: 2µs

Use Map for caching when available,
otherwise fall back to the old caching
scheme based on string keys.

These changes were code-golfed to
their current form together with @developit.
@jviide

jviide commented Jan 21, 2019

Copy link
Copy Markdown
Collaborator Author

The latest version is one we code-golfed together with @developit. It uses Map for caching when available, and otherwise falls back to current the string key based caching scheme. This boosts performance quite a bit on platforms where a native Map implementation is available, with no performance loss on platforms that don't. The current benchmark numbers on a platform that supports Map natively are:

Creation: 83,069/s, average: 12µs
Usage: 717,657/s, average: 1µs

The corresponding numbers on the current master branch are:

Creation: 15,978/s, average: 62µs
Usage: 471,498/s, average: 2µs

The file sizes got bigger, but mostly stayed under 600 bytes:

Build "htm" to dist:
        591 B: htm.mjs.gz
        540 B: htm.mjs.br
        654 B: htm.umd.js.gz
        584 B: htm.umd.js.b

@jviide

jviide commented Feb 20, 2019

Copy link
Copy Markdown
Collaborator Author

Closing this in favor of #62, which essentially includes the features of this PR as well as #59.

@jviide jviide closed this Feb 20, 2019
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