Work on performance improvements
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-06-13 16:26:10 +02:00
parent e93bcab14d
commit 1cb65f17c9
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
19 changed files with 84 additions and 108 deletions

View File

@ -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<TableScriptType>(new TableScriptType(tableScope, innerScope->GetDeepestScope()));
auto tableType = shared_ptr<TableScriptType>(new TableScriptType(tableScope, innerScope->GetLocalVariableCount()));
delete innerScope;
return new BoundTableExpression((BoundBlockStatement*)block, tableType, expression->GetStartPosition(), expression->GetLength());

View File

@ -57,18 +57,19 @@ public:
};
class BoundScriptStatement : public BoundBlockStatement{
int _deepestScope;
int _localVariableCount;
public:
explicit BoundScriptStatement(vector<BoundStatement*> statements, int deepestScope) : BoundBlockStatement(std::move(statements)){
_deepestScope = deepestScope;
explicit BoundScriptStatement(vector<BoundStatement*> statements, int localVariableCount)
: BoundBlockStatement(std::move(statements)){
_localVariableCount = localVariableCount;
}
BoundStatementKind GetKind() final{
return BoundStatementKind ::Script;
}
int GetDeepestScope(){
return _deepestScope;
int GetLocalVariableCount(){
return _localVariableCount;
}
};

View File

@ -6,7 +6,6 @@
BoundScope::BoundScope(unordered_map<int, BoundVariable *> *tableScope) {
_tableScope = tableScope;
_currentScope = 1;
_deepestScope = 1;
_lastCreatedScope = 1;
auto localUpmostScope = new unordered_map<int, BoundVariable*>();
_localScope.push_back(localUpmostScope);
@ -28,10 +27,6 @@ void BoundScope::GoInnerScope() {
auto innerScope = new unordered_map<int, BoundVariable*>();
_localScope.push_back(innerScope);
}
/*
if (_deepestScope < _currentScope){
_deepestScope = _currentScope;
}*/
}
void BoundScope::GoOuterScope() {

View File

@ -18,7 +18,6 @@ class BoundScope {
vector<unordered_map<int, BoundVariable*>*> _localScope;
int _currentScope;
int _lastCreatedScope;
int _deepestScope;
public:
explicit BoundScope(unordered_map<int, BoundVariable*> *tableScope);
~BoundScope();
@ -31,8 +30,8 @@ public:
VariableAssignment CreateExplicitLocal(int identifier, std::shared_ptr<ScriptType> type);
VariableAssignment AssignVariable(int identifier, const std::shared_ptr<ScriptType>& type);
int GetDeepestScope(){
return _deepestScope;
size_t GetLocalVariableCount(){
return _localScope.size();
}
int GetCurrentScope(){

View File

@ -1,5 +1,3 @@
#include <utility>
#ifndef PORYGONLANG_BOUNDVARIABLEKEY_HPP
#define PORYGONLANG_BOUNDVARIABLEKEY_HPP
@ -7,39 +5,39 @@
#include <string>
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;
}
};

View File

@ -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){

View File

@ -12,7 +12,7 @@ class EvalValue{
public:
EvalValue() = default;
virtual ~EvalValue() = default;
virtual std::shared_ptr<ScriptType> 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<ScriptType> _type;
const bool _value;
public:
explicit BooleanEvalValue(bool val){
_value = val;
_type = std::make_shared<ScriptType>(TypeClass::Bool);
explicit BooleanEvalValue(bool val)
: _value(val)
{
}
shared_ptr<EvalValue> Clone() final{
return make_shared<BooleanEvalValue>(_value);
}
std::shared_ptr<ScriptType> 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();
};

View File

@ -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())

View File

@ -10,12 +10,11 @@ class NumericEvalValue : public EvalValue{
virtual long GetIntegerValue() = 0;
virtual double GetFloatValue() = 0;
protected:
std::shared_ptr<ScriptType> _type;
public:
virtual const bool IsFloat() = 0;
std::shared_ptr<ScriptType> 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<NumericScriptType>(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<NumericScriptType>(true, true);
_value = value;
}
const bool IsFloat() final{

View File

@ -38,17 +38,21 @@ public:
_hash = rand();
}
std::shared_ptr<ScriptType> GetType() final{
std::shared_ptr<ScriptType> GetType(){
return _type;
}
const TypeClass GetTypeClass() final{
return TypeClass ::Function;
}
shared_ptr<EvalValue> Clone() final{
return shared_ptr<ScriptFunctionEvalValue>(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;
};

View File

@ -1,2 +0,0 @@
#include "StringEvalValue.hpp"

View File

@ -11,19 +11,18 @@ using namespace std;
class StringEvalValue : public EvalValue{
string _value;
size_t _hash;
std::shared_ptr<ScriptType> _type;
public:
explicit StringEvalValue(string s){
_value = move(s);
_hash = HashedString::ConstHash (_value.c_str());
_type = std::make_shared<StringScriptType>(true, _hash);
}
std::shared_ptr<ScriptType> 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();
};

View File

@ -1,2 +0,0 @@
#include "TableEvalValue.hpp"

View File

@ -8,23 +8,20 @@ using namespace std;
class TableEvalValue : public EvalValue {
shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> _table;
shared_ptr<ScriptType> _type;
size_t _hash;
explicit TableEvalValue(shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> table, shared_ptr<ScriptType> type, size_t hash){
explicit TableEvalValue(shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> table, size_t hash){
_table = std::move(table);
_type = std::move(type);
_hash = hash;
}
public:
explicit TableEvalValue(shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> table, shared_ptr<ScriptType> type){
explicit TableEvalValue(shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>> table){
_table = std::move(table);
_type = std::move(type);
_hash = rand();
}
std::shared_ptr<ScriptType> GetType() final{
return _type;
const TypeClass GetTypeClass() final{
return TypeClass ::Table;
}
size_t GetHashCode() final{
@ -36,7 +33,7 @@ public:
}
shared_ptr<EvalValue> Clone() final{
return shared_ptr<EvalValue>(new TableEvalValue(_table, _type, _hash));
return shared_ptr<EvalValue>(new TableEvalValue(_table, _hash));
}
shared_ptr<EvalValue> IndexValue(EvalValue* val) final{

View File

@ -2,9 +2,9 @@
#include "EvaluationScope.hpp"
#include <memory>
EvaluationScope::EvaluationScope(unordered_map<size_t, shared_ptr<EvalValue>> *scriptVariables, int deepestScope) {
EvaluationScope::EvaluationScope(unordered_map<size_t, shared_ptr<EvalValue>> *scriptVariables, int localVariableCount) {
_scriptScope = scriptVariables;
_localScope = unordered_map<uint64_t, shared_ptr<EvalValue>>(deepestScope);
_localScope = unordered_map<uint64_t, shared_ptr<EvalValue>>(localVariableCount);
}
void EvaluationScope::CreateVariable(BoundVariableKey* key, const shared_ptr<EvalValue> &value) {

View File

@ -1,3 +1,5 @@
#include <memory>
#include <utility>
#include <memory>
#include "Evaluator.hpp"
@ -12,7 +14,7 @@
using namespace std;
EvalValue* Evaluator::Evaluate(BoundScriptStatement *statement) {
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptData->_scriptVariables, statement->GetDeepestScope());
this->_evaluationScope = make_shared<EvaluationScope>(this->_scriptData->_scriptVariables, statement->GetLocalVariableCount());
EvaluateBlockStatement(statement, false);
return this -> _returnValue.get();
}
@ -223,10 +225,6 @@ shared_ptr<EvalValue> 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<EvalValue> 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<EvalValue> Evaluator::EvaluateNumericTableExpression(BoundExpression
values -> insert({i + 1, val});
}
auto valuesPointer = shared_ptr<unordered_map<size_t, shared_ptr<EvalValue>>>(values);
return make_shared<TableEvalValue>(valuesPointer, tableExpression->GetType());
return make_shared<TableEvalValue>(valuesPointer);
}
shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(BoundExpression *expression) {
@ -291,10 +285,10 @@ shared_ptr<EvalValue> Evaluator::EvaluateComplexTableExpression(BoundExpression
for (auto i : *declaredVars){
variables->insert({i.first, nullptr});
}
auto evaluator = make_shared<EvaluationScope>(variables.get(), type -> GetDeepestScope());
auto evaluator = make_shared<EvaluationScope>(variables.get(), type -> GetLocalVariableCount());
auto currentEvaluator = this -> _evaluationScope;
this -> _evaluationScope = evaluator;
this -> EvaluateBlockStatement(tableExpression->GetBlock(), false);
this -> _evaluationScope = currentEvaluator;
return shared_ptr<TableEvalValue>(new TableEvalValue(variables, tableExpression->GetType()));
return make_shared<TableEvalValue>(variables);
}

View File

@ -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<EvalValue> Script::CallFunction(const string &key, vector<EvalValue *> variables) {

View File

@ -5,13 +5,14 @@
#include "Binder/BoundVariables/BoundVariable.hpp"
class TableScriptType : public ScriptType{
unordered_map<int, BoundVariable*>* _values;
int _deepestScope;
const unordered_map<int, BoundVariable*>* _values;
const int _localVariableCount;
public:
explicit TableScriptType(unordered_map<int, BoundVariable*>* values, int deepestScope) : ScriptType(TypeClass::Table){
_values = values;
_deepestScope = deepestScope;
}
explicit TableScriptType(unordered_map<int, BoundVariable*>* 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<int, BoundVariable*>* GetValues(){
const unordered_map<int, BoundVariable*>* GetValues(){
return _values;
}
int GetDeepestScope(){
return _deepestScope;
const int GetLocalVariableCount(){
return _localVariableCount;
}
};

View File

@ -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;