Skip to content

Commit d4020de

Browse files
Avoid string copy + free in string_length if the RValue is already a string
1 parent a718075 commit d4020de

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/vm_builtins.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,14 @@ static RValue builtinShowDebugMessage(MAYBE_UNUSED VMContext* ctx, RValue* args,
708708
static RValue builtinStringLength(MAYBE_UNUSED VMContext* ctx, RValue* args, int32_t argCount) {
709709
if (1 > argCount) return RValue_makeInt32(0);
710710
// GML converts non-string arguments to string before measuring length
711-
char* str = RValue_toString(args[0]);
711+
RValue value = args[0];
712+
// Fast path: If the RValue is already a string, just return its length instead of creating a copy
713+
if (value.type == RVALUE_STRING) {
714+
if (value.string == nullptr)
715+
return RValue_makeInt32(0);
716+
return RValue_makeInt32((int32_t) strlen(value.string));
717+
}
718+
char* str = RValue_toString(value);
712719
int32_t len = (int32_t) strlen(str);
713720
free(str);
714721
return RValue_makeInt32(len);

0 commit comments

Comments
 (0)