Skip to content

Lua - #301

Open
callumok2004 wants to merge 12 commits into
mainfrom
lua
Open

Lua#301
callumok2004 wants to merge 12 commits into
mainfrom
lua

Conversation

@callumok2004

Copy link
Copy Markdown
Member

No description provided.

Comment on lines +545 to +553
public ReadOnlySpan<char> CheckString(int stackPos, Span<char> buffer) {
int type = lua_type(state, stackPos);
if (type != TSTRING && type != TNUMBER)
TypeError("string", stackPos);
ReadOnlySpan<byte> bytes = GetStringBytes(stackPos);
if (Encoding.UTF8.GetMaxCharCount(bytes.Length) > buffer.Length)
return Encoding.UTF8.GetString(bytes);
return buffer[..Encoding.UTF8.GetChars(bytes, buffer)];
}

@ZehMatt ZehMatt Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is honestly an odd choice to go with, we shouldn't ever assume that what comes from lua is actually valid UTF-8, also having to provide a buffer is strange, this function should just hand out a span of bytes and let the function using it decide what to do with it, for as long the function doesn't go out of scope Lua has no chance to garbage collect the string, quite often the string is not used to permanently store it and only then should it copy that to a new string object or buffer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The old CheckString already decoded as UTF-8 anyways, and the buffer is just a stackalloc to keep things off the heap

This was what I was seeing before, over 5 seconds, as a result of the matproxies for sky, player color etc
image

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes I'm not a fan of the other CheckString variant either, the data is already on heap provided by Lua, majority of the time we pass it along into some function which also quite often already expects Span or Span, the best would be to stick to byte as in Lua a string has no encoding, thats just a binary blob and should be treated as such.

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