Merge pull request #1136 from colobot/dev-cbot-optimizations

Some CBot code optimizations
1008-fix
krzys_h 2018-05-07 20:53:52 +02:00 committed by GitHub
commit 5283865a5c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 42 additions and 26 deletions

View File

@ -192,7 +192,7 @@ bool CBotClass::AddItem(CBotVar* pVar)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string CBotClass::GetName() const std::string& CBotClass::GetName()
{ {
return m_name; return m_name;
} }

View File

@ -173,7 +173,7 @@ public:
* \brief GetName Gives the name of the class. * \brief GetName Gives the name of the class.
* \return * \return
*/ */
std::string GetName(); const std::string& GetName();
/*! /*!
* \brief GetParent Gives the parent class (or nullptr). * \brief GetParent Gives the parent class (or nullptr).

View File

@ -996,7 +996,7 @@ bool CBotFunction::CheckParam(CBotDefParam* pParam)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string CBotFunction::GetName() const std::string& CBotFunction::GetName()
{ {
return m_token.GetString(); return m_token.GetString();
} }

View File

@ -207,7 +207,7 @@ public:
* \brief GetName * \brief GetName
* \return * \return
*/ */
std::string GetName(); const std::string& GetName();
/*! /*!
* \brief GetParams * \brief GetParams

View File

@ -21,13 +21,22 @@
#include <cstdarg> #include <cstdarg>
#include <cassert> #include <cassert>
#include <boost/bimap.hpp>
namespace CBot namespace CBot
{ {
namespace {
template <typename L, typename R>
boost::bimap<L, R> makeBimap(std::initializer_list<typename boost::bimap<L, R>::value_type> list)
{
return boost::bimap<L, R>(list.begin(), list.end());
}
}
//! \brief Keeps the string corresponding to keyword ID //! \brief Keeps the string corresponding to keyword ID
//! Map is filled with id-string pars that are needed for CBot language parsing //! Map is filled with id-string pars that are needed for CBot language parsing
static const std::map<TokenId, const std::string> KEYWORDS = { static const boost::bimap<TokenId, std::string> KEYWORDS = makeBimap<TokenId, std::string>({
{ID_IF, "if"}, {ID_IF, "if"},
{ID_ELSE, "else"}, {ID_ELSE, "else"},
{ID_WHILE, "while"}, {ID_WHILE, "while"},
@ -115,7 +124,7 @@ static const std::map<TokenId, const std::string> KEYWORDS = {
{ID_ASSMODULO, "%="}, {ID_ASSMODULO, "%="},
{TX_UNDEF, "undefined"}, {TX_UNDEF, "undefined"},
{TX_NAN, "not a number"} {TX_NAN, "not a number"}
}; });
namespace namespace
{ {
@ -123,9 +132,10 @@ static const std::string emptyString = "";
} }
const std::string& LoadString(TokenId id) 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 else
{ {
@ -210,7 +220,7 @@ long CBotToken::GetKeywordId()
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string CBotToken::GetString() const std::string& CBotToken::GetString()
{ {
return m_text; 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,:()[]{}-+*/=;><!~^|&%.\"\'?"; static char sep1[] = " \r\n\t,:()[]{}-+*/=;><!~^|&%.\"\'?";
static char sep2[] = " \r\n\t"; // only separators static char sep2[] = " \r\n\t"; // only separators
static char sep3[] = ",:()[]{}-+*/=;<>!~^|&%.?"; // operational separators static char sep3[] = ",:()[]{}-+*/=;<>!~^|&%.?"; // operational separators
@ -438,10 +449,10 @@ std::unique_ptr<CBotToken> CBotToken::CompileTokens(const std::string& program)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
int CBotToken::GetKeyWord(const std::string& w) int CBotToken::GetKeyWord(const std::string& w)
{ {
for (const auto& it : KEYWORDS) auto it = KEYWORDS.right.find(w);
{ if (it != KEYWORDS.right.end()) {
if (it.second == w) return it.first; return it->second;
} }
return -1; return -1;
} }

View File

