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

improve performance of string.join #822

Merged
merged 2 commits into from
Apr 7, 2025
Merged

Conversation

yoshi-monster
Copy link
Contributor

This is a similar situation to string.concat, however I do not know about a BIF we could use here so the Gleam implementation is actually best.

There is also a FFI version for Javascript, but even that is slower than a pure Gleam solution now, most likely due to the recent improvements to list pattern matching in 1.9.

For the benchmark numbers, I chose to join on , , since I believe a short character join string is the most common case.

Numbers!

# erlang - again string length / list length
Input               Function                       IPS           Min           P99
10 / 10             stdlib string.join     777191.7326        0.0009        0.0015
10 / 10             gleam string.join     1630428.8300        0.0005        0.0011
10 / 100            stdlib string.join     150520.7347        0.0054        0.0150
10 / 100            gleam string.join      244764.1153        0.0036        0.0058
10 / 1000           stdlib string.join      16522.4048        0.0514        0.0925
10 / 1000           gleam string.join       28194.7461        0.0313        0.0702
100 / 10            stdlib string.join     283124.6178        0.0025        0.0086
100 / 10            gleam string.join     1078351.4757        0.0008        0.0011
100 / 100           stdlib string.join      38820.4196        0.0222        0.0331
100 / 100           gleam string.join      213138.1997        0.0040        0.0058
100 / 1000          stdlib string.join       2427.9732        0.3004        0.4007
100 / 1000          gleam string.join       26382.4137        0.0338        0.0469
1000 / 10           stdlib string.join      44855.8251        0.0189        0.0279
1000 / 10           gleam string.join      656004.6761        0.0012        0.0030
1000 / 100          stdlib string.join       3466.0823        0.2569        0.3667
1000 / 100          gleam string.join      115357.6359        0.0070        0.0148
1000 / 1000         stdlib string.join        346.1668        2.6125        3.9133
1000 / 1000         gleam string.join        2725.6218        0.2280        0.5837
# javascript

Input               Function                       IPS           Min           P99
10 / 10             stdlib string.join    1971708.4696        0.0002        0.0004
10 / 10             gleam string.join     2568384.8514        0.0001        0.0003
10 / 100            stdlib string.join     357832.8593        0.0014        0.0030
10 / 100            gleam string.join      462116.0739        0.0012        0.0022
10 / 1000           stdlib string.join      37463.1275        0.0147        0.0318
10 / 1000           gleam string.join       51937.8002        0.0115        0.0211
100 / 10            stdlib string.join    1790550.0451        0.0001        0.0004
100 / 10            gleam string.join      807797.3019        0.0001        0.0002
100 / 100           stdlib string.join     318071.6749        0.0014        0.0031
100 / 100           gleam string.join      428962.5024        0.0012        0.0022
100 / 1000          stdlib string.join      32831.2719        0.0150        0.0294
100 / 1000          gleam string.join       46558.1323        0.0116        0.0210
1000 / 10           stdlib string.join    1667662.0459        0.0001        0.0004
1000 / 10           gleam string.join     2458001.2974        0.0001        0.0002
1000 / 100          stdlib string.join     263659.1989        0.0015        0.0028
1000 / 100          gleam string.join      366360.3739        0.0012        0.0022
1000 / 1000         stdlib string.join      28046.5656        0.0144        0.0302
1000 / 1000         gleam string.join       39795.7678        0.0114        0.0231

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Wonderful! Thank you

@lpil lpil merged commit dd76f0f into gleam-lang:main Apr 7, 2025
7 checks passed
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