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

Optimise encodeInto reallocations #1414

Merged
merged 1 commit into from
Apr 2, 2019

Conversation

RReverser
Copy link
Member

Instead of doubling the size on each iteration, use precise upper limit (3 * JS length) if the string turned out not to be ASCII-only. This results in maximum of 1 reallocation instead of O(log N).

Some dummy examples of what this would change:

  • 1000 of ASCII chars: no change, allocates 1000 bytes and bails out.
  • 1000 ASCII chars + 1 '😃': before allocated 1000 bytes and reallocated to 2000; now allocates 1000 bytes and reallocates to 1006.
  • 1000 of '😃' chars: before allocated 1000 bytes, reallocated to 2000, finally reallocated again to 4000; now allocates 1000 bytes and reallocates to 4000 right away.

Related issue: #1313

r? @alexcrichton @hsivonen

Instead of doubling the size on each iteration, use precise upper limit (3 * JS length) if the string turned out not to be ASCII-only. This results in maximum of 1 reallocation instead of O(log N).

Some dummy examples of what this would change:
 - 1000 of ASCII chars: no change, allocates 1000 bytes and bails out.
 - 1000 ASCII chars + 1 '😃': before allocated 1000 bytes and reallocated to 2000; now allocates 1000 bytes and reallocates to 1006.
 - 1000 of '😃' chars: before allocated 1000 bytes, reallocated to 2000, finally reallocated again to 4000; now allocates 1000 bytes and reallocates to 4000 right away.

Related issue: rustwasm#1313
@RReverser
Copy link
Member Author

This seems to pass all tests (the failures, as usual, are related to deployment scripts and not code changes).

@alexcrichton can we merge this as-is for now? This might be not the final version of optimisations, but it already significantly reduces costs of encodeInto in mixed ASCII & Unicode scenarios.

@alexcrichton alexcrichton merged commit a1c457b into rustwasm:master Apr 2, 2019
@alexcrichton
Copy link
Contributor

Looks good to me!

@RReverser RReverser deleted the smaller-realloc branch April 2, 2019 15:41
@RReverser
Copy link
Member Author

Thanks!

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.

2 participants