lazy evaluation of lluistring to improve LLUIString::format performance

reviewed by Mani
meow-7.2.2
richard 2009-12-09 17:17:07 -08:00
parent 7466aa198a
commit 23610472d5
2 changed files with 55 additions and 28 deletions

View File

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

View File

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