Improved performance when binding by reusing many common scripttype objects
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2019-09-01 15:35:45 +02:00
parent 79873d9d6a
commit a3e77f650a
14 changed files with 141 additions and 113 deletions

View File

@@ -106,7 +106,7 @@ namespace Porygon::Binder {
auto valueExpression = this->BindExpression(s->GetValueExpression());
auto boundIndexType = indexable->GetType();
if (boundIndexType->GetClass() != TypeClass::Error &&
boundIndexType->operator!=(valueExpression->GetType().get())) {
boundIndexType->operator!=(valueExpression->GetType())) {
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTableValueType,
statement->GetStartPosition(), statement->GetLength());
return new BoundBadStatement();
@@ -115,15 +115,15 @@ namespace Porygon::Binder {
return new BoundIndexAssignmentStatement(indexable, valueExpression);
}
std::shared_ptr<ScriptType> ParseTypeIdentifier(const HashedString *s) {
std::shared_ptr<const ScriptType> ParseTypeIdentifier(const HashedString *s) {
auto hash = s->GetHash();
switch (hash) {
case HashedString::ConstHash("number"):
return std::make_shared<NumericScriptType>(false, false);
return NumericScriptType::Unaware;
case HashedString::ConstHash("bool"):
return std::make_shared<ScriptType>(TypeClass::Bool);
return ScriptType::BoolType;
case HashedString::ConstHash("string"):
return std::make_shared<StringScriptType>(false, 0);
return StringScriptType::Dynamic;
default:
if (!UserData::UserDataStorage::HasUserDataType(hash)) {
return nullptr;
@@ -135,7 +135,7 @@ namespace Porygon::Binder {
BoundStatement *Binder::BindFunctionDeclarationStatement(const ParsedStatement *statement) {
auto functionStatement = (ParsedFunctionDeclarationStatement *) statement;
auto parameters = functionStatement->GetParameters();
auto parameterTypes = vector<shared_ptr<ScriptType>>(parameters->size());
auto parameterTypes = vector<shared_ptr<const ScriptType>>(parameters->size());
auto parameterKeys = vector<shared_ptr<const BoundVariableKey>>(parameters->size());
this->_scope->GoInnerScope();
@@ -159,7 +159,7 @@ namespace Porygon::Binder {
}
auto identifier = functionStatement->GetIdentifier();
auto returnType = make_shared<ScriptType>(TypeClass::Nil);
auto returnType = ScriptType::NilType;
auto option = new ScriptFunctionOption(returnType, parameterTypes, parameterKeys);
this->_currentFunction = option;
@@ -211,7 +211,7 @@ namespace Porygon::Binder {
statement->GetLength());
return new BoundBadStatement();
} else if (expression == nullptr) {
currentReturnType = make_shared<ScriptType>(TypeClass::Nil);
currentReturnType = ScriptType::NilType;
return new BoundReturnStatement(nullptr);
}
auto boundExpression = this->BindExpression(expression);
@@ -224,7 +224,7 @@ namespace Porygon::Binder {
}
return new BoundReturnStatement(boundExpression);
}
if (currentReturnType.get()->operator!=(expresionType.get())) {
if (currentReturnType.get()->operator!=(expresionType)) {
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidReturnType,
statement->GetStartPosition(),
statement->GetLength());
@@ -280,7 +280,7 @@ namespace Porygon::Binder {
}
this->_scope->GoInnerScope();
auto variableKey = this->_scope->CreateExplicitLocal(identifier, make_shared<NumericScriptType>(true, false));
auto variableKey = this->_scope->CreateExplicitLocal(identifier, NumericScriptType::AwareInt);
if (variableKey.GetResult() != VariableAssignmentResult::Ok) {
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::CantAssignVariable,
statement->GetStartPosition(),
@@ -424,19 +424,18 @@ namespace Porygon::Binder {
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Addition,
std::make_shared<NumericScriptType>(true,
NumericScriptType::ResolveType(true,
leftNumeric->IsFloat() ||
rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
} else {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Addition,
std::make_shared<NumericScriptType>(false, false),
NumericScriptType::Unaware,
expression->GetStartPosition(), expression->GetLength());
}
} else if (boundLeftType->GetClass() == TypeClass::String) {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Concatenation,
std::make_shared<StringScriptType>(false,
0),
StringScriptType::Dynamic,
expression->GetStartPosition(), expression->GetLength());
}
break;
@@ -447,13 +446,13 @@ namespace Porygon::Binder {
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Subtraction,
std::make_shared<NumericScriptType>(true,
NumericScriptType::ResolveType(true,
leftNumeric->IsFloat() ||
rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
} else {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Subtraction,
std::make_shared<NumericScriptType>(false, false),
NumericScriptType::Unaware,
expression->GetStartPosition(), expression->GetLength());
}
}
@@ -465,13 +464,13 @@ namespace Porygon::Binder {
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Multiplication,
std::make_shared<NumericScriptType>(true,
NumericScriptType::ResolveType(true,
leftNumeric->IsFloat() ||
rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
} else {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Multiplication,
std::make_shared<NumericScriptType>(false, false),
NumericScriptType::Unaware,
expression->GetStartPosition(), expression->GetLength());
}
}
@@ -483,63 +482,63 @@ namespace Porygon::Binder {
if (leftNumeric->IsAwareOfFloat() && rightNumeric->IsAwareOfFloat()) {
return new BoundBinaryExpression(boundLeft, boundRight,
BoundBinaryOperation::Division,
std::make_shared<NumericScriptType>(true,
NumericScriptType::ResolveType(true,
leftNumeric->IsFloat() ||
rightNumeric->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
} else {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Division,
std::make_shared<NumericScriptType>(false, false),
NumericScriptType::Unaware,
expression->GetStartPosition(), expression->GetLength());
}
}
break;
case BinaryOperatorKind::Equality:
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Equality,
std::make_shared<ScriptType>(TypeClass::Bool),
ScriptType::BoolType,
expression->GetStartPosition(), expression->GetLength());
case BinaryOperatorKind::Inequality:
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::Inequality,
std::make_shared<ScriptType>(TypeClass::Bool),
ScriptType::BoolType,
expression->GetStartPosition(), expression->GetLength());
case BinaryOperatorKind::Less:
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
boundRight->GetType()->GetClass() == TypeClass::Number) {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LessThan,
std::make_shared<ScriptType>(TypeClass::Bool),
ScriptType::BoolType,
expression->GetStartPosition(), expression->GetLength());
}
case BinaryOperatorKind::LessOrEquals:
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
boundRight->GetType()->GetClass() == TypeClass::Number) {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LessThanEquals,
std::make_shared<ScriptType>(TypeClass::Bool),
ScriptType::BoolType,
expression->GetStartPosition(), expression->GetLength());
}
case BinaryOperatorKind::Greater:
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
boundRight->GetType()->GetClass() == TypeClass::Number) {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::GreaterThan,
std::make_shared<ScriptType>(TypeClass::Bool),
ScriptType::BoolType,
expression->GetStartPosition(), expression->GetLength());
}
case BinaryOperatorKind::GreaterOrEquals:
if (boundLeft->GetType()->GetClass() == TypeClass::Number &&
boundRight->GetType()->GetClass() == TypeClass::Number) {
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::GreaterThanEquals,
std::make_shared<ScriptType>(TypeClass::Bool),
ScriptType::BoolType,
expression->GetStartPosition(), expression->GetLength());
}
case BinaryOperatorKind::LogicalAnd:
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalAnd,
std::make_shared<ScriptType>(TypeClass::Bool),
ScriptType::BoolType,
expression->GetStartPosition(), expression->GetLength());
break;
case BinaryOperatorKind::LogicalOr:
if (boundLeftType->GetClass() == TypeClass::Bool && boundRightType->GetClass() == TypeClass::Bool)
return new BoundBinaryExpression(boundLeft, boundRight, BoundBinaryOperation::LogicalOr,
std::make_shared<ScriptType>(TypeClass::Bool),
ScriptType::BoolType,
expression->GetStartPosition(), expression->GetLength());
break;
}
@@ -563,7 +562,7 @@ namespace Porygon::Binder {
if (operandType->GetClass() == TypeClass::Number) {
auto innerType = std::dynamic_pointer_cast<const NumericScriptType>(operandType);
return new BoundUnaryExpression(operand, BoundUnaryOperation::Negation,
std::make_shared<NumericScriptType>(
NumericScriptType::ResolveType(
innerType.get()->IsAwareOfFloat(),
innerType.get()->IsFloat()),
expression->GetStartPosition(), expression->GetLength());
@@ -572,7 +571,7 @@ namespace Porygon::Binder {
case UnaryOperatorKind::LogicalNegation:
if (operandType->GetClass() == TypeClass::Bool) {
return new BoundUnaryExpression(operand, BoundUnaryOperation::LogicalNegation,
std::make_shared<ScriptType>(TypeClass::Bool),
ScriptType::BoolType,
expression->GetStartPosition(), expression->GetLength());
}
break;
@@ -778,7 +777,7 @@ namespace Porygon::Binder {
valueType = boundExpressions[0]->GetType();
for (size_t i = 1; i < expressions->size(); i++) {
boundExpressions[i] = this->BindExpression(expressions->at(i));
if (boundExpressions[i]->GetType().get()->operator!=(valueType.get())) {
if (boundExpressions[i]->GetType()->operator!=(valueType)) {
this->_scriptData->Diagnostics->LogError(Diagnostics::DiagnosticCode::InvalidTableValueType,
boundExpressions[i]->GetStartPosition(),
boundExpressions[i]->GetLength());
@@ -786,7 +785,7 @@ namespace Porygon::Binder {
}
}
if (valueType == nullptr) {
valueType = std::make_shared<const ScriptType>(TypeClass::Nil);
valueType = ScriptType::NilType;
}
auto tableType = std::make_shared<const NumericalTableScriptType>(valueType);
return new BoundNumericalTableExpression(boundExpressions, tableType, expression->GetStartPosition(),