@ -120,7 +120,7 @@ public:
* \brief Return the token string * \brief Return the token string
* \return The string associated with this token * \return The string associated with this token
*/ */
std::string GetString(); const std::string& GetString();
/** /**
* \brief Set the token string * \brief Set the token string

View File

@ -49,7 +49,7 @@ namespace CBot
long CBotVar::m_identcpt = 0; long CBotVar::m_identcpt = 0;
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotVar::CBotVar( ) CBotVar::CBotVar( ) : m_token(nullptr)
{ {
m_pMyThis = nullptr; m_pMyThis = nullptr;
m_pUserPtr = nullptr; m_pUserPtr = nullptr;
@ -62,9 +62,17 @@ CBotVar::CBotVar( )
m_mPrivate = ProtectionLevel::Public; m_mPrivate = ProtectionLevel::Public;
} }
CBotVar::CBotVar(const CBotToken &name) : CBotVar() CBotVar::CBotVar(const CBotToken &name) : m_token(new CBotToken(name))
{ {
m_token = new CBotToken(name); m_pMyThis = nullptr;
m_pUserPtr = nullptr;
m_InitExpr = nullptr;
m_LimExpr = nullptr;
m_type = -1;
m_binit = InitType::UNDEF;
m_ident = 0;
m_bStatic = false;
m_mPrivate = ProtectionLevel::Public;
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
@ -378,7 +386,7 @@ void CBotVar::SetInit(CBotVar::InitType initType)
} }
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
std::string CBotVar::GetName() const std::string& CBotVar::GetName()
{ {
return m_token->GetString(); return m_token->GetString();
} }

View File

@ -170,7 +170,7 @@ public:
* \brief Returns the name of the variable * \brief Returns the name of the variable
* \return The name of the variable, empty string if unknown * \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 * \brief SetName Changes the name of the variable
@ -650,7 +650,7 @@ public:
protected: protected:
//! The corresponding token, defines the variable name //! The corresponding token, defines the variable name
CBotToken* m_token; CBotToken* const m_token;
//! Type of value. //! Type of value.
CBotTypResult m_type; CBotTypResult m_type;
//! Initialization status //! Initialization status

View File

@ -31,12 +31,11 @@ namespace CBot
{ {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotVarArray::CBotVarArray(const CBotToken& name, CBotTypResult& type) CBotVarArray::CBotVarArray(const CBotToken& name, CBotTypResult& type) : CBotVar(name)
{ {
if ( !type.Eq(CBotTypArrayPointer) && if ( !type.Eq(CBotTypArrayPointer) &&
!type.Eq(CBotTypArrayBody)) assert(0); !type.Eq(CBotTypArrayBody)) assert(0);
m_token = new CBotToken(name);
m_next = nullptr; m_next = nullptr;
m_pMyThis = nullptr; m_pMyThis = nullptr;
m_pUserPtr = nullptr; m_pUserPtr = nullptr;

View File

@ -36,7 +36,7 @@ namespace CBot
std::set<CBotVarClass*> CBotVarClass::m_instances{}; std::set<CBotVarClass*> CBotVarClass::m_instances{};
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotVarClass::CBotVarClass(const CBotToken& name, const CBotTypResult& type) CBotVarClass::CBotVarClass(const CBotToken& name, const CBotTypResult& type) : CBotVar(name)
{ {
if ( !type.Eq(CBotTypClass) && if ( !type.Eq(CBotTypClass) &&
!type.Eq(CBotTypIntrinsic) && // by convenience there accepts these types !type.Eq(CBotTypIntrinsic) && // by convenience there accepts these types
@ -44,7 +44,6 @@ CBotVarClass::CBotVarClass(const CBotToken& name, const CBotTypResult& type)
!type.Eq(CBotTypArrayPointer) && !type.Eq(CBotTypArrayPointer) &&
!type.Eq(CBotTypArrayBody)) assert(0); !type.Eq(CBotTypArrayBody)) assert(0);
m_token = new CBotToken(name);
m_next = nullptr; m_next = nullptr;
m_pMyThis = nullptr; m_pMyThis = nullptr;
m_pUserPtr = OBJECTCREATED;//nullptr; m_pUserPtr = OBJECTCREATED;//nullptr;

View File

@ -32,14 +32,13 @@ namespace CBot
{ {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CBotVarPointer::CBotVarPointer(const CBotToken& name, CBotTypResult& type) CBotVarPointer::CBotVarPointer(const CBotToken& name, CBotTypResult& type) : CBotVar(name)
{ {
if ( !type.Eq(CBotTypPointer) && if ( !type.Eq(CBotTypPointer) &&
!type.Eq(CBotTypNullPointer) && !type.Eq(CBotTypNullPointer) &&
!type.Eq(CBotTypClass) && // for convenience accepts Class and Intrinsic !type.Eq(CBotTypClass) && // for convenience accepts Class and Intrinsic
!type.Eq(CBotTypIntrinsic) ) assert(0); !type.Eq(CBotTypIntrinsic) ) assert(0);
m_token = new CBotToken(name);
m_next = nullptr; m_next = nullptr;
m_pMyThis = nullptr; m_pMyThis = nullptr;
m_pUserPtr = nullptr; m_pUserPtr = nullptr;