stringstream() performance in multi-thread
hongguangxu opened this issue · 4 comments
Hi, this hjson is very useful, I appreciated for your work. In my project I need to do unMarshal in multi-thread, but they are blocking, I found stringstream() in below two functions is not thread-safety, so I hope this will be helpful for those guys use hjson in multi-threads.
static bool _parseFloat(double *pNumber, const std::string &str) {
std::stringstream ss(str);
// Make sure we expect dot (not comma) as decimal point.
ss.imbue(std::locale::classic());
ss >> *pNumber;
return ss.eof() && !ss.fail() && !std::isinf(*pNumber) && !std::isnan(*pNumber);
}
static bool _parseInt(std::int64_t *pNumber, const std::string &str) {
std::stringstream ss(str);
ss >> *pNumber;
return ss.eof() && !ss.fail();
}
After I fixed it in multi-threads it works fine, and speed it up.
Hi hongguangxu, thank you for letting me know, I was not aware of that problem! I will try to replace std::stringstream
with something faster.
I remember now that I chose to use std::stringstream
because std::strtod
depends on the current C locale, as mentioned in the comment. I cannot change the C locale from within Hjson because that would affect the entire application. The only viable option to std::stringstream
seems to be including a custom implementation of float parsing in Hjson. I'll have to think about that.
Hi trobro, thanks for your reply. As you mentioned, I just replaced the float parsing function with my own one which only works fine in my project. Of course, if there is a better option I will have a try. Thanks!