Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant fold string comparisons #1326

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Compiler/src/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <vector>
#include <math.h>

LUAU_FASTFLAGVARIABLE(LuauConstantFoldStringComparisons, false)

namespace Luau
{
namespace Compile
Expand Down Expand Up @@ -72,6 +74,17 @@ static void foldUnary(Constant& result, AstExprUnary::Op op, const Constant& arg
}
}

static int compareStrings(const char* string1, const char* string2, unsigned int size1, unsigned int size2)
{
unsigned int size = std::min(size1, size2);

int res = memcmp(string1, string2, size);
if (res != 0 || size1 == size2)
return res;

return size1 < size2 ? -1 : 1;
}

static void foldBinary(Constant& result, AstExprBinary::Op op, const Constant& la, const Constant& ra)
{
switch (op)
Expand Down Expand Up @@ -157,6 +170,11 @@ static void foldBinary(Constant& result, AstExprBinary::Op op, const Constant& l
result.type = Constant::Type_Boolean;
result.valueBoolean = la.valueNumber < ra.valueNumber;
}
else if (FFlag::LuauConstantFoldStringComparisons && la.type == Constant::Type_String && ra.type == Constant::Type_String)
{
result.type = Constant::Type_Boolean;
result.valueBoolean = compareStrings(la.valueString, ra.valueString, la.stringLength, ra.stringLength) < 0;
}
break;

case AstExprBinary::CompareLe:
Expand All @@ -165,6 +183,11 @@ static void foldBinary(Constant& result, AstExprBinary::Op op, const Constant& l
result.type = Constant::Type_Boolean;
result.valueBoolean = la.valueNumber <= ra.valueNumber;
}
else if (FFlag::LuauConstantFoldStringComparisons && la.type == Constant::Type_String && ra.type == Constant::Type_String)
{
result.type = Constant::Type_Boolean;
result.valueBoolean = compareStrings(la.valueString, ra.valueString, la.stringLength, ra.stringLength) <= 0;
}
break;

case AstExprBinary::CompareGt:
Expand All @@ -173,6 +196,11 @@ static void foldBinary(Constant& result, AstExprBinary::Op op, const Constant& l
result.type = Constant::Type_Boolean;
result.valueBoolean = la.valueNumber > ra.valueNumber;
}
else if (FFlag::LuauConstantFoldStringComparisons && la.type == Constant::Type_String && ra.type == Constant::Type_String)
{
result.type = Constant::Type_Boolean;
result.valueBoolean = compareStrings(la.valueString, ra.valueString, la.stringLength, ra.stringLength) > 0;
}
break;

case AstExprBinary::CompareGe:
Expand All @@ -181,6 +209,11 @@ static void foldBinary(Constant& result, AstExprBinary::Op op, const Constant& l
result.type = Constant::Type_Boolean;
result.valueBoolean = la.valueNumber >= ra.valueNumber;
}
else if (FFlag::LuauConstantFoldStringComparisons && la.type == Constant::Type_String && ra.type == Constant::Type_String)
{
result.type = Constant::Type_Boolean;
result.valueBoolean = compareStrings(la.valueString, ra.valueString, la.stringLength, ra.stringLength) >= 0;
}
break;

case AstExprBinary::And:
Expand Down
Loading