Replacing boost::format with fmt

Everything about development and the OpenMW source code.
Post Reply
User avatar
psi29a
Posts: 5355
Joined: 29 Sep 2011, 10:13
Location: Belgium
Gitlab profile: https://gitlab.com/psi29a/
Contact:

Replacing boost::format with fmt

Post by psi29a »

In looking to de-boost, I stumbled across boost::string(boost::format( that happens quite a bit in our codebase, it does utf8/wide %s string formatting.

Apparently, thanks to benchmarks, it shows that boost is the slowest of the string formatting options. Introducing FMT, a C++11 library that apparently used by many projects for this specific purpose and is super fast.

https://github.com/fmtlib/fmt

I've already tested it here and have it in a branch, but I wanted to know how you guys felt about it? We can either add it to our 3rd party libs that we carry with us, or add as a dependency for packagers.

Benchmarks:
Library Method Run Time, s
EGLIBC 2.19 printf 1.30
libstdc++ 4.8.2 std::ostream 1.85
fmt 1.0 fmt::print 1.42
tinyformat 2.0.1 tfm::printf 2.25
Boost Format 1.54 boost::format 9.94
User avatar
raevol
Posts: 3093
Joined: 07 Aug 2011, 01:12
Location: Caldera

Re: Replacing boost::format with fmt

Post by raevol »

I'm not sure what I am talking about here, but why not use std::ostream ? Seems only a little slower than fmt (and still way faster than boost), but isn't external? I may be missing something big here though...
User avatar
psi29a
Posts: 5355
Joined: 29 Sep 2011, 10:13
Location: Belgium
Gitlab profile: https://gitlab.com/psi29a/
Contact:

Re: Replacing boost::format with fmt

Post by psi29a »

Pure convenience, instead of having to juggle a string buffer and worry about it... I can just use:
auto result = fmt::sprintf("something %s", 'cool'); // <-- 'something cool' in result that is std::string
^-- very wow, very python ;)

otherwise I would have to do things like described here:
https://stackoverflow.com/questions/369 ... tring-in-c
User avatar
scrawl
Posts: 2152
Joined: 18 Feb 2012, 11:51

Re: Replacing boost::format with fmt

Post by scrawl »

Performance is irrelevant here for a function that's barely used.

Most of current uses seem to be either replacing %s with a given string or just formatting a number (not even surrounded by a string). This should be trivial to implement ourselves. Don't see the need to add a new dependency.
Chris
Posts: 1625
Joined: 04 Sep 2011, 08:33

Re: Replacing boost::format with fmt

Post by Chris »

With C++11 we also have access to std::to_string now, and given rvalue move semantics and small-string optimizations, should be fairly good performance-wise, as far as string manipulation goes. fmt might be something to look at if these things aren't enough, but I'd first try to go through and see what we can do with standard functions first.
Post Reply