From 1cb65f17c96ea49c9a5c2bca30efa35ddc70331b Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 13 Jun 2019 16:26:10 +0200 Subject: [PATCH] Work on performance improvements --- src/Binder/Binder.cpp | 4 +-- src/Binder/BoundStatements/BoundStatement.hpp | 11 ++++--- src/Binder/BoundVariables/BoundScope.cpp | 5 --- src/Binder/BoundVariables/BoundScope.hpp | 5 ++- .../BoundVariables/BoundVariableKey.hpp | 32 +++++++++---------- src/Evaluator/EvalValues/EvalValue.cpp | 6 +--- src/Evaluator/EvalValues/EvalValue.hpp | 19 ++++++----- src/Evaluator/EvalValues/NumericEvalValue.cpp | 2 +- src/Evaluator/EvalValues/NumericEvalValue.hpp | 9 ++---- .../EvalValues/ScriptFunctionEvalValue.hpp | 8 +++-- src/Evaluator/EvalValues/StringEvalValue.cpp | 2 -- src/Evaluator/EvalValues/StringEvalValue.hpp | 11 +++---- src/Evaluator/EvalValues/TableEvalValue.cpp | 2 -- src/Evaluator/EvalValues/TableEvalValue.hpp | 13 +++----- .../EvaluationScope/EvaluationScope.cpp | 4 +-- src/Evaluator/Evaluator.cpp | 18 ++++------- src/Script.cpp | 2 +- src/TableScriptType.hpp | 19 +++++------ tests/integration/Functions.cpp | 20 ++++++------ 19 files changed, 84 insertions(+), 108 deletions(-) delete mode 100644 src/Evaluator/EvalValues/StringEvalValue.cpp delete mode 100644 src/Evaluator/EvalValues/TableEvalValue.cpp diff --git a/src/Binder/Binder.cpp b/src/Binder/Binder.cpp index 7c72eea..4cad000 100644 --- a/src/Binder/Binder.cpp +++ b/src/Binder/Binder.cpp @@ -13,7 +13,7 @@ BoundScriptStatement *Binder::Bind(Script* script, ParsedScriptStatement *s, Bou for (int i = 0; i < statements->size(); i++){ boundStatements[i] = binder.BindStatement(statements->at(i)); } - return new BoundScriptStatement(boundStatements, scriptScope->GetDeepestScope()); + return new BoundScriptStatement(boundStatements, scriptScope->GetLocalVariableCount()); } Binder::~Binder() { @@ -434,7 +434,7 @@ BoundExpression *Binder::BindTableExpression(ParsedTableExpression *expression) auto block = this -> BindBlockStatement(expression -> GetBlock()); this -> _scope = currentScope; - auto tableType = shared_ptr(new TableScriptType(tableScope, innerScope->GetDeepestScope())); + auto tableType = shared_ptr(new TableScriptType(tableScope, innerScope->GetLocalVariableCount())); delete innerScope; return new BoundTableExpression((BoundBlockStatement*)block, tableType, expression->GetStartPosition(), expression->GetLength()); diff --git a/src/Binder/BoundStatements/BoundStatement.hpp b/src/Binder/BoundStatements/BoundStatement.hpp index ef2dc55..1109cdd 100644 --- a/src/Binder/BoundStatements/BoundStatement.hpp +++ b/src/Binder/BoundStatements/BoundStatement.hpp @@ -57,18 +57,19 @@ public: }; class BoundScriptStatement : public BoundBlockStatement{ - int _deepestScope; + int _localVariableCount; public: - explicit BoundScriptStatement(vector statements, int deepestScope) : BoundBlockStatement(std::move(statements)){ - _deepestScope = deepestScope; + explicit BoundScriptStatement(vector statements, int localVariableCount) + : BoundBlockStatement(std::move(statements)){ + _localVariableCount = localVariableCount; } BoundStatementKind GetKind() final{ return BoundStatementKind ::Script; } - int GetDeepestScope(){ - return _deepestScope; + int GetLocalVariableCount(){ + return _localVariableCount; } }; diff --git a/src/Binder/BoundVariables/BoundScope.cpp b/src/Binder/BoundVariables/BoundScope.cpp index 155a632..1a9c1fa 100644 --- a/src/Binder/BoundVariables/BoundScope.cpp +++ b/src/Binder/BoundVariables/BoundScope.cpp @@ -6,7 +6,6 @@ BoundScope::BoundScope(unordered_map *tableScope) { _tableScope = tableScope; _currentScope = 1; - _deepestScope = 1; _lastCreatedScope = 1; auto localUpmostScope = new unordered_map(); _localScope.push_back(localUpmostScope); @@ -28,10 +27,6 @@ void BoundScope::GoInnerScope() { auto innerScope = new unordered_map(); _localScope.push_back(innerScope); } - /* - if (_deepestScope < _currentScope){ - _deepestScope = _currentScope; - }*/ } void BoundScope::GoOuterScope() { diff --git a/src/Binder/BoundVariables/BoundScope.hpp b/src/Binder/BoundVariables/BoundScope.hpp index 2388c19..82a646a 100644 --- a/src/Binder/BoundVariables/BoundScope.hpp +++ b/src/Binder/BoundVariables/BoundScope.hpp @@ -18,7 +18,6 @@ class BoundScope { vector*> _localScope; int _currentScope; int _lastCreatedScope; - int _deepestScope; public: explicit BoundScope(unordered_map *tableScope); ~BoundScope(); @@ -31,8 +30,8 @@ public: VariableAssignment CreateExplicitLocal(int identifier, std::shared_ptr type); VariableAssignment AssignVariable(int identifier, const std::shared_ptr& type); - int GetDeepestScope(){ - return _deepestScope; + size_t GetLocalVariableCount(){ + return _localScope.size(); } int GetCurrentScope(){ diff --git a/src/Binder/BoundVariables/BoundVariableKey.hpp b/src/Binder/BoundVariables/BoundVariableKey.hpp index 022bebe..f3ac63f 100644 --- a/src/Binder/BoundVariables/BoundVariableKey.hpp +++ b/src/Binder/BoundVariables/BoundVariableKey.hpp @@ -1,5 +1,3 @@ -#include - #ifndef PORYGONLANG_BOUNDVARIABLEKEY_HPP #define PORYGONLANG_BOUNDVARIABLEKEY_HPP @@ -7,39 +5,39 @@ #include class BoundVariableKey{ - int _identifier; - unsigned int _scopeId; - bool _isCreation; - uint64_t _hash; + const int _identifier; + const unsigned int _scopeId; + const bool _isCreation; + const uint64_t _hash; - static uint64_t KnuthsHash(int i1, int i2) + static uint64_t KnuthsHash(unsigned int i1, unsigned int i2) { uint64_t ret = i1; ret *= 2654435761U; return ret ^ i2; } public: - BoundVariableKey(int id, unsigned int scope, bool creation){ - _identifier = id; - _scopeId = scope; - _hash = KnuthsHash(scope, id); - _isCreation = creation; - } + BoundVariableKey(int id, unsigned int scope, bool creation) + : _identifier(id), + _scopeId(scope), + _isCreation(creation), + _hash(KnuthsHash(id, scope)) + {} - int GetIdentifier(){ + const int GetIdentifier(){ return _identifier; } - unsigned int GetScopeId(){ + const unsigned int GetScopeId(){ return _scopeId; } - bool IsCreation(){ + const bool IsCreation(){ return _isCreation; } - uint64_t GetHash(){ + const uint64_t GetHash(){ return _hash; } }; diff --git a/src/Evaluator/EvalValues/EvalValue.cpp b/src/Evaluator/EvalValues/EvalValue.cpp index 981fd9c..375fa9b 100644 --- a/src/Evaluator/EvalValues/EvalValue.cpp +++ b/src/Evaluator/EvalValues/EvalValue.cpp @@ -5,11 +5,7 @@ extern "C" { TypeClass GetEvalValueTypeClass(EvalValue* v){ - return v->GetType().get()->GetClass(); - } - - ScriptType* GetEvalValueType(EvalValue* v){ - return v->GetType().get(); + return v->GetTypeClass(); } int64_t EvaluateEvalValueInteger(EvalValue* v){ diff --git a/src/Evaluator/EvalValues/EvalValue.hpp b/src/Evaluator/EvalValues/EvalValue.hpp index f3a1f4b..8875df3 100644 --- a/src/Evaluator/EvalValues/EvalValue.hpp +++ b/src/Evaluator/EvalValues/EvalValue.hpp @@ -12,7 +12,7 @@ class EvalValue{ public: EvalValue() = default; virtual ~EvalValue() = default; - virtual std::shared_ptr GetType() = 0; + virtual const TypeClass GetTypeClass() = 0; virtual bool operator ==(EvalValue* b) = 0; @@ -43,28 +43,27 @@ public: }; class BooleanEvalValue : public EvalValue{ - bool _value; - std::shared_ptr _type; + const bool _value; public: - explicit BooleanEvalValue(bool val){ - _value = val; - _type = std::make_shared(TypeClass::Bool); + explicit BooleanEvalValue(bool val) + : _value(val) + { } shared_ptr Clone() final{ return make_shared(_value); } - std::shared_ptr GetType() final{ - return _type; - }; + const TypeClass GetTypeClass() final{ + return TypeClass ::Bool; + } bool EvaluateBool() final{ return _value; } bool operator ==(EvalValue* b) final{ - if (b->GetType()->GetClass() != TypeClass::Bool) + if (b->GetTypeClass() != TypeClass::Bool) return false; return this->EvaluateBool() == b->EvaluateBool(); }; diff --git a/src/Evaluator/EvalValues/NumericEvalValue.cpp b/src/Evaluator/EvalValues/NumericEvalValue.cpp index 9b788da..8cf2afa 100644 --- a/src/Evaluator/EvalValues/NumericEvalValue.cpp +++ b/src/Evaluator/EvalValues/NumericEvalValue.cpp @@ -66,7 +66,7 @@ NumericEvalValue *NumericEvalValue::operator/(NumericEvalValue *b) { } bool NumericEvalValue::operator==(EvalValue *b) { - if (b->GetType()->GetClass() != TypeClass::Number) + if (b->GetTypeClass() != TypeClass::Number) return false; auto numVal = (NumericEvalValue*)b; if (this->IsFloat() != numVal->IsFloat()) diff --git a/src/Evaluator/EvalValues/NumericEvalValue.hpp b/src/Evaluator/EvalValues/NumericEvalValue.hpp index 21991b4..f2c4ba4 100644 --- a/src/Evaluator/EvalValues/NumericEvalValue.hpp +++ b/src/Evaluator/EvalValues/NumericEvalValue.hpp @@ -10,12 +10,11 @@ class NumericEvalValue : public EvalValue{ virtual long GetIntegerValue() = 0; virtual double GetFloatValue() = 0; -protected: - std::shared_ptr _type; public: virtual const bool IsFloat() = 0; - std::shared_ptr GetType() override { - return _type; + + const TypeClass GetTypeClass() final{ + return TypeClass ::Number; } NumericEvalValue* operator +(NumericEvalValue* b); @@ -35,7 +34,6 @@ class IntegerEvalValue : public NumericEvalValue{ double GetFloatValue() final{ throw EvaluationException("Attempting to retrieve float from int eval value."); } public: explicit IntegerEvalValue(long value){ - _type = std::make_shared(true, false); _value = value; } const bool IsFloat() final{ @@ -61,7 +59,6 @@ class FloatEvalValue : public NumericEvalValue{ double GetFloatValue() final{return _value;} public: explicit FloatEvalValue(double value){ - _type = std::make_shared(true, true); _value = value; } const bool IsFloat() final{ diff --git a/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp b/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp index adc38d6..4b20f2d 100644 --- a/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp +++ b/src/Evaluator/EvalValues/ScriptFunctionEvalValue.hpp @@ -38,17 +38,21 @@ public: _hash = rand(); } - std::shared_ptr GetType() final{ + std::shared_ptr GetType(){ return _type; } + const TypeClass GetTypeClass() final{ + return TypeClass ::Function; + } + shared_ptr Clone() final{ return shared_ptr(new ScriptFunctionEvalValue(_innerBlock, _scope, _type, _hash)); } bool operator ==(EvalValue* b) final{ - if (b->GetType()->GetClass() != TypeClass::Function) + if (b->GetTypeClass() != TypeClass::Function) return false; return this->_hash == ((ScriptFunctionEvalValue*)b)->_hash; }; diff --git a/src/Evaluator/EvalValues/StringEvalValue.cpp b/src/Evaluator/EvalValues/StringEvalValue.cpp deleted file mode 100644 index cde4137..0000000 --- a/src/Evaluator/EvalValues/StringEvalValue.cpp +++ /dev/null @@ -1,2 +0,0 @@ - -#include "StringEvalValue.hpp" diff --git a/src/Evaluator/EvalValues/StringEvalValue.hpp b/src/Evaluator/EvalValues/StringEvalValue.hpp index 5f4acac..561ec6d 100644 --- a/src/Evaluator/EvalValues/StringEvalValue.hpp +++ b/src/Evaluator/EvalValues/StringEvalValue.hpp @@ -11,19 +11,18 @@ using namespace std; class StringEvalValue : public EvalValue{ string _value; size_t _hash; - std::shared_ptr _type; public: explicit StringEvalValue(string s){ _value = move(s); _hash = HashedString::ConstHash (_value.c_str()); - _type = std::make_shared(true, _hash); } - std::shared_ptr GetType() final{ - return _type; - }; + const TypeClass GetTypeClass() final{ + return TypeClass ::String; + } + bool operator ==(EvalValue* b) final{ - if (b->GetType()->GetClass() != TypeClass::String) + if (b->GetTypeClass() != TypeClass::String) return false; return this->_hash == b->GetHashCode(); }; diff --git a/src/Evaluator/EvalValues/TableEvalValue.cpp b/src/Evaluator/EvalValues/TableEvalValue.cpp deleted file mode 100644 index 4f08c93..0000000 --- a/src/Evaluator/EvalValues/TableEvalValue.cpp +++ /dev/null @@ -1,2 +0,0 @@ - -#include "TableEvalValue.hpp" diff --git a/src/Evaluator/EvalValues/TableEvalValue.hpp b/src/Evaluator/EvalValues/TableEvalValue.hpp index c3b5009..dfaa620 100644 --- a/src/Evaluator/EvalValues/TableEvalValue.hpp +++ b/src/Evaluator/EvalValues/TableEvalValue.hpp @@ -8,23 +8,20 @@ using namespace std; class TableEvalValue : public EvalValue { shared_ptr>> _table; - shared_ptr _type; size_t _hash; - explicit TableEvalValue(shared_ptr>> table, shared_ptr type, size_t hash){ + explicit TableEvalValue(shared_ptr>> table, size_t hash){ _table = std::move(table); - _type = std::move(type); _hash = hash; } public: - explicit TableEvalValue(shared_ptr>> table, shared_ptr type){ + explicit TableEvalValue(shared_ptr>> table){ _table = std::move(table); - _type = std::move(type); _hash = rand(); } - std::shared_ptr GetType() final{ - return _type; + const TypeClass GetTypeClass() final{ + return TypeClass ::Table; } size_t GetHashCode() final{ @@ -36,7 +33,7 @@ public: } shared_ptr Clone() final{ - return shared_ptr(new TableEvalValue(_table, _type, _hash)); + return shared_ptr(new TableEvalValue(_table, _hash)); } shared_ptr IndexValue(EvalValue* val) final{ diff --git a/src/Evaluator/EvaluationScope/EvaluationScope.cpp b/src/Evaluator/EvaluationScope/EvaluationScope.cpp index cc0a873..4f93909 100644 --- a/src/Evaluator/EvaluationScope/EvaluationScope.cpp +++ b/src/Evaluator/EvaluationScope/EvaluationScope.cpp @@ -2,9 +2,9 @@ #include "EvaluationScope.hpp" #include -EvaluationScope::EvaluationScope(unordered_map> *scriptVariables, int deepestScope) { +EvaluationScope::EvaluationScope(unordered_map> *scriptVariables, int localVariableCount) { _scriptScope = scriptVariables; - _localScope = unordered_map>(deepestScope); + _localScope = unordered_map>(localVariableCount); } void EvaluationScope::CreateVariable(BoundVariableKey* key, const shared_ptr &value) { diff --git a/src/Evaluator/Evaluator.cpp b/src/Evaluator/Evaluator.cpp index 872ea58..10382af 100644 --- a/src/Evaluator/Evaluator.cpp +++ b/src/Evaluator/Evaluator.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include "Evaluator.hpp" @@ -12,7 +14,7 @@ using namespace std; EvalValue* Evaluator::Evaluate(BoundScriptStatement *statement) { - this->_evaluationScope = make_shared(this->_scriptData->_scriptVariables, statement->GetDeepestScope()); + this->_evaluationScope = make_shared(this->_scriptData->_scriptVariables, statement->GetLocalVariableCount()); EvaluateBlockStatement(statement, false); return this -> _returnValue.get(); } @@ -223,10 +225,6 @@ shared_ptr Evaluator::EvaluateFunctionCallExpression(BoundExpression* for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){ auto parameter = parameters[i]; - auto requiredType = parameterTypes.at(i); - if (*parameter->GetType() != requiredType.get()){ - throw EvaluationException("Passed wrong type to function."); - } auto key = parameterKeys.at(i); this->_evaluationScope->CreateVariable(key.get(), parameter->Clone()); } @@ -249,10 +247,6 @@ shared_ptr Evaluator::EvaluateFunction(ScriptFunctionEvalValue *funct for (int i = 0; i < parameterTypes.size() && i < parameterKeys.size() && i < parameters.size(); i++){ auto parameter = parameters[i]; - auto requiredType = parameterTypes.at(i); - if (*parameter->GetType() != requiredType.get()){ - throw EvaluationException("Passed wrong type to function."); - } auto key = parameterKeys.at(i); this->_evaluationScope->CreateVariable(key.get(), parameter->Clone()); } @@ -280,7 +274,7 @@ shared_ptr Evaluator::EvaluateNumericTableExpression(BoundExpression values -> insert({i + 1, val}); } auto valuesPointer = shared_ptr>>(values); - return make_shared(valuesPointer, tableExpression->GetType()); + return make_shared(valuesPointer); } shared_ptr Evaluator::EvaluateComplexTableExpression(BoundExpression *expression) { @@ -291,10 +285,10 @@ shared_ptr Evaluator::EvaluateComplexTableExpression(BoundExpression for (auto i : *declaredVars){ variables->insert({i.first, nullptr}); } - auto evaluator = make_shared(variables.get(), type -> GetDeepestScope()); + auto evaluator = make_shared(variables.get(), type -> GetLocalVariableCount()); auto currentEvaluator = this -> _evaluationScope; this -> _evaluationScope = evaluator; this -> EvaluateBlockStatement(tableExpression->GetBlock(), false); this -> _evaluationScope = currentEvaluator; - return shared_ptr(new TableEvalValue(variables, tableExpression->GetType())); + return make_shared(variables); } diff --git a/src/Script.cpp b/src/Script.cpp index 653b03a..74cadd8 100644 --- a/src/Script.cpp +++ b/src/Script.cpp @@ -69,7 +69,7 @@ EvalValue *Script::GetLastValue() { bool Script::HasFunction(const string &key) { auto f = _scriptVariables->find(HashedString(key).GetHash()); - return f != _scriptVariables->end() && f.operator->()->second->GetType()->GetClass() == TypeClass ::Function; + return f != _scriptVariables->end() && f.operator->()->second->GetTypeClass() == TypeClass ::Function; } shared_ptr Script::CallFunction(const string &key, vector variables) { diff --git a/src/TableScriptType.hpp b/src/TableScriptType.hpp index 058babe..29abb48 100644 --- a/src/TableScriptType.hpp +++ b/src/TableScriptType.hpp @@ -5,13 +5,14 @@ #include "Binder/BoundVariables/BoundVariable.hpp" class TableScriptType : public ScriptType{ - unordered_map* _values; - int _deepestScope; + const unordered_map* _values; + const int _localVariableCount; public: - explicit TableScriptType(unordered_map* values, int deepestScope) : ScriptType(TypeClass::Table){ - _values = values; - _deepestScope = deepestScope; - } + explicit TableScriptType(unordered_map* values, int localVariableCount) + : ScriptType(TypeClass::Table), + _values(values), + _localVariableCount(localVariableCount) + {} ~TableScriptType() final{ for (auto i : *_values){ @@ -32,12 +33,12 @@ public: throw "TODO: indexing with dynamic keys"; } - unordered_map* GetValues(){ + const unordered_map* GetValues(){ return _values; } - int GetDeepestScope(){ - return _deepestScope; + const int GetLocalVariableCount(){ + return _localVariableCount; } }; diff --git a/tests/integration/Functions.cpp b/tests/integration/Functions.cpp index e77e741..76a7800 100644 --- a/tests/integration/Functions.cpp +++ b/tests/integration/Functions.cpp @@ -8,7 +8,7 @@ TEST_CASE( "Define script function", "[integration]" ) { script->Evaluate(); auto variable = script->GetVariable("add"); REQUIRE(variable != nullptr); - REQUIRE(variable->GetType()->GetClass() == TypeClass::Function); + REQUIRE(variable->GetTypeClass() == TypeClass::Function); delete script; } @@ -18,9 +18,9 @@ TEST_CASE( "Define script function and call", "[integration]" ) { script->Evaluate(); auto variable = script->GetVariable("add"); REQUIRE(variable != nullptr); - REQUIRE(variable->GetType()->GetClass() == TypeClass::Function); + REQUIRE(variable->GetTypeClass() == TypeClass::Function); auto result = script->GetVariable("result"); - REQUIRE(result->GetType()->GetClass() == TypeClass::Number); + REQUIRE(result->GetTypeClass() == TypeClass::Number); REQUIRE(result->EvaluateInteger() == 3); delete script; } @@ -31,9 +31,9 @@ TEST_CASE( "Define script function and call multiple times", "[integration]" ) { script->Evaluate(); auto variable = script->GetVariable("add"); REQUIRE(variable != nullptr); - REQUIRE(variable->GetType()->GetClass() == TypeClass::Function); + REQUIRE(variable->GetTypeClass() == TypeClass::Function); auto result = script->GetVariable("result"); - REQUIRE(result->GetType()->GetClass() == TypeClass::Number); + REQUIRE(result->GetTypeClass() == TypeClass::Number); REQUIRE(result->EvaluateInteger() == 5); delete script; } @@ -52,7 +52,7 @@ TEST_CASE( "Define script function and call from extern", "[integration]" ) { delete toAddVal; auto result = script->GetVariable("result"); - REQUIRE(result->GetType()->GetClass() == TypeClass::Number); + REQUIRE(result->GetTypeClass() == TypeClass::Number); REQUIRE(result->EvaluateInteger() == 11); delete script; } @@ -74,11 +74,11 @@ TEST_CASE( "Define script function and return", "[integration]" ) { delete toAddVal; delete toAddVal2; - REQUIRE(result->GetType()->GetClass() == TypeClass::Number); + REQUIRE(result->GetTypeClass() == TypeClass::Number); REQUIRE(result->EvaluateInteger() == 11); auto variable = script->GetVariable("val"); - REQUIRE(variable->GetType()->GetClass() == TypeClass::Number); + REQUIRE(variable->GetTypeClass() == TypeClass::Number); REQUIRE(variable->EvaluateInteger() == 0); delete script; @@ -100,7 +100,7 @@ end script->CallFunction("add", {}); auto variable = script->GetVariable("val"); - REQUIRE(variable->GetType()->GetClass() == TypeClass::Number); + REQUIRE(variable->GetTypeClass() == TypeClass::Number); REQUIRE(variable->EvaluateInteger() == 5); delete script; @@ -128,7 +128,7 @@ test() script->Evaluate(); auto variable = script->GetVariable("result"); - REQUIRE(variable->GetType()->GetClass() == TypeClass::Number); + REQUIRE(variable->GetTypeClass() == TypeClass::Number); REQUIRE(variable->EvaluateInteger() == 50); delete script;