Skip to content

modernize loader #1279

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

Merged
merged 9 commits into from
Jun 13, 2020
Merged

modernize loader #1279

merged 9 commits into from
Jun 13, 2020

Conversation

MaxGraey
Copy link
Member

  • use string templates
  • speedup getStringImpl by switching from collecting array parts to direct string concatenations

do {
const last = U16[offset + CHUNKSIZE - 1];
const size = last >= 0xD800 && last < 0xDC00 ? CHUNKSIZE - 1 : CHUNKSIZE;
parts.push(String.fromCharCode.apply(String, U16.subarray(offset, offset += size)));
parts += String.fromCharCode.apply(String, U16.subarray(offset, offset += size));
Copy link
Member

@dcodeIO dcodeIO May 16, 2020

Choose a reason for hiding this comment

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

Are you sure that this is always faster? Would imagine that it is for smaller strings, but once parts grows bigger, it might slow down substantially.

Copy link
Member Author

Choose a reason for hiding this comment

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

On Chrome it should be definitely faster. See this tests:
https://twitter.com/MaxGraey/status/1232045301374111744
https://esbench.com/bench/5e540d53328a52009e8b1b22
It relate to string reversing but it also use same technique as string decoding

Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need to do String.fromCharCode.apply(String) could simply do something like

const fromCharCode = String.fromCharCode;

fromCharCode.apply(null, theArray);

Copy link
Contributor

Choose a reason for hiding this comment

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

Does this affect performance?

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think so

Copy link
Member Author

@MaxGraey MaxGraey May 17, 2020

Choose a reason for hiding this comment

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

@dcodeIO I made micro benchmark which more relate to this case:
https://gist.github.com/MaxGraey/6db5c6c6d7e4a0f45154b73a5e130976

and it seems string concat approach faster in 3 times

Copy link
Member

Choose a reason for hiding this comment

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

While 4K+ is already a reasonably long string, an interesting measurement there would be how long a string has to be for both approaches to execute in an equal time span, if at all. That would help to inform if it is good enough in any case, or if we'd need another code path to deal with strings longer than X.

Copy link
Member

Choose a reason for hiding this comment

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

Also, the test string is exclusively ASCII currently, so it might be that engines optimize this differently than strings that have at least one non-LATIN1 character. Both cases would be interesting.

Copy link

Choose a reason for hiding this comment

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

Based on my benchmarks inside Node, concat is faster than +:
https://github.com/aminya/typescript-optimization#string--vs-concat

@dcodeIO dcodeIO merged commit 2ec7133 into AssemblyScript:master Jun 13, 2020
@dcodeIO
Copy link
Member

dcodeIO commented Jun 13, 2020

Thanks :)

@github-actions
Copy link

🎉 This PR is included in version 0.11.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@MaxGraey MaxGraey deleted the use-templates-in-loader branch June 14, 2020 05:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants