Improve performance of cloning StringEvalValue
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2019-07-28 12:33:14 +02:00
parent 1f3ab9243e
commit 3b344d6d72
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
1 changed files with 13 additions and 8 deletions

View File

@ -10,12 +10,16 @@ using namespace std;
namespace Porygon::Evaluation { namespace Porygon::Evaluation {
class StringEvalValue : public EvalValue { class StringEvalValue : public EvalValue {
u16string _value; shared_ptr<u16string> _value;
size_t _hash; size_t _hash;
public: explicit StringEvalValue(shared_ptr<u16string> s) : _value(move(s)){
explicit StringEvalValue(u16string s) : _value(move(s)){ _hash = Utilities::HashedString::ConstHash(_value->c_str());
_hash = Utilities::HashedString::ConstHash(_value.c_str());
} }
public:
explicit StringEvalValue(const u16string& s) : _value(make_shared<u16string>(s)){
_hash = Utilities::HashedString::ConstHash(_value->c_str());
}
[[nodiscard]] [[nodiscard]]
inline TypeClass GetTypeClass() const final { inline TypeClass GetTypeClass() const final {
@ -30,7 +34,7 @@ namespace Porygon::Evaluation {
[[nodiscard]] [[nodiscard]]
inline u16string EvaluateString() const final { inline u16string EvaluateString() const final {
return _value; return *_value.get();
} }
[[nodiscard]] [[nodiscard]]
@ -41,7 +45,7 @@ namespace Porygon::Evaluation {
EvalValue* IndexValue(const EvalValue *val) const final { EvalValue* IndexValue(const EvalValue *val) const final {
// Porygon is 1-indexed, so we convert to that. // Porygon is 1-indexed, so we convert to that.
auto l = val->EvaluateInteger() - 1; auto l = val->EvaluateInteger() - 1;
return new StringEvalValue(u16string(1, _value[l])); return new StringEvalValue(u16string(1, (*_value)[l]));
} }
[[nodiscard]] [[nodiscard]]
@ -55,9 +59,10 @@ namespace Porygon::Evaluation {
throw EvaluationException("Binary operation not supported for strings."); throw EvaluationException("Binary operation not supported for strings.");
} }
std::basic_ostringstream<char16_t> stringStream; std::basic_ostringstream<char16_t> stringStream;
stringStream << _value; stringStream << *_value;
stringStream << b->EvaluateString(); stringStream << b->EvaluateString();
return new StringEvalValue(stringStream.str()); auto str = stringStream.str();
return new StringEvalValue(str);
} }
}; };
} }