arturocepeda / Cflat

Embeddable lightweight scripting language with C++ syntax
49 stars 8 forks source link

Performance/Cleanliness Improvement - std::string_view #3

Closed stephenberry closed 2 years ago

stephenberry commented 2 years ago

I noted in my first issue concerning macros my desire and recommendation to move to C++17 for cleaner and faster code. One motivation is the inclusion of std::string_view. It's a simple class, but using it instead of const char* in some places it will bring significant performance improvements.

Using std::string_view will allow you to avoid calling strlen in a lot of places, and instead directly get the size. For example, mName in Identifier should be a string_view. strlen has to iterate through the characters of the string, making it expensive to call constantly, like in findFirstSeparator.

You could obviously write your own simple string_view class to maintain C++11 compliance, but there are a number of additional reasons to move to C++17, and I think it's best to aim to keep the code base as simple and minimal as possible by using the standard library.

arturocepeda commented 2 years ago

You are totally right, the calls to strlen could be easily avoided! However, as mentioned in the previous issue, I would rather add a member to Identifier to store the string size (mNameSize?), at least for the time being.

arturocepeda commented 2 years ago

Storing the string size in identifiers.