Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 4f6e9b7

Browse files
[[ Perf ]] Add a variant of MCStringCreateWithNativeCharsAndRelease that is zero-copy
1 parent c1a1a2c commit 4f6e9b7

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

engine/src/exec.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,12 @@ bool MCExecContext::ConvertToString(MCValueRef p_value, MCStringRef& r_string)
9898
t_length = MCU_r8tos(t_buffer, t_buffer_size, MCNumberFetchAsReal((MCNumberRef)p_value), m_nffw, m_nftrailing, m_nfforce);
9999

100100
bool t_success;
101-
t_success = MCStringCreateWithNativeChars((char_t *)t_buffer, t_length, r_string) &&
101+
t_success = MCStringCreateWithNativeCharBufferAndRelease((char_t *)t_buffer, t_length, t_buffer_size, r_string) &&
102102
MCStringSetNumericValue(r_string, MCNumberFetchAsReal((MCNumberRef)p_value));
103+
104+
if (!t_success)
105+
delete[] t_buffer;
103106

104-
delete[] t_buffer;
105107
return t_success;
106108
}
107109
break;

libfoundation/include/foundation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,7 @@ bool MCStringCreateWithWStringAndRelease(unichar_t *wstring, MCStringRef& r_stri
14731473
// Create an immutable string from the given native char sequence.
14741474
bool MCStringCreateWithNativeChars(const char_t *chars, uindex_t char_count, MCStringRef& r_string);
14751475
bool MCStringCreateWithNativeCharsAndRelease(char_t *chars, uindex_t char_count, MCStringRef& r_string);
1476+
bool MCStringCreateWithNativeCharBufferAndRelease(char_t* buffer, uindex_t char_count, uindex_t buffer_length, MCStringRef& r_string);
14761477

14771478
// Create an immutable string from the given (native) c-string.
14781479
bool MCStringCreateWithCString(const char *cstring, MCStringRef& r_string);

libfoundation/src/foundation-string.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,11 @@ bool MCStringCreateWithNativeChars(const char_t *p_chars, uindex_t p_char_count,
567567
}
568568

569569
bool MCStringCreateWithNativeCharsAndRelease(char_t *p_chars, uindex_t p_char_count, MCStringRef& r_string)
570+
{
571+
return MCStringCreateWithNativeCharBufferAndRelease(p_chars, p_char_count, p_char_count, r_string);
572+
}
573+
574+
bool MCStringCreateWithNativeCharBufferAndRelease(char_t* p_chars, uindex_t p_char_count, uindex_t p_buffer_length, MCStringRef& r_string)
570575
{
571576
bool t_success;
572577
t_success = true;
@@ -583,14 +588,19 @@ bool MCStringCreateWithNativeCharsAndRelease(char_t *p_chars, uindex_t p_char_co
583588
if (t_success)
584589
t_success = __MCValueCreate(kMCValueTypeCodeString, self);
585590

586-
if (t_success)
587-
t_success = MCMemoryReallocate(p_chars, p_char_count + 1, p_chars);
588-
591+
uindex_t t_capacity = p_buffer_length;
592+
if (t_success && t_capacity < p_char_count + 1)
593+
{
594+
t_capacity = p_char_count + 1;
595+
t_success = MCMemoryReallocate(p_chars, t_capacity, p_chars);
596+
}
597+
589598
if (t_success)
590599
{
591600
p_chars[p_char_count] = '\0';
592601
self -> native_chars = p_chars;
593602
self -> char_count = p_char_count;
603+
self -> capacity = t_capacity;
594604
r_string = self;
595605
}
596606
else

0 commit comments

Comments
 (0)