Skip to content

Commit d180cd3

Browse files
committed
[MERGE #5685 @jackhorton] Remove questionable uses of LiteralString class
Merge pull request #5685 from jackhorton:literal-strings Found this as a driveby while making a different change. To me, `LiteralString::NewCopy*` is an anti-pattern -- LiteralString doesn't override NewCopy*, so it calls the JavascriptString version regardless, and it gives the impression that some optimization was in mind but not achieved. Additionally, by searching for more cases, I found a number of cases where we were wastefully creating JavascriptStrings for various string constants (most were especially brutal since they were going in rarely used ConcatStrings). Not sure if there is value in actually hardening LiteralString against incorrect uses, but I can implement that if others see value.
2 parents 704e088 + 6cf039d commit d180cd3

File tree

6 files changed

+22
-11
lines changed

6 files changed

+22
-11
lines changed

lib/Runtime/Library/BoundFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ namespace Js
267267
displayName = JavascriptString::FromVar(value);
268268
}
269269
}
270-
return LiteralString::Concat(LiteralString::NewCopySz(_u("bound "), this->GetScriptContext()), displayName);
270+
return JavascriptString::Concat(GetLibrary()->GetBoundFunctionPrefixString(), displayName);
271271
}
272272

273273
RecyclableObject* BoundFunction::GetBoundThis()

lib/Runtime/Library/CompoundString.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ namespace Js
12431243
return;
12441244
}
12451245

1246-
JavascriptString *const js = LiteralString::NewCopyBuffer(s, appendCharLength, toString->GetScriptContext());
1246+
JavascriptString *const js = JavascriptString::NewCopyBuffer(s, appendCharLength, toString->GetScriptContext());
12471247
if(TryAppendGeneric(js, appendCharLength, toString))
12481248
return;
12491249
toString->AppendSlow(js);

lib/Runtime/Library/JavascriptFunction.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3185,19 +3185,20 @@ void __cdecl _alloca_probe_16()
31853185
ParseableFunctionInfo * func = this->GetFunctionProxy()->EnsureDeserialized();
31863186
if (func->GetDisplayName() == Js::Constants::FunctionCode)
31873187
{
3188-
return LiteralString::NewCopyBuffer(Js::Constants::Anonymous, Js::Constants::AnonymousLength, scriptContext);
3188+
// TODO(jahorto): multiple places use pointer equality on the Constants:: string buffers. Consider moving these to StringCacheList.h and use backing buffer pointer equality if need be.
3189+
return JavascriptString::NewWithBuffer(Constants::Anonymous, Constants::AnonymousLength, scriptContext);
31893190
}
31903191
else if (func->GetIsAccessor())
31913192
{
31923193
const char16* accessorName = func->GetDisplayName();
31933194
if (accessorName[0] == _u('g'))
31943195
{
3195-
return LiteralString::Concat(LiteralString::NewCopySz(_u("get "), scriptContext), LiteralString::NewCopyBuffer(name, length, scriptContext));
3196+
return JavascriptString::Concat(scriptContext->GetLibrary()->GetGetterFunctionPrefixString(), JavascriptString::NewCopyBuffer(name, length, scriptContext));
31963197
}
31973198
AssertMsg(accessorName[0] == _u('s'), "should be a set");
3198-
return LiteralString::Concat(LiteralString::NewCopySz(_u("set "), scriptContext), LiteralString::NewCopyBuffer(name, length, scriptContext));
3199+
return JavascriptString::Concat(scriptContext->GetLibrary()->GetSetterFunctionPrefixString(), JavascriptString::NewCopyBuffer(name, length, scriptContext));
31993200
}
3200-
return LiteralString::NewCopyBuffer(name, length, scriptContext);
3201+
return JavascriptString::NewCopyBuffer(name, length, scriptContext);
32013202
}
32023203

32033204
bool JavascriptFunction::GetFunctionName(JavascriptString** name) const
@@ -3246,7 +3247,14 @@ void __cdecl _alloca_probe_16()
32463247
if (proxy)
32473248
{
32483249
ParseableFunctionInfo * func = proxy->EnsureDeserialized();
3249-
return LiteralString::NewCopySz(func->GetDisplayName(), scriptContext);
3250+
if (func->GetDisplayNameIsRecyclerAllocated())
3251+
{
3252+
return JavascriptString::NewWithSz(func->GetDisplayName(), scriptContext);
3253+
}
3254+
else
3255+
{
3256+
return JavascriptString::NewCopySz(func->GetDisplayName(), scriptContext);
3257+
}
32503258
}
32513259
JavascriptString* sourceStringName = nullptr;
32523260
if (GetSourceStringName(&sourceStringName))

lib/Runtime/Library/JavascriptLibrary.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,7 +2757,7 @@ namespace Js
27572757

27582758
library->AddMember(functionPrototype, PropertyIds::constructor, library->functionConstructor);
27592759
library->AddMember(functionPrototype, PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable);
2760-
library->AddMember(functionPrototype, PropertyIds::name, LiteralString::CreateEmptyString(scriptContext->GetLibrary()->GetStringTypeStatic()), PropertyConfigurable);
2760+
library->AddMember(functionPrototype, PropertyIds::name, library->GetEmptyString(), PropertyConfigurable);
27612761

27622762
JavascriptFunction *func = library->AddFunctionToLibraryObject(functionPrototype, PropertyIds::apply, &JavascriptFunction::EntryInfo::Apply, 2);
27632763
builtinFuncs[BuiltinFunction::JavascriptFunction_Apply] = func;
@@ -4500,15 +4500,15 @@ namespace Js
45004500

45014501
RuntimeFunction* JavascriptLibrary::CreateGetterFunction(PropertyId nameId, FunctionInfo* functionInfo)
45024502
{
4503-
Var name_withGetPrefix = LiteralString::Concat(LiteralString::NewCopySz(_u("get "), scriptContext), scriptContext->GetPropertyString(nameId));
4503+
Var name_withGetPrefix = JavascriptString::Concat(GetGetterFunctionPrefixString(), scriptContext->GetPropertyString(nameId));
45044504
RuntimeFunction* getterFunction = DefaultCreateFunction(functionInfo, 0, nullptr, nullptr, name_withGetPrefix);
45054505
getterFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(0), PropertyConfigurable, nullptr);
45064506
return getterFunction;
45074507
}
45084508

45094509
RuntimeFunction* JavascriptLibrary::CreateSetterFunction(PropertyId nameId, FunctionInfo* functionInfo)
45104510
{
4511-
Var name_withSetPrefix = LiteralString::Concat(LiteralString::NewCopySz(_u("set "), scriptContext), scriptContext->GetPropertyString(nameId));
4511+
Var name_withSetPrefix = JavascriptString::Concat(GetSetterFunctionPrefixString(), scriptContext->GetPropertyString(nameId));
45124512
RuntimeFunction* setterFunction = DefaultCreateFunction(functionInfo, 0, nullptr, nullptr, name_withSetPrefix);
45134513
setterFunction->SetPropertyWithAttributes(PropertyIds::length, TaggedInt::ToVarUnchecked(1), PropertyConfigurable, nullptr);
45144514
return setterFunction;

lib/Runtime/Library/ScriptFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ using namespace Js;
391391
wmemcpy_s(buffer, decodedCount, builder.DangerousGetWritableBuffer(), decodedCount);
392392
buffer[decodedCount] = 0;
393393

394-
cachedSourceString = LiteralString::New(scriptContext->GetLibrary()->GetStringTypeStatic(), buffer, static_cast<charcount_t>(decodedCount), recycler);
394+
cachedSourceString = JavascriptString::NewWithBuffer(buffer, static_cast<charcount_t>(decodedCount), scriptContext);
395395
}
396396
else
397397
{

lib/Runtime/Library/StringCacheList.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ STRING(ObjectRegExpDisplay, _u("[object RegExp]"))
3737
STRING(ObjectStringDisplay, _u("[object String]"))
3838
STRING(ObjectNullDisplay, _u("[object Null]"))
3939
STRING(ObjectUndefinedDisplay, _u("[object Undefined]"))
40+
STRING(GetterFunctionPrefix, _u("get "))
41+
STRING(SetterFunctionPrefix, _u("set "))
42+
STRING(BoundFunctionPrefix, _u("bound "))
4043
PROPERTY_STRING(UndefinedDisplay, _u("undefined"))
4144
PROPERTY_STRING(NaNDisplay, _u("NaN"))
4245
PROPERTY_STRING(NullDisplay, _u("null"))

0 commit comments

Comments
 (0)