Replacing boost::format with fmt

Everything about development and the OpenMW source code.
Post Reply
User avatar
psi29a
Posts: 3452
Joined: 29 Sep 2011, 10:13
Github profile: https://github.com/psi29a/
Contact:

Replacing boost::format with fmt

Post by psi29a » 28 Oct 2017, 08:24

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: 2500
Joined: 07 Aug 2011, 01:12
Location: Caldera

Re: Replacing boost::format with fmt

Post by raevol » 28 Oct 2017, 09:36

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: 3452
Joined: 29 Sep 2011, 10:13
Github profile: https://github.com/psi29a/
Contact:

Re: Replacing boost::format with fmt

Post by psi29a » 28 Oct 2017, 13:29

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: 2085
Joined: 18 Feb 2012, 11:51
Contact:

Re: Replacing boost::format with fmt

Post by scrawl » 28 Oct 2017, 16:53

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: 1299
Joined: 04 Sep 2011, 08:33

Re: Replacing boost::format with fmt

Post by Chris » 28 Oct 2017, 19:49

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

Who is online

Users browsing this forum: No registered users and 3 guests