Commit db42f9bd authored by Victor Yacovlev's avatar Victor Yacovlev

Improved runtime performance

parent c4285c7d
...@@ -56,20 +56,21 @@ static void getVarListSizes(const QVariant & var, int sizes[3], int fromDim) ...@@ -56,20 +56,21 @@ static void getVarListSizes(const QVariant & var, int sizes[3], int fromDim)
static VM::AnyValue makeAnyValue(const QVariant & val, std::list<Bytecode::ValueType> vt) static VM::AnyValue makeAnyValue(const QVariant & val, std::list<Bytecode::ValueType> vt)
{ {
VM::AnyValue result;
if (val==QVariant::Invalid) if (val==QVariant::Invalid)
return VM::AnyValue(); return result;
switch (vt.front()) switch (vt.front())
{ {
case Bytecode::VT_int: case Bytecode::VT_int:
return VM::AnyValue(val.toInt()); result = VM::AnyValue(val.toInt()); break;
case Bytecode::VT_real: case Bytecode::VT_real:
return VM::AnyValue(val.toDouble()); result = VM::AnyValue(val.toDouble()); break;
case Bytecode::VT_bool: case Bytecode::VT_bool:
return VM::AnyValue(bool(val.toBool())); result = VM::AnyValue(bool(val.toBool())); break;
case Bytecode::VT_char: case Bytecode::VT_char:
return VM::AnyValue(Kumir::Char(val.toChar().unicode())); result = VM::AnyValue(Kumir::Char(val.toChar().unicode())); break;
case Bytecode::VT_string: case Bytecode::VT_string:
return VM::AnyValue(val.toString().toStdWString()); result = VM::AnyValue(val.toString().toStdWString()); break;
case Bytecode::VT_record: case Bytecode::VT_record:
{ {
QVariantList valueFields = val.toList(); QVariantList valueFields = val.toList();
...@@ -101,11 +102,13 @@ static VM::AnyValue makeAnyValue(const QVariant & val, std::list<Bytecode::Value ...@@ -101,11 +102,13 @@ static VM::AnyValue makeAnyValue(const QVariant & val, std::list<Bytecode::Value
} }
it++; it++;
} }
return value; result = value;
break;
} }
default: default:
return VM::AnyValue(); break;
} }
return result;
} }
static Bytecode::TableElem makeConstant(const ConstValue & val) static Bytecode::TableElem makeConstant(const ConstValue & val)
...@@ -116,7 +119,8 @@ static Bytecode::TableElem makeConstant(const ConstValue & val) ...@@ -116,7 +119,8 @@ static Bytecode::TableElem makeConstant(const ConstValue & val)
e.dimension = val.dimension; e.dimension = val.dimension;
if (val.dimension==0) { if (val.dimension==0) {
VM::Variable var; VM::Variable var;
var.setValue(makeAnyValue(val.value, val.baseType)); VM::AnyValue vv = makeAnyValue(val.value, val.baseType);
var.setValue(vv);
var.setBaseType(val.baseType.front()); var.setBaseType(val.baseType.front());
var.setDimension(val.dimension); var.setDimension(val.dimension);
var.setConstantFlag(true); var.setConstantFlag(true);
......
...@@ -28,13 +28,43 @@ class AnyValue ...@@ -28,13 +28,43 @@ class AnyValue
friend class Variable; friend class Variable;
public: public:
inline explicit AnyValue() { __init__(); } inline explicit AnyValue() { __init__(); }
inline AnyValue(const AnyValue & other) {
__init__();
tp = other.tp;
if (other.svalue) {
svalue = new String(*(other.svalue));
}
if (other.uvalue) {
uvalue = new Record(*(other.uvalue));
}
if (other.avalue) {
avalue = new std::vector<class AnyValue>(*(other.avalue));
}
if (tp==VT_int)
ivalue = other.ivalue;
if (tp==VT_real)
rvalue = other.rvalue;
if (tp==VT_bool)
bvalue = other.bvalue;
if (tp==VT_char)
cvalue = other.cvalue;
}
inline explicit AnyValue(ValueType t) { __init__(); tp = t; svalue = t==VT_string? new String() : 0; ivalue = 0; } inline explicit AnyValue(ValueType t) { __init__(); tp = t; svalue = t==VT_string? new String() : 0; ivalue = 0; }
inline explicit AnyValue(int v) { __init__(); tp = VT_int; ivalue = v; } inline explicit AnyValue(int v) {
__init__();
tp = VT_int;
ivalue = v;
}
inline explicit AnyValue(real v) { __init__(); tp = VT_real; rvalue = v; } inline explicit AnyValue(real v) { __init__(); tp = VT_real; rvalue = v; }
inline explicit AnyValue(bool v) { __init__(); tp = VT_bool; bvalue = v; } inline explicit AnyValue(bool v) { __init__(); tp = VT_bool; bvalue = v; }
inline explicit AnyValue(Char v) { __init__(); tp = VT_char; cvalue = v; } inline explicit AnyValue(Char v) { __init__(); tp = VT_char; cvalue = v; }
inline explicit AnyValue(const String & v) { tp = VT_string; svalue = new String(v); } inline explicit AnyValue(const String & v) { __init__(); tp = VT_string; svalue = new String(v); }
inline explicit AnyValue(const Record & value) { __init__(); tp = VT_record; uvalue = value; } inline explicit AnyValue(const Record & value) {
__init__();
tp = VT_record;
uvalue = new Record(value);
}
inline void operator=(ValueType t) { __init__(); tp = t; svalue = t==VT_string? new String() : 0; } inline void operator=(ValueType t) { __init__(); tp = t; svalue = t==VT_string? new String() : 0; }
inline void operator=(int v) { __init__(); tp = VT_int; ivalue = v; } inline void operator=(int v) { __init__(); tp = VT_int; ivalue = v; }
...@@ -45,7 +75,28 @@ public: ...@@ -45,7 +75,28 @@ public:
inline void operator=(const Record & value) { inline void operator=(const Record & value) {
__init__(); __init__();
tp = VT_record; tp = VT_record;
uvalue = value; uvalue = new Record(value);
}
inline void operator=(const AnyValue &other) {
__init__();
tp = other.tp;
if (other.svalue) {
svalue = new String(*(other.svalue));
}
if (other.uvalue) {
uvalue = new Record(*(other.uvalue));
}
if (other.avalue) {
avalue = new std::vector<class AnyValue>(*(other.avalue));
}
if (tp==VT_int)
ivalue = other.ivalue;
if (tp==VT_real)
rvalue = other.rvalue;
if (tp==VT_bool)
bvalue = other.bvalue;
if (tp==VT_char)
cvalue = other.cvalue;
} }
inline int toInt() const { inline int toInt() const {
...@@ -77,13 +128,13 @@ public: ...@@ -77,13 +128,13 @@ public:
return sval; return sval;
} }
else if (tp==VT_void) return String(); else if (tp==VT_void) return String();
else return *svalue; else return String(*svalue);
} }
inline const Record & toRecord() const { inline const Record & toRecord() const {
return uvalue; return *uvalue;
} }
inline Record & toRecord() { inline Record & toRecord() {
return uvalue; return *uvalue;
} }
template <typename T> template <typename T>
...@@ -97,7 +148,7 @@ public: ...@@ -97,7 +148,7 @@ public:
char tp = T::_()[i]; char tp = T::_()[i];
switch (tp) { switch (tp) {
case 'i': { case 'i': {
int val = uvalue.at(i).toInt(); int val = uvalue->at(i).toInt();
fieldptr = &val; fieldptr = &val;
copySize = sizeof(int); copySize = sizeof(int);
ptr = (void*)((size_t)(ptr)+copySize); ptr = (void*)((size_t)(ptr)+copySize);
...@@ -106,7 +157,7 @@ public: ...@@ -106,7 +157,7 @@ public:
break; break;
} }
case 'd': { case 'd': {
double val = uvalue.at(i).toReal(); double val = uvalue->at(i).toReal();
fieldptr = &val; fieldptr = &val;
copySize = sizeof(double); copySize = sizeof(double);
ptr = (void*)((size_t)(ptr)+copySize); ptr = (void*)((size_t)(ptr)+copySize);
...@@ -114,7 +165,7 @@ public: ...@@ -114,7 +165,7 @@ public:
break; break;
} }
case 'b': { case 'b': {
bool val = uvalue.at(i).toBool(); bool val = uvalue->at(i).toBool();
fieldptr = &val; fieldptr = &val;
copySize = sizeof(bool); copySize = sizeof(bool);
ptr = (void*)((size_t)(ptr)+copySize); ptr = (void*)((size_t)(ptr)+copySize);
...@@ -122,7 +173,7 @@ public: ...@@ -122,7 +173,7 @@ public:
break; break;
} }
case 'c': { case 'c': {
Char val = uvalue.at(i).toChar(); Char val = uvalue->at(i).toChar();
fieldptr = &val; fieldptr = &val;
copySize = sizeof(Char); copySize = sizeof(Char);
...@@ -130,7 +181,7 @@ public: ...@@ -130,7 +181,7 @@ public:
break; break;
} }
case 's': { case 's': {
String val = uvalue.at(i).toString(); String val = uvalue->at(i).toString();
fieldptr = &val; fieldptr = &val;
copySize = sizeof(String); copySize = sizeof(String);
ptr = (void*)((size_t)(ptr)+copySize); ptr = (void*)((size_t)(ptr)+copySize);
...@@ -145,28 +196,42 @@ public: ...@@ -145,28 +196,42 @@ public:
return value; return value;
} }
inline bool isValid() const { return tp!=VT_void || avalue.size()>0; } inline bool isValid() const { return tp!=VT_void || ( avalue && avalue->size()>0 ); }
inline ValueType type() const { return tp; } inline ValueType type() const { return tp; }
inline const AnyValue & at(size_t index) const { return avalue[index]; } inline const AnyValue & at(size_t index) const { return avalue->at(index); }
inline const AnyValue & operator[](size_t index) const { return at(index); } inline const AnyValue & operator[](size_t index) const { return at(index); }
inline AnyValue & at(size_t index) { return avalue[index]; } inline AnyValue & at(size_t index) { return avalue->at(index); }
inline AnyValue & operator[](size_t index) { return at(index); } inline AnyValue & operator[](size_t index) { return at(index); }
inline size_t rawSize() const { return avalue.size(); } inline size_t rawSize() const { return avalue? avalue->size() : 0; }
inline ~AnyValue() {
if (svalue)
delete svalue;
if (avalue) {
avalue->clear();
delete avalue;
}
if (uvalue) {
uvalue->clear();
delete uvalue;
}
}
protected: protected:
inline void resize(size_t size) { inline void resize(size_t size) {
if (!avalue)
avalue = new std::vector<class AnyValue>(size);
if (size==0) { if (size==0) {
if (avalue.size()) if (avalue->size())
avalue.clear(); avalue->clear();
} }
else { else {
if (size != avalue.size()) { if (size != avalue->size()) {
size_t asize = avalue.size(); size_t asize = avalue->size();
avalue.resize(size); avalue->resize(size);
} }
} }
} }
...@@ -176,6 +241,8 @@ private: ...@@ -176,6 +241,8 @@ private:
tp = VT_void; tp = VT_void;
svalue = 0; svalue = 0;
ivalue = 0; ivalue = 0;
uvalue = 0;
avalue = 0;
} }
ValueType tp; ValueType tp;
...@@ -185,9 +252,9 @@ private: ...@@ -185,9 +252,9 @@ private:
Char cvalue; Char cvalue;
bool bvalue; bool bvalue;
}; };
Record uvalue; Record * uvalue;
String * svalue; String * svalue;
std::vector<class AnyValue> avalue; std::vector<class AnyValue> * avalue;
}; };
...@@ -224,6 +291,19 @@ public: ...@@ -224,6 +291,19 @@ public:
} }
inline explicit Variable(Variable * ref) { create(); m_reference = ref; } inline explicit Variable(Variable * ref) { create(); m_reference = ref; }
inline explicit Variable(const AnyValue & v) { create(); e_baseType = v.type(); m_value = v; } inline explicit Variable(const AnyValue & v) { create(); e_baseType = v.type(); m_value = v; }
// inline Variable(const Variable & other) {
// create();
// e_baseType = other.e_baseType;
// i_dimension = other.i_dimension;
// memcpy(l_bounds, other.l_bounds, sizeof(l_bounds));
// memcpy(l_restrictedBounds, other.l_restrictedBounds, sizeof(l_restrictedBounds));
// memcpy(l_referenceIndeces, other.l_referenceIndeces, sizeof(l_referenceIndeces));
// m_reference = other.m_reference;
// b_constant = other.b_constant;
// s_name = other.s_name;
// s_algorhitmName = other.s_algorhitmName;
// m_value = other.m_value;
// }
inline bool isValid() const { return e_baseType!=VT_void; } inline bool isValid() const { return e_baseType!=VT_void; }
...@@ -275,9 +355,9 @@ public: ...@@ -275,9 +355,9 @@ public:
inline AnyValue value(int indeces[4]) const; inline AnyValue value(int indeces[4]) const;
inline size_t rawSize() const { return m_value.rawSize(); } inline size_t rawSize() const { return m_value.rawSize(); }
inline const AnyValue & at(size_t index) const { return m_value.avalue[index]; } inline const AnyValue & at(size_t index) const { return m_value.avalue->at(index); }
inline const AnyValue & operator[](size_t index) const { return at(index); } inline const AnyValue & operator[](size_t index) const { return at(index); }
inline AnyValue & at(size_t index) { return m_value.avalue[index]; } inline AnyValue & at(size_t index) { return m_value.avalue->at(index); }
inline AnyValue & operator[](size_t index) { return at(index); } inline AnyValue & operator[](size_t index) { return at(index); }
inline bool isReference() const { return m_reference!=0; } inline bool isReference() const { return m_reference!=0; }
......
...@@ -435,7 +435,6 @@ int main(int argc, char *argv[]) ...@@ -435,7 +435,6 @@ int main(int argc, char *argv[])
// Prepare runner // Prepare runner
VM::KumirVM vm; VM::KumirVM vm;
vm.setDebugOff(false);
InteractionHandler interactionHandler(argc, argv); InteractionHandler interactionHandler(argc, argv);
vm.setExternalHandler(&interactionHandler); vm.setExternalHandler(&interactionHandler);
...@@ -468,6 +467,7 @@ int main(int argc, char *argv[]) ...@@ -468,6 +467,7 @@ int main(int argc, char *argv[])
} }
vm.reset(); vm.reset();
vm.setDebugOff(true);
// Main loop // Main loop
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment