Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

const std::string #5

Closed
JamesBoer opened this issue Mar 3, 2021 · 1 comment
Closed

const std::string #5

JamesBoer opened this issue Mar 3, 2021 · 1 comment

Comments

@JamesBoer
Copy link

There are several places in code you're using const std::string to define whitespace and end-of-line strings. There are a couple of issues with this.

First, these aren't inline, meaning that an instance is created for every translation unit.

Second, because you're using std::string, this performs an unnecessary allocation in debug mode (it disappears in release due to SSO). This is causing me problems with my leak detection, since the deallocation order isn't guaranteed, and is occurring after my leak check exit point.

It would be best to simply replace these several instances with inline const char* This will avoid any allocations, and works exactly like before.

inline const char* whitespaceDelimiters = " \t\n\r\f\v";
...
#ifdef _WIN32
    inline const char* endl = "\r\n";
#else
    inline const char* endl = "\n";
#endif
@metayeti
Copy link
Owner

metayeti commented Mar 3, 2021

Thanks for bringing this to my attention, I changed it to const char* const in the latest release. My compiler had issues with inline - I think it should be constexpr for inline. Either way this should optimize allocations.

@metayeti metayeti closed this as completed Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants