From 477dc0cae7fc41ca0d3b0616839774b3ec4bd3df Mon Sep 17 00:00:00 2001 From: krzys-h Date: Fri, 6 Apr 2018 15:02:06 +0200 Subject: [PATCH] Some CBot code optimizations --- src/CBot/CBotClass.cpp | 2 +- src/CBot/CBotClass.h | 2 +- src/CBot/CBotInstr/CBotFunction.cpp | 2 +- src/CBot/CBotInstr/CBotFunction.h | 2 +- src/CBot/CBotToken.cpp | 29 ++++++++++++++++++++--------- src/CBot/CBotToken.h | 2 +- src/CBot/CBotVar/CBotVar.cpp | 2 +- src/CBot/CBotVar/CBotVar.h | 2 +- 8 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index ef220943..599155d0 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -192,7 +192,7 @@ bool CBotClass::AddItem(CBotVar* pVar) } //////////////////////////////////////////////////////////////////////////////// -std::string CBotClass::GetName() +const std::string& CBotClass::GetName() { return m_name; } diff --git a/src/CBot/CBotClass.h b/src/CBot/CBotClass.h index 503c62db..f3a0c83f 100644 --- a/src/CBot/CBotClass.h +++ b/src/CBot/CBotClass.h @@ -173,7 +173,7 @@ public: * \brief GetName Gives the name of the class. * \return */ - std::string GetName(); + const std::string& GetName(); /*! * \brief GetParent Gives the parent class (or nullptr). diff --git a/src/CBot/CBotInstr/CBotFunction.cpp b/src/CBot/CBotInstr/CBotFunction.cpp index 13b9005b..7f1ac168 100644 --- a/src/CBot/CBotInstr/CBotFunction.cpp +++ b/src/CBot/CBotInstr/CBotFunction.cpp @@ -996,7 +996,7 @@ bool CBotFunction::CheckParam(CBotDefParam* pParam) } //////////////////////////////////////////////////////////////////////////////// -std::string CBotFunction::GetName() +const std::string& CBotFunction::GetName() { return m_token.GetString(); } diff --git a/src/CBot/CBotInstr/CBotFunction.h b/src/CBot/CBotInstr/CBotFunction.h index 1f25474f..206deacf 100644 --- a/src/CBot/CBotInstr/CBotFunction.h +++ b/src/CBot/CBotInstr/CBotFunction.h @@ -207,7 +207,7 @@ public: * \brief GetName * \return */ - std::string GetName(); + const std::string& GetName(); /*! * \brief GetParams diff --git a/src/CBot/CBotToken.cpp b/src/CBot/CBotToken.cpp index 3c4bc7d9..6c7a5833 100644 --- a/src/CBot/CBotToken.cpp +++ b/src/CBot/CBotToken.cpp @@ -21,13 +21,22 @@ #include #include +#include namespace CBot { +namespace { + template + boost::bimap makeBimap(std::initializer_list::value_type> list) + { + return boost::bimap(list.begin(), list.end()); + } +} + //! \brief Keeps the string corresponding to keyword ID //! Map is filled with id-string pars that are needed for CBot language parsing -static const std::map KEYWORDS = { +static const boost::bimap KEYWORDS = makeBimap({ {ID_IF, "if"}, {ID_ELSE, "else"}, {ID_WHILE, "while"}, @@ -115,7 +124,7 @@ static const std::map KEYWORDS = { {ID_ASSMODULO, "%="}, {TX_UNDEF, "undefined"}, {TX_NAN, "not a number"} -}; +}); namespace { @@ -123,9 +132,10 @@ static const std::string emptyString = ""; } const std::string& LoadString(TokenId id) { - if (KEYWORDS.find(id) != KEYWORDS.end()) + auto it = KEYWORDS.left.find(id); + if (it != KEYWORDS.left.end()) { - return KEYWORDS.at(id); + return it->second; } else { @@ -210,7 +220,7 @@ long CBotToken::GetKeywordId() } //////////////////////////////////////////////////////////////////////////////// -std::string CBotToken::GetString() +const std::string& CBotToken::GetString() { return m_text; } @@ -242,6 +252,7 @@ void CBotToken::SetPos(int start, int end) //////////////////////////////////////////////////////////////////////////////// +// TODO: CharInList could probably be optimized to conditions like (*c >= '0' && *c <= '9') to gain some performance static char sep1[] = " \r\n\t,:()[]{}-+*/=;>!~^|&%.?"; // operational separators @@ -438,10 +449,10 @@ std::unique_ptr CBotToken::CompileTokens(const std::string& program) //////////////////////////////////////////////////////////////////////////////// int CBotToken::GetKeyWord(const std::string& w) { - for (const auto& it : KEYWORDS) - { - if (it.second == w) return it.first; - } + auto it = KEYWORDS.right.find(w); + if (it != KEYWORDS.right.end()) { + return it->second; + } return -1; } diff --git a/src/CBot/CBotToken.h b/src/CBot/CBotToken.h index a9254434..63c9eb5b 100644 --- a/src/CBot/CBotToken.h +++ b/src/CBot/CBotToken.h @@ -120,7 +120,7 @@ public: * \brief Return the token string * \return The string associated with this token */ - std::string GetString(); + const std::string& GetString(); /** * \brief Set the token string diff --git a/src/CBot/CBotVar/CBotVar.cpp b/src/CBot/CBotVar/CBotVar.cpp index 7ea659f5..836b2a7d 100644 --- a/src/CBot/CBotVar/CBotVar.cpp +++ b/src/CBot/CBotVar/CBotVar.cpp @@ -378,7 +378,7 @@ void CBotVar::SetInit(CBotVar::InitType initType) } //////////////////////////////////////////////////////////////////////////////// -std::string CBotVar::GetName() +const std::string& CBotVar::GetName() { return m_token->GetString(); } diff --git a/src/CBot/CBotVar/CBotVar.h b/src/CBot/CBotVar/CBotVar.h index 7babb7d1..1a2bff76 100644 --- a/src/CBot/CBotVar/CBotVar.h +++ b/src/CBot/CBotVar/CBotVar.h @@ -170,7 +170,7 @@ public: * \brief Returns the name of the variable * \return The name of the variable, empty string if unknown */ - std::string GetName(); + const std::string& GetName(); /** * \brief SetName Changes the name of the variable