From 3b344d6d721ecc45a96da00d2189c0d73f62d70b Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 28 Jul 2019 12:33:14 +0200 Subject: [PATCH] Improve performance of cloning StringEvalValue --- src/Evaluator/EvalValues/StringEvalValue.hpp | 21 ++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Evaluator/EvalValues/StringEvalValue.hpp b/src/Evaluator/EvalValues/StringEvalValue.hpp index 77857e9..41d28ee 100644 --- a/src/Evaluator/EvalValues/StringEvalValue.hpp +++ b/src/Evaluator/EvalValues/StringEvalValue.hpp @@ -10,12 +10,16 @@ using namespace std; namespace Porygon::Evaluation { class StringEvalValue : public EvalValue { - u16string _value; + shared_ptr _value; size_t _hash; - public: - explicit StringEvalValue(u16string s) : _value(move(s)){ - _hash = Utilities::HashedString::ConstHash(_value.c_str()); + explicit StringEvalValue(shared_ptr s) : _value(move(s)){ + _hash = Utilities::HashedString::ConstHash(_value->c_str()); } + public: + explicit StringEvalValue(const u16string& s) : _value(make_shared(s)){ + _hash = Utilities::HashedString::ConstHash(_value->c_str()); + } + [[nodiscard]] inline TypeClass GetTypeClass() const final { @@ -30,7 +34,7 @@ namespace Porygon::Evaluation { [[nodiscard]] inline u16string EvaluateString() const final { - return _value; + return *_value.get(); } [[nodiscard]] @@ -41,7 +45,7 @@ namespace Porygon::Evaluation { EvalValue* IndexValue(const EvalValue *val) const final { // Porygon is 1-indexed, so we convert to that. auto l = val->EvaluateInteger() - 1; - return new StringEvalValue(u16string(1, _value[l])); + return new StringEvalValue(u16string(1, (*_value)[l])); } [[nodiscard]] @@ -55,9 +59,10 @@ namespace Porygon::Evaluation { throw EvaluationException("Binary operation not supported for strings."); } std::basic_ostringstream stringStream; - stringStream << _value; + stringStream << *_value; stringStream << b->EvaluateString(); - return new StringEvalValue(stringStream.str()); + auto str = stringStream.str(); + return new StringEvalValue(str); } }; }