Skip to content

Commit 8a4df57

Browse files
committed
[MERGE #5494 @sethbrenith] change GetSz to GetString where it's easy to
Merge pull request #5494 from sethbrenith:user/sethb/getsz I recently noticed a website spending 0.5% of main thread time copying the contents of substrings during decodeURIComponent. There is no reason to copy the string data here, as URIHelper::Decode already respects the string length and does not require zero termination. This change updates callers of GetSz to use GetString instead, in any cases where I could very clearly see that it is safe to do so.
2 parents 17a9de8 + 781fd67 commit 8a4df57

File tree

9 files changed

+56
-56
lines changed

9 files changed

+56
-56
lines changed

lib/Runtime/Debug/TTActionEvents.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ namespace TTD
202202
Js::Var message = InflateVarInReplay(executeContext, GetVarItem_0(errorData));
203203
TTD_REPLAY_VALIDATE_INCOMING_REFERENCE(message, ctx);
204204

205-
*res = nullptr;
205+
*res = nullptr;
206206
switch(eventKind)
207207
{
208208
case EventKind::CreateErrorActionTag:
@@ -1067,7 +1067,7 @@ namespace TTD
10671067
if(Js::JavascriptFunction::Is(funcVar))
10681068
{
10691069
Js::JavascriptString* displayName = Js::JavascriptFunction::FromVar(funcVar)->GetDisplayName();
1070-
alloc.CopyStringIntoWLength(displayName->GetSz(), displayName->GetLength(), cfAction->FunctionName);
1070+
alloc.CopyStringIntoWLength(displayName->GetString(), displayName->GetLength(), cfAction->FunctionName);
10711071
}
10721072
else
10731073
{

lib/Runtime/Debug/TTEventLog.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ namespace TTD
201201

202202
for(TTEventListLink* curr = this->m_headBlock; curr != nullptr; curr = curr->Previous)
203203
{
204-
size_t cpos = curr->StartPos;
204+
size_t cpos = curr->StartPos;
205205
while(cpos != curr->CurrPos)
206206
{
207207
count++;
@@ -814,7 +814,7 @@ namespace TTD
814814
void EventLog::RecordTelemetryLogEvent(Js::JavascriptString* infoStringJs, bool doPrint)
815815
{
816816
NSLogEvents::TelemetryEventLogEntry* tEvent = this->RecordGetInitializedEvent_DataOnly<NSLogEvents::TelemetryEventLogEntry, NSLogEvents::EventKind::TelemetryLogTag>();
817-
this->m_eventSlabAllocator.CopyStringIntoWLength(infoStringJs->GetSz(), infoStringJs->GetLength(), tEvent->InfoString);
817+
this->m_eventSlabAllocator.CopyStringIntoWLength(infoStringJs->GetString(), infoStringJs->GetLength(), tEvent->InfoString);
818818
tEvent->DoPrint = doPrint;
819819

820820
#if ENABLE_BASIC_TRACE || ENABLE_FULL_BC_TRACE
@@ -864,7 +864,7 @@ namespace TTD
864864
this->RecordGetInitializedEvent_DataOnly<NSLogEvents::ExplicitLogWriteEventLogEntry, NSLogEvents::EventKind::ExplicitLogWriteTag>();
865865

866866
AutoArrayPtr<char> uri(HeapNewArrayZ(char, uriString->GetLength() * 3), uriString->GetLength() * 3);
867-
size_t uriLength = utf8::EncodeInto((LPUTF8)((char*)uri), uriString->GetSz(), uriString->GetLength());
867+
size_t uriLength = utf8::EncodeInto((LPUTF8)((char*)uri), uriString->GetString(), uriString->GetLength());
868868

869869
this->EmitLog(uri, uriLength);
870870
}
@@ -901,7 +901,7 @@ namespace TTD
901901
void EventLog::RecordDateStringEvent(Js::JavascriptString* stringValue)
902902
{
903903
NSLogEvents::StringValueEventLogEntry* sEvent = this->RecordGetInitializedEvent_DataOnly<NSLogEvents::StringValueEventLogEntry, NSLogEvents::EventKind::StringTag>();
904-
this->m_eventSlabAllocator.CopyStringIntoWLength(stringValue->GetSz(), stringValue->GetLength(), sEvent->StringValue);
904+
this->m_eventSlabAllocator.CopyStringIntoWLength(stringValue->GetString(), stringValue->GetLength(), sEvent->StringValue);
905905
}
906906

907907
void EventLog::ReplayDateTimeEvent(double* result)
@@ -949,12 +949,12 @@ namespace TTD
949949
#if ENABLE_TTD_INTERNAL_DIAGNOSTICS
950950
if(returnCode)
951951
{
952-
this->m_eventSlabAllocator.CopyStringIntoWLength(propertyName->GetSz(), propertyName->GetLength(), peEvent->PropertyString);
952+
this->m_eventSlabAllocator.CopyStringIntoWLength(propertyName->GetString(), propertyName->GetLength(), peEvent->PropertyString);
953953
}
954954
#else
955955
if(returnCode && pid == Js::Constants::NoProperty)
956956
{
957-
this->m_eventSlabAllocator.CopyStringIntoWLength(propertyName->GetSz(), propertyName->GetLength(), peEvent->PropertyString);
957+
this->m_eventSlabAllocator.CopyStringIntoWLength(propertyName->GetString(), propertyName->GetLength(), peEvent->PropertyString);
958958
}
959959
#endif
960960

@@ -1335,7 +1335,7 @@ namespace TTD
13351335
}
13361336

13371337
//Now allocate the arrays for them and do the processing
1338-
snapEvent->LongLivedRefRootsIdArray = nullptr;
1338+
snapEvent->LongLivedRefRootsIdArray = nullptr;
13391339
if(snapEvent->LongLivedRefRootsCount != 0)
13401340
{
13411341
snapEvent->LongLivedRefRootsIdArray = this->m_eventSlabAllocator.SlabAllocateArray<TTD_LOG_PTR_ID>(snapEvent->LongLivedRefRootsCount);
@@ -2131,7 +2131,7 @@ namespace TTD
21312131
NSLogEvents::EventLogEntry* evt = this->RecordGetInitializedEvent<NSLogEvents::JsRTByteBufferAction, NSLogEvents::EventKind::AllocateExternalArrayBufferActionTag>(&cAction);
21322132
cAction->Length = size;
21332133

2134-
cAction->Buffer = nullptr;
2134+
cAction->Buffer = nullptr;
21352135
if(cAction->Length != 0)
21362136
{
21372137
cAction->Buffer = this->m_eventSlabAllocator.SlabAllocateArray<byte>(cAction->Length);

lib/Runtime/Debug/TTEvents.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ namespace TTD
433433
ExternalCallEventLogEntry* callEvt = GetInlineEventDataAs<ExternalCallEventLogEntry, EventKind::ExternalCallTag>(evt);
434434

435435
Js::JavascriptString* displayName = function->GetDisplayName();
436-
alloc.CopyStringIntoWLength(displayName->GetSz(), displayName->GetLength(), callEvt->FunctionName);
436+
alloc.CopyStringIntoWLength(displayName->GetString(), displayName->GetLength(), callEvt->FunctionName);
437437
}
438438
#endif
439439

lib/Runtime/Debug/TTSerialize.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,7 @@ namespace TTD
802802

803803
if(!endFound)
804804
{
805-
// no ending found
805+
// no ending found
806806
return NSTokens::ParseTokenKind::Error;
807807
}
808808

@@ -966,7 +966,7 @@ namespace TTD
966966

967967
if(!endFound)
968968
{
969-
// no ending found
969+
// no ending found
970970
return NSTokens::ParseTokenKind::Error;
971971
}
972972

@@ -1882,7 +1882,7 @@ namespace TTD
18821882
this->AppendLiteral(", attrib: ");
18831883
this->AppendInteger(attrib);
18841884
this->AppendLiteral(", name: ");
1885-
this->AppendText(pname->GetSz(), (uint32)pname->GetLength());
1885+
this->AppendText(pname->GetString(), (uint32)pname->GetLength());
18861886
}
18871887

18881888
this->AppendLiteral(")\n");
@@ -1926,11 +1926,11 @@ namespace TTD
19261926
{
19271927
if(Js::JavascriptString::FromVar(var)->GetLength() <= 40)
19281928
{
1929-
this->AppendText(Js::JavascriptString::FromVar(var)->GetSz(), Js::JavascriptString::FromVar(var)->GetLength());
1929+
this->AppendText(Js::JavascriptString::FromVar(var)->GetString(), Js::JavascriptString::FromVar(var)->GetLength());
19301930
}
19311931
else
19321932
{
1933-
this->AppendText(Js::JavascriptString::FromVar(var)->GetSz(), 40);
1933+
this->AppendText(Js::JavascriptString::FromVar(var)->GetString(), 40);
19341934
this->AppendLiteral("...");
19351935
this->AppendInteger(Js::JavascriptString::FromVar(var)->GetLength());
19361936
}
@@ -1983,7 +1983,7 @@ namespace TTD
19831983
Js::JavascriptString* displayName = function->GetDisplayName();
19841984

19851985
this->AppendIndent();
1986-
const char16* nameStr = displayName->GetSz();
1986+
const char16* nameStr = displayName->GetString();
19871987
uint32 nameLength = displayName->GetLength();
19881988
this->AppendText(nameStr, nameLength);
19891989

@@ -2024,7 +2024,7 @@ namespace TTD
20242024

20252025
this->AppendIndent();
20262026
this->AppendLiteral("return(");
2027-
this->AppendText(displayName->GetSz(), displayName->GetLength());
2027+
this->AppendText(displayName->GetString(), displayName->GetLength());
20282028
this->AppendLiteral(") -> ");
20292029
this->WriteVar(res);
20302030

@@ -2042,7 +2042,7 @@ namespace TTD
20422042

20432043
this->AppendIndent();
20442044
this->AppendLiteral("return(");
2045-
this->AppendText(displayName->GetSz(), displayName->GetLength());
2045+
this->AppendText(displayName->GetString(), displayName->GetLength());
20462046
this->AppendLiteral(") -> !!exception");
20472047

20482048
this->AppendLiteral(" @ ");
@@ -2059,7 +2059,7 @@ namespace TTD
20592059
this->m_currLength += sprintf_s(this->m_buffer + this->m_currLength, 128, "(l:%I32u, c:%I32u)\n", line + 1, column);
20602060

20612061
////
2062-
//Temp debugging help if needed
2062+
//Temp debugging help if needed
20632063
this->ForceFlush();
20642064
//
20652065
////

lib/Runtime/Debug/TTSnapValues.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ namespace TTD
345345
break;
346346
case Js::TypeIds_String:
347347
snapValue->u_stringValue = alloc.SlabAllocateStruct<TTString>();
348-
alloc.CopyStringIntoWLength(Js::JavascriptString::FromVar(jsValue)->GetSz(), Js::JavascriptString::FromVar(jsValue)->GetLength(), *(snapValue->u_stringValue));
348+
alloc.CopyStringIntoWLength(Js::JavascriptString::FromVar(jsValue)->GetString(), Js::JavascriptString::FromVar(jsValue)->GetLength(), *(snapValue->u_stringValue));
349349
break;
350350
case Js::TypeIds_Symbol:
351351
snapValue->u_propertyIdValue = jslib->ExtractPrimitveSymbolId_TTD(jsValue);
@@ -1902,7 +1902,7 @@ namespace TTD
19021902
reader->ReadRecordEnd();
19031903
}
19041904

1905-
#if ENABLE_SNAPSHOT_COMPARE
1905+
#if ENABLE_SNAPSHOT_COMPARE
19061906
void AssertSnapEquiv(const SnapContext* snapCtx1, const SnapContext* snapCtx2, const JsUtil::BaseDictionary<TTD_LOG_PTR_ID, NSSnapValues::SnapRootInfoEntry*, HeapAllocator>& allRootMap1, const JsUtil::BaseDictionary<TTD_LOG_PTR_ID, NSSnapValues::SnapRootInfoEntry*, HeapAllocator>& allRootMap2, TTDCompareMap& compareMap)
19071907
{
19081908
compareMap.DiagnosticAssert(snapCtx1->ScriptContextLogId == snapCtx2->ScriptContextLogId);

lib/Runtime/Library/ArgumentsObject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Js
2626
stringBuilder->AppendCppLiteral(_u("Object, (Arguments)"));
2727
return TRUE;
2828
}
29-
29+
3030
bool ArgumentsObject::Is(Var aValue)
3131
{
3232
return JavascriptOperators::GetTypeId(aValue) == TypeIds_Arguments;
@@ -402,7 +402,7 @@ namespace Js
402402

403403
BOOL HeapArgumentsObject::SetProperty(JavascriptString* propertyNameString, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
404404
{
405-
AssertMsg(!PropertyRecord::IsPropertyNameNumeric(propertyNameString->GetSz(), propertyNameString->GetLength()),
405+
AssertMsg(!PropertyRecord::IsPropertyNameNumeric(propertyNameString->GetString(), propertyNameString->GetLength()),
406406
"Numeric property names should have been converted to uint or PropertyRecord*");
407407

408408
// TODO: In strict mode, "callee" and "caller" cannot be set.

lib/Runtime/Library/JavascriptLibrary.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5218,7 +5218,7 @@ namespace Js
52185218
str = JavascriptString::FromVar(var);
52195219

52205220
charcount_t len = str->GetLength();
5221-
js_wmemcpy_s(ptr, remainingSpace, str->GetSz(), len);
5221+
js_wmemcpy_s(ptr, remainingSpace, str->GetString(), len);
52225222
ptr += len;
52235223
remainingSpace -= len;
52245224

@@ -5234,7 +5234,7 @@ namespace Js
52345234
str = JavascriptString::FromVar(var);
52355235

52365236
len = str->GetLength();
5237-
js_wmemcpy_s(ptr, remainingSpace, str->GetSz(), len);
5237+
js_wmemcpy_s(ptr, remainingSpace, str->GetString(), len);
52385238
ptr += len;
52395239
remainingSpace -= len;
52405240
}
@@ -5300,7 +5300,7 @@ namespace Js
53005300
}
53015301

53025302
// If the strings at this index are not equal, the callsite objects are not equal.
5303-
if (!JsUtil::CharacterBuffer<char16>::StaticEquals(pid->Psz(), str->GetSz(), str->GetLength()))
5303+
if (!JsUtil::CharacterBuffer<char16>::StaticEquals(pid->Psz(), str->GetString(), str->GetLength()))
53045304
{
53055305
return false;
53065306
}
@@ -5324,7 +5324,7 @@ namespace Js
53245324
}
53255325

53265326
// If the strings at this index are not equal, the callsite objects are not equal.
5327-
if (!JsUtil::CharacterBuffer<char16>::StaticEquals(pid->Psz(), str->GetSz(), str->GetLength()))
5327+
if (!JsUtil::CharacterBuffer<char16>::StaticEquals(pid->Psz(), str->GetString(), str->GetLength()))
53285328
{
53295329
return false;
53305330
}
@@ -6181,20 +6181,20 @@ namespace Js
61816181
JavascriptPromiseThenFinallyFunction* JavascriptLibrary::CreatePromiseThenFinallyFunction(JavascriptMethod entryPoint, RecyclableObject* OnFinally, RecyclableObject* Constructor, bool shouldThrow)
61826182
{
61836183
Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
6184-
6184+
61856185
FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
61866186
DynamicType* type = DynamicType::New(scriptContext, TypeIds_Function, functionPrototype, entryPoint, GetDeferredAnonymousFunctionTypeHandler());
61876187

61886188
JavascriptPromiseThenFinallyFunction* function = RecyclerNewEnumClass(this->GetRecycler(), EnumFunctionClass, JavascriptPromiseThenFinallyFunction, type, functionInfo, OnFinally, Constructor, shouldThrow);
61896189
function->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable, nullptr);
6190-
6190+
61916191
return function;
61926192
}
61936193

61946194
JavascriptPromiseThunkFinallyFunction* JavascriptLibrary::CreatePromiseThunkFinallyFunction(JavascriptMethod entryPoint, Var value, bool shouldThrow)
61956195
{
61966196
Assert(scriptContext->GetConfig()->IsES6PromiseEnabled());
6197-
6197+
61986198
FunctionInfo* functionInfo = RecyclerNew(this->GetRecycler(), FunctionInfo, entryPoint);
61996199
DynamicType* type = CreateDeferredPrototypeFunctionType(entryPoint);
62006200

lib/Runtime/Library/JavascriptString.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,15 +2237,15 @@ namespace Js
22372237

22382238
ScriptContext* scriptContext = pThis->type->GetScriptContext();
22392239
ApiError error = ApiError::NoError;
2240-
const char16 *pThisSz = pThis->GetSz();
22412240
charcount_t pThisLength = pThis->GetLength();
22422241

22432242
if (useInvariant)
22442243
{
2244+
const char16 *pThisString = pThis->GetString();
22452245
bool isAscii = true;
22462246
for (charcount_t i = 0; i < pThisLength; i++)
22472247
{
2248-
if (pThisSz[i] >= 0x80)
2248+
if (pThisString[i] >= 0x80)
22492249
{
22502250
isAscii = false;
22512251
break;
@@ -2258,7 +2258,7 @@ namespace Js
22582258
const char16 diffBetweenCases = 32;
22592259
for (charcount_t i = 0; i < pThisLength; i++)
22602260
{
2261-
char16 cur = pThisSz[i];
2261+
char16 cur = pThisString[i];
22622262
if (toUpper)
22632263
{
22642264
if (cur >= _u('a') && cur <= _u('z'))

0 commit comments

Comments
 (0)