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

First iteration of performance tweaks #27

Merged
merged 9 commits into from
Nov 16, 2016
Merged

First iteration of performance tweaks #27

merged 9 commits into from
Nov 16, 2016

Conversation

mrmlnc
Copy link
Member

@mrmlnc mrmlnc commented Nov 16, 2016

Proposed Changes

Replacing the placeholders

string.replace 2-2.5x faster than vm.runInContext, so we can use string.replace for simple expression (without dynamic calculations).

If the expression has something besides letters and numbers - use VM otherwise replace.

Benchmark [150x {{ key }}]

runInContext runInContext + replace faster
234 op/s 530 op/s 2.3x

Loop statement parser

Use new solution for parsing loop statement

Benchmark

Current solution New solution faster
196 450 op/s 1 798 479 op/s 9.2x

Manual expansion the expression of conditions

Benchmark [20x if - else if -else if - else]

Current solution New solution faster
322 op/s 349 op/s 1.08x

Types of Changes

Fully compatible with the current version.

@coveralls
Copy link

coveralls commented Nov 16, 2016

Coverage Status

Coverage decreased (-2.0%) to 96.825% when pulling b86b401 on mrmlnc:performance-1 into d80c026 on posthtml:master.

@mrmlnc
Copy link
Member Author

mrmlnc commented Nov 16, 2016

Don't merge this now, please. I want to add some tests.

P.S.: But I'll be glad if you look your standards.

Copy link
Member

@michael-ciniawsky michael-ciniawsky left a comment

Choose a reason for hiding this comment

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

👍 Thx for your hard work so far and especially the perf (benchmark) results are very impressive. Pls ping me here, when you think it's time to merge it in :). I grant you write access to this repo later

@@ -2,5 +2,7 @@ language: node_js
sudo: false
Copy link
Member

Choose a reason for hiding this comment

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

language: node_js
node_js:
  - node
  - 6
  - 4
cache:
  directories:
    - node_modules
before_script:
  - npm i
script:
  - npm test
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
notifications:
  email: false

// Since we are matching multiple sets of delimiters, we need to run a loop
// here to match each one.
for (let i = 0; i < settings.length; i++) {
const matchs = input.match(settings[i].regexp)
Copy link
Member

Choose a reason for hiding this comment

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

typo ? matchs = matches

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeap, IntelliSense 🗡

options = Object.assign({
delimiters: ['{{', '}}'],
unescapeDelimiters: ['{{{', '}}}'],
conditionalTags: ['if', 'elseif', 'else'],
Copy link
Member

@michael-ciniawsky michael-ciniawsky Nov 16, 2016

Choose a reason for hiding this comment

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

Are options for setting the conditional-, loop-, scope tags really necessary? what if someone tries to add e.g options.loopTags = ['each', 'foreach', 'forof', 'forin'...] expecting them to work like the normally do in JS ? :) To reduce eventual confusion and configuration imo, we should maybe consider removing them from options enterily. It's not really a benefit being able to customize them :). But maybe I'm missing something important? cc @voischev @posthtml/collaborators

Copy link
Member Author

@mrmlnc mrmlnc Nov 16, 2016

Choose a reason for hiding this comment

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

In original code (https://github.com/posthtml/posthtml-expressions/blob/master/lib/index.js#L129) uses only the first element for loop & scope.

Copy link
Member

Choose a reason for hiding this comment

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

Ok I see, that's 👍 for dropping I would say :). Fixed tags for logical expressions, matching the JS naming conventions && logic when possible, is a valuable thing. Everyone can still use his/her's favourite delimiter flavour of choice. But not my decision alone, lets wait for input of others :)

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Nov 16, 2016

@mrmlnc Whats your opinion on a <switch> tag, mimicking a switch statement?

<switch>
  <case>
  <case>
  <case>
</switch>

likely this one is total rubbish 😛 , anyways...

<js>Like a script tag but gets executed in Context</js>

@mrmlnc
Copy link
Member Author

mrmlnc commented Nov 16, 2016

@michael-ciniawsky,

I want to talk about it soon. Because all these changes (a needle in a haystack) do not solve the problem, which was announced in #26.

@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Nov 16, 2016

Are there no known faster implementations of merge and clonedeep or at least vanilla ones? 😛

@mrmlnc
Copy link
Member Author

mrmlnc commented Nov 16, 2016

@michael-ciniawsky,

Spoiler: even a simple Object.assign({}, options) kills performance from 500 op/s to 6-10 op/s.

@michael-ciniawsky
Copy link
Member

uhhhh... 😟 I'm an extensive Object.assign user

@mrmlnc
Copy link
Member Author

mrmlnc commented Nov 16, 2016

@michael-ciniawsky,

Ready to merge. Please look again 😄

@michael-ciniawsky michael-ciniawsky merged commit fffa601 into posthtml:master Nov 16, 2016
@michael-ciniawsky
Copy link
Member

michael-ciniawsky commented Nov 16, 2016

@mrmlnc I make few chore/docs tweaks later today and publish new release under posthtml-expressions

This was referenced Nov 16, 2016
delimiters.push('escaped')
unescapeDelimiters.push('unescaped')
function escapeRegexpString (input) {
return input.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
Copy link
Member

Choose a reason for hiding this comment

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

should be use cached variable for RE

Copy link
Member Author

@mrmlnc mrmlnc Nov 18, 2016

Choose a reason for hiding this comment

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

Will be resolved by performance-2

@@ -278,7 +276,7 @@ function executeLoop (loopParams, p1, p2, node) {
*/
function executeScoped (scopedLocals, node) {
// merge nondestructively into existing locals
const scopedOptions = merge(cloneDeep(options), { locals: scopedLocals })
const scopedOptions = merge(cloneDeep(data), { locals: scopedLocals })
Copy link
Member

Choose a reason for hiding this comment

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

🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

globals / globalLocals?

Copy link
Member

Choose a reason for hiding this comment

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

merge/cloneDeep :)

const vm = require('vm')

function escapeHtmlCharacters (unsafe) {
return unsafe.replace(/[&<>"']/g, (match) => ({
Copy link
Member

Choose a reason for hiding this comment

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

Object and RE should be cached ;)

Copy link
Member Author

@mrmlnc mrmlnc Nov 18, 2016

Choose a reason for hiding this comment

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

Will be resolved by performance-2

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.

None yet

4 participants