Skip to content

Avoid a redundant MOVE on some string interpolation setups#2324

Open
9382 wants to merge 4 commits intoluau-lang:masterfrom
9382:string-interp
Open

Avoid a redundant MOVE on some string interpolation setups#2324
9382 wants to merge 4 commits intoluau-lang:masterfrom
9382:string-interp

Conversation

@9382
Copy link
Copy Markdown

@9382 9382 commented Apr 3, 2026

This fixes a discrepancy between string interpolation (local a = `{xyz}`) and the manually written equivilant version (local a = ("%*"):format(xyz)), where string interpolation would sometimes introduce a MOVE even when it didn't need to as the target register was marked as temporary

Bytecode difference

Code: local f = `{global}`

Before:

Function 0 (??):
    1: local f = `{global}`
LOADK R1 K0 ['%*']
GETGLOBAL R3 K1 ['global']
NAMECALL R1 R1 K2 ['format']
CALL R1 2 1
MOVE R0 R1
RETURN R0 0

After:

Function 0 (??):
    1: local f = `{global}`
LOADK R0 K0 ['%*']
GETGLOBAL R2 K1 ['global']
NAMECALL R0 R0 K2 ['format']
CALL R0 2 1
RETURN R0 0

This is also technically a performance improvement, though the difference is mostly unnoticeable in normal contexts (I'm seeing a 4-5% improvement on a 1e7 loop which is nothing but the optimisable string interp, so about as good as the case gets)

I've not written a new test case as InterpStringRegisterCleanup already covers the unoptimisable case

This keeps the generated bytecode of string interpolation in line with the result of writing the interpolation manually (e.g. `("%*"):format(xyz)`)

Also means string interpolation may sometimes be faster and use less bytecode, though the improvement is minimal in practice
Copy link
Copy Markdown
Contributor

@tommyscholly tommyscholly left a comment

Choose a reason for hiding this comment

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

This looks good, great catch on the redundant move. Just need the changes to be flagged.

@9382
Copy link
Copy Markdown
Author

9382 commented Apr 6, 2026

(Merged with master to resolve a conflict in Compiler, hopefully it isn't too much of a mess git-wise)

Copy link
Copy Markdown
Contributor

@SPY SPY left a comment

Choose a reason for hiding this comment

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

LGTM

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.

4 participants