Store Token::_originalName on heap, since it is only used for a small amount of Tokens (reduce memory usage of TokenList by ~12% (x64))

This commit is contained in:
PKEuS 2014-06-26 10:57:39 +02:00
parent ec1bd420a7
commit 7ea7ee0005
3 changed files with 22 additions and 11 deletions

View File

@ -39,7 +39,6 @@ Token::Token(Token **t) :
_link(0),
_scope(0),
_function(0), // Initialize whole union
_str(""),
_varId(0),
_fileIndex(0),
_linenr(0),
@ -48,13 +47,14 @@ Token::Token(Token **t) :
_flags(0),
_astOperand1(nullptr),
_astOperand2(nullptr),
_astParent(nullptr)
_astParent(nullptr),
_originalName(nullptr)
{
}
Token::~Token()
{
delete _originalName;
}
void Token::update_property_info()
@ -174,7 +174,10 @@ void Token::deleteThis()
_scope = _next->_scope;
_function = _next->_function;
_variable = _next->_variable;
_originalName = _next->_originalName;
if (_next->_originalName) {
_originalName = _next->_originalName;
_next->_originalName = nullptr;
}
values = _next->values;
if (_link)
_link->link(this);
@ -191,7 +194,10 @@ void Token::deleteThis()
_scope = _previous->_scope;
_function = _previous->_function;
_variable = _previous->_variable;
_originalName = _previous->_originalName;
if (_previous->_originalName) {
_originalName = _previous->_originalName;
_previous->_originalName = nullptr;
}
values = _previous->values;
if (_link)
_link->link(this);
@ -931,7 +937,8 @@ void Token::insertToken(const std::string &tokenStr, const std::string &original
else
newToken = new Token(tokensBack);
newToken->str(tokenStr);
newToken->_originalName = originalNameStr;
if (!originalNameStr.empty())
newToken->originalName(originalNameStr);
newToken->_linenr = _linenr;
newToken->_fileIndex = _fileIndex;
newToken->_progressValue = _progressValue;

View File

@ -620,7 +620,7 @@ public:
* @return the original name.
*/
const std::string & originalName() const {
return _originalName;
return _originalName ? *_originalName : emptyString;
}
/**
@ -628,7 +628,10 @@ public:
*/
template<typename T>
void originalName(T&& name) {
_originalName = name;
if (!_originalName)
_originalName = new std::string(name);
else
*_originalName = name;
}
/** Values of token */
@ -687,6 +690,7 @@ private:
*/
static int firstWordLen(const char *str);
std::string _str;
Token *_next;
Token *_previous;
@ -699,7 +703,6 @@ private:
const Variable *_variable;
};
std::string _str;
unsigned int _varId;
unsigned int _fileIndex;
unsigned int _linenr;
@ -761,7 +764,7 @@ private:
Token *_astParent;
// original name like size_t
std::string _originalName;
std::string* _originalName;
public:
void astOperand1(Token *tok);

View File

@ -165,7 +165,8 @@ void TokenList::addtoken(const Token * tok, const unsigned int lineno, const uns
_front = new Token(&_back);
_back = _front;
_back->str(tok->str());
_back->originalName(tok->originalName());
if (!tok->originalName().empty())
_back->originalName(tok->originalName());
}
_back->linenr(lineno);