Change internals of lluicolortable to hold a const char* and not a string. this way we can avoid a lot of string creating and destructions per frame.

Nicky 2013-01-02 20:59:08 +01:00
parent a8481122d5
commit 2ea3aad28b
2 changed files with 102 additions and 14 deletions

View File

@ -131,7 +131,14 @@ void LLUIColorTable::insertFromParams(const Params& p, string_color_map_t& table
{
// since this reference does not refer to another reference it must refer to an
// actual color, lets find it...
string_color_map_t::iterator color_value = mLoadedColors.find(previous->second);
// string_color_map_t::iterator color_value = mLoadedColors.find(previous->second);
ColorName oName;
oName.nLen = previous->second.size();
oName.pName = const_cast<char*>(previous->second.c_str()); // That's ok, I won't hurt you.
string_color_map_t::iterator color_value = mLoadedColors.find(oName);
if(color_value != mLoadedColors.end())
{
@ -178,14 +185,33 @@ void LLUIColorTable::clear()
LLUIColor LLUIColorTable::getColor(const std::string& name, const LLColor4& default_color) const
{
string_color_map_t::const_iterator iter = mUserSetColors.find(name);
return getColor( name.c_str(), default_color );
}
LLUIColor LLUIColorTable::getColor( char const *name, const LLColor4& default_color) const // <FS:ND> Change from std::string to char*, avoind lots of unecessary string constructions
{
// <FS:ND> Change from std::string to char*, avoind lots of unecessary string constructions
// string_color_map_t::const_iterator iter = mUserSetColors.find(name);
ColorName oName;
oName.nLen = strlen( name );
oName.pName = const_cast<char*>(name);
string_color_map_t::const_iterator iter = mUserSetColors.find(oName);
// </FS:ND>
if(iter != mUserSetColors.end())
{
return LLUIColor(&iter->second);
}
iter = mLoadedColors.find(name);
// <FS:ND> Change from std::string to char*, avoind lots of unecessary string constructions
// iter = mLoadedColors.find(name);
iter = mLoadedColors.find(oName);
// </FS:ND>
if(iter != mLoadedColors.end())
{
@ -238,7 +264,10 @@ void LLUIColorTable::saveUserSettings() const
++it)
{
ColorEntryParams color_entry;
color_entry.name = it->first;
// color_entry.name = it->first;
color_entry.name = it->first.pName;
color_entry.color.value = it->second;
params.color_entries.add(color_entry);
@ -272,7 +301,10 @@ void LLUIColorTable::saveUserSettingsPaletteOnly() const
++it)
{
ColorEntryParams color_entry;
color_entry.name = it->first;
// color_entry.name = it->first;
color_entry.name = it->first.pName;
color_entry.color.value = it->second;
@ -300,10 +332,21 @@ void LLUIColorTable::saveUserSettingsPaletteOnly() const
}
}
bool LLUIColorTable::colorExists(const std::string& color_name) const
// bool LLUIColorTable::colorExists(const std::string& color_name) const
bool LLUIColorTable::colorExists( char const *name ) const
{
return ((mLoadedColors.find(color_name) != mLoadedColors.end())
|| (mUserSetColors.find(color_name) != mUserSetColors.end()));
// <FS:ND> Change from std::string to char*, avoind lots of unecessary string constructions
// return ((mLoadedColors.find(color_name) != mLoadedColors.end())
// || (mUserSetColors.find(color_name) != mUserSetColors.end()));
ColorName oName;
oName.nLen = strlen( name );
oName.pName = const_cast<char*>(name);
return ((mLoadedColors.find(oName) != mLoadedColors.end())
|| (mUserSetColors.find(oName) != mUserSetColors.end()));
// </FS:ND>
}
void LLUIColorTable::clearTable(string_color_map_t& table)
@ -320,15 +363,27 @@ void LLUIColorTable::clearTable(string_color_map_t& table)
// if the color already exists it changes the color
void LLUIColorTable::setColor(const std::string& name, const LLColor4& color, string_color_map_t& table)
{
string_color_map_t::iterator it = table.lower_bound(name);
if(it != table.end()
&& !(table.key_comp()(name, it->first)))
// <FS:ND> Change from std::string to char*, avoind lots of unecessary string constructions
// string_color_map_t::iterator it = table.lower_bound(name);
ColorName oName;
oName.nLen = name.size();
oName.pName = const_cast<char*>( name.c_str() );
string_color_map_t::iterator it = table.find(oName);
// if(it != table.end() && !(table.key_comp()(name, it->first)))
if(it != table.end() )
// </FS:ND>
{
it->second = color;
}
else
{
table.insert(it, string_color_map_t::value_type(name, color));
oName.pName = strdup( oName.pName );
table.insert(string_color_map_t::value_type(oName, color));
}
}

View File

@ -41,7 +41,29 @@ class LLUIColorTable : public LLSingleton<LLUIColorTable>
LOG_CLASS(LLUIColorTable);
// consider using sorted vector, can be much faster
typedef std::map<std::string, LLUIColor> string_color_map_t;
// <FS:ND> Change from std::string to char*, avoind lots of unecessary string constructions
// typedef std::map<std::string, LLUIColor> string_color_map_t;
struct ColorName
{
char *pName;
int nLen;
bool operator<( ColorName const &aRHS ) const
{
if( nLen == aRHS.nLen )
return strcmp( pName, aRHS.pName ) < 0;
return nLen < aRHS.nLen;
}
};
typedef std::map<ColorName, LLUIColor> string_color_map_t;
// </FS:ND>
public:
struct ColorParams : LLInitParam::ChoiceBlock<ColorParams>
@ -74,13 +96,24 @@ public:
void clear();
// color lookup
LLUIColor getColor(const std::string& name, const LLColor4& default_color = LLColor4::magenta) const;
// <FS:ND> Change from std::string to char*, avoind lots of unecessary string constructions
LLUIColor getColor(char const *name, const LLColor4& default_color = LLColor4::magenta) const;
// </FS:ND>
// if the color is in the table, it's value is changed, otherwise it is added
void setColor(const std::string& name, const LLColor4& color);
// returns true if color_name exists in the table
bool colorExists(const std::string& color_name) const;
// <FS:ND> Change from std::string to char*, avoind lots of unecessary string constructions
// bool colorExists(const std::string& color_name) const;
bool colorExists(char const *name) const;
// </FS:ND>
// loads colors from settings files
bool loadFromSettings();