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

Better string interpolation #1161

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mhermier
Copy link
Contributor

@mhermier mhermier commented Mar 23, 2023

Currently string interpolation rely on 2 mechanisms:

  • String.+(_) aka string concatenation.
  • Creating a List of string and perform List.join() on it.

First patch optimize string concatenation, by taking advantage of string immutability. So adding an empty string can trivially optimized by returning the other value.

string_interpolation - wren    .......... 2.04s 0.0834 130.18% relative to baseline
string_interpolation_gc - wren .......... 2.93s 0.0724 120.93% relative to baseline

It is quite important since the compiler insert them when parsing interpolation strings values happen at the begin ("%(a)..."), in the middle ("...%(a)%(b)...") or at the end ("...%(a)").

The second patch eliminates spurious empty literal string concatenation, and the need of the helper List object. It is obsolete the first patch but it is remains an interesting change to have I think.

string_interpolation - wren    .......... 0.43s 0.0049 612.68% relative to baseline
string_interpolation_gc - wren .......... 1.34s 0.0149 264.72% relative to baseline

Second patch technically produce a little bit more code, but I think in general case interpolated values have a lot of spurious empty strings that are eliminated, reducing code size to a similar size. In addition the removal of the helper List shows it has a significant impact in the benchmark.

@PureFox48
Copy link
Contributor

Can't say I'd ever thought much about the efficiency of string interpolation before but, if can be improved as much as that by making a few simple changes, we might as well have it.

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