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

Make table.concat faster #1243

Merged
merged 4 commits into from Apr 29, 2024
Merged

Make table.concat faster #1243

merged 4 commits into from Apr 29, 2024

Conversation

zeux
Copy link
Collaborator

@zeux zeux commented Apr 28, 2024

table.concat is idiomatic and should be the fastest way to concatenate all table array elements together, but apparently you can beat it by using string.format, string.rep and table.unpack:

string.format(string.rep("%*", #t), table.unpack(t))

... this just won't do, so we should fix table.concat performance.

The deficit comes from two places:

  • rawgeti overhead followed by other stack accesses, all to extract a string from what is almost always an in-bounds array lookup
  • addlstring overhead in case separator is empty (extra function calls)

This change fixes this by using a fast path for in-bounds array lookup for a string. Note that table.concat also supports numbers (these need to be converted to strings which is a little cumbersome and has innate overhead), and out-of-bounds accesses*. In these cases we fall back to the old implementation.

To trigger out-of-bounds accesses, you need to skip the past-array-end element (which is nil per array invariant), but this is achievable because table.concat supports offset+length arguments. This should almost never come up in practice but the per-element branches et al are fairly cheap compared to the eventual string copy/alloc anyway.

This change makes table.concat ~2x faster when the separator is empty; the table.concat benchmark shows +40% gains but it uses a variety of string separators of different lengths so it doesn't get the full benefit from this change.

zeux and others added 4 commits April 27, 2024 18:14
table.concat is idiomatic and should be the fastest way to concatenate
all table array elements together, but apparently you can beat it by
using string.format, string.rep and table.unpack:

	string.format(string.rep("%s", #t), table.unpack(t))

... this just won't do, so we should fix table.concat performance.

The deficit comes from two places:

- rawgeti overhead followed by other stack accesses, all to extract
a string from what is almost always an in-bounds array lookup
- addlstring overhead in case separator is empty (extra function calls)

This change fixes this by using a fast path for in-bounds array lookup
for a string. Note that table.concat also supports numbers (these need
to be converted to strings which is a little cumbersome and has innate
overhead), and out-of-bounds accesses*. In these cases we fall back to
the old implementation.

To trigger out-of-bounds accesses, you need to skip the past-array-end
element (which is nil per array invariant), but this is achievable
because table.concat supports offset+length arguments. This should
almost never come up in practice but the per-element branches et al are
fairly cheap compared to the eventual string copy/alloc anyway.

This change makes table.concat ~2x faster when the separator is empty;
the table.concat benchmark shows +40% gains but it uses a variety of
string separators of different lengths so it doesn't get the full
benefit from this change.
@vegorov-rbx vegorov-rbx merged commit f5303b3 into master Apr 29, 2024
8 checks passed
@vegorov-rbx vegorov-rbx deleted the tconcat-perf branch April 29, 2024 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants