diff options
author | richard <none@none> | 2009-12-09 17:17:07 -0800 |
---|---|---|
committer | richard <none@none> | 2009-12-09 17:17:07 -0800 |
commit | 23610472d5e09b66cd05039a9c6cb96664fdbaeb (patch) | |
tree | 29df2d98f71b0708bbacabb6bcea61909c9c1c0f | |
parent | 7466aa198a7707f2acadacfb6709bed8351fff96 (diff) |
lazy evaluation of lluistring to improve LLUIString::format performance
reviewed by Mani
-rw-r--r-- | indra/llui/lluistring.cpp | 51 | ||||
-rw-r--r-- | indra/llui/lluistring.h | 32 |
2 files changed, 55 insertions, 28 deletions
diff --git a/indra/llui/lluistring.cpp b/indra/llui/lluistring.cpp index 3a1e656364..f7a53e87de 100644 --- a/indra/llui/lluistring.cpp +++ b/indra/llui/lluistring.cpp @@ -39,22 +39,23 @@ LLFastTimer::DeclareTimer FTM_UI_STRING("UI String"); LLUIString::LLUIString(const std::string& instring, const LLStringUtil::format_map_t& args) - : mOrig(instring), - mArgs(args) +: mOrig(instring), + mArgs(args) { - format(); + dirty(); } void LLUIString::assign(const std::string& s) { mOrig = s; - format(); + dirty(); } void LLUIString::setArgList(const LLStringUtil::format_map_t& args) + { mArgs = args; - format(); + dirty(); } void LLUIString::setArgs(const LLSD& sd) @@ -68,40 +69,40 @@ void LLUIString::setArgs(const LLSD& sd) { setArg(sd_it->first, sd_it->second.asString()); } - format(); + dirty(); } void LLUIString::setArg(const std::string& key, const std::string& replacement) { mArgs[key] = replacement; - format(); + dirty(); } void LLUIString::truncate(S32 maxchars) { - if (mWResult.size() > (size_t)maxchars) + if (getUpdatedWResult().size() > (size_t)maxchars) { - LLWStringUtil::truncate(mWResult, maxchars); - mResult = wstring_to_utf8str(mWResult); + LLWStringUtil::truncate(getUpdatedWResult(), maxchars); + mResult = wstring_to_utf8str(getUpdatedWResult()); } } void LLUIString::erase(S32 charidx, S32 len) { - mWResult.erase(charidx, len); - mResult = wstring_to_utf8str(mWResult); + getUpdatedWResult().erase(charidx, len); + mResult = wstring_to_utf8str(getUpdatedWResult()); } void LLUIString::insert(S32 charidx, const LLWString& wchars) { - mWResult.insert(charidx, wchars); - mResult = wstring_to_utf8str(mWResult); + getUpdatedWResult().insert(charidx, wchars); + mResult = wstring_to_utf8str(getUpdatedWResult()); } void LLUIString::replace(S32 charidx, llwchar wc) { - mWResult[charidx] = wc; - mResult = wstring_to_utf8str(mWResult); + getUpdatedWResult()[charidx] = wc; + mResult = wstring_to_utf8str(getUpdatedWResult()); } void LLUIString::clear() @@ -112,8 +113,16 @@ void LLUIString::clear() mWResult.clear(); } -void LLUIString::format() +void LLUIString::dirty() { + mNeedsResult = true; + mNeedsWResult = true; +} + +void LLUIString::updateResult() const +{ + mNeedsResult = false; + LLFastTimer timer(FTM_UI_STRING); // optimize for empty strings (don't attempt string replacement) @@ -129,5 +138,11 @@ void LLUIString::format() LLStringUtil::format_map_t combined_args = LLTrans::getDefaultArgs(); combined_args.insert(mArgs.begin(), mArgs.end()); LLStringUtil::format(mResult, combined_args); - mWResult = utf8str_to_wstring(mResult); +} + +void LLUIString::updateWResult() const +{ + mNeedsWResult = false; + + mWResult = utf8str_to_wstring(getUpdatedResult()); } diff --git a/indra/llui/lluistring.h b/indra/llui/lluistring.h index 763de4d6a3..7ec0fd603a 100644 --- a/indra/llui/lluistring.h +++ b/indra/llui/lluistring.h @@ -76,19 +76,19 @@ public: void setArgs(const class LLSD& sd); void setArg(const std::string& key, const std::string& replacement); - const std::string& getString() const { return mResult; } - operator std::string() const { return mResult; } + const std::string& getString() const { return getUpdatedResult(); } + operator std::string() const { return getUpdatedResult(); } - const LLWString& getWString() const { return mWResult; } - operator LLWString() const { return mWResult; } + const LLWString& getWString() const { return getUpdatedWResult(); } + operator LLWString() const { return getUpdatedWResult(); } - bool empty() const { return mWResult.empty(); } - S32 length() const { return mWResult.size(); } + bool empty() const { return getUpdatedResult().empty(); } + S32 length() const { return getUpdatedWResult().size(); } void clear(); void clearArgs() { mArgs.clear(); } - // These utuilty functions are included for text editing. + // These utility functions are included for text editing. // They do not affect mOrig and do not perform argument substitution void truncate(S32 maxchars); void erase(S32 charidx, S32 len); @@ -96,12 +96,24 @@ public: void replace(S32 charidx, llwchar wc); private: - void format(); + // something changed, requiring reformatting of strings + void dirty(); + + std::string& getUpdatedResult() const { if (mNeedsResult) { updateResult(); } return mResult; } + LLWString& getUpdatedWResult() const{ if (mNeedsWResult) { updateWResult(); } return mWResult; } + + // do actual work of updating strings (non-inlined) + void updateResult() const; + void updateWResult() const; std::string mOrig; - std::string mResult; - LLWString mWResult; // for displaying + mutable std::string mResult; + mutable LLWString mWResult; // for displaying LLStringUtil::format_map_t mArgs; + + // controls lazy evaluation + mutable bool mNeedsResult; + mutable bool mNeedsWResult; }; #endif // LL_LLUISTRING_H |