Skip to content

build: Use fmt as a header only library everywhere - #36484

Closed
adamemerson wants to merge 1 commit into
ceph:masterfrom
adamemerson:wip-header-only-fmt
Closed

adamemerson wants to merge 1 commit into
ceph:masterfrom
adamemerson:wip-header-only-fmt

Conversation

@adamemerson

Copy link
Copy Markdown
Contributor

As suggested in a review elsewhere.

@adamemerson
adamemerson requested review from cbodley and tchaikov August 5, 2020 20:53
@adamemerson
adamemerson requested a review from a team as a code owner August 5, 2020 20:53
@tchaikov

tchaikov commented Aug 6, 2020 •

Copy link
Copy Markdown
Contributor

@adamemerson could you elaborate the rationales of this change? i don't like header only libfmt, as it gets more source code pulled into the header, which could have been compiled into the library for saving the compile time. this is also one of the reasons why i packaged fmt for fedora and EPEL8.

@adamemerson

Copy link
Copy Markdown
Contributor Author

@tchaikov It gets around issues I've found (and I think others have, too, considering they were doing it in a few places) with clashing/missing symbols in libraries.

Also according to @wjwithagen it helps portability to BSD.

@adamemerson
adamemerson requested a review from wjwithagen August 26, 2020 20:20
@wjwithagen

Copy link
Copy Markdown
Member

Also according to @wjwithagen it helps portability to BSD.

My general objection was largely with embedding yet another library in het ceph tree, and then have it build in yet again a way that requires fixing it if the system is too Linux specific.

And my specific peeve was more about this change I have for Nautilus:

patches/src_rgw_rgw_lc.cc.diff:-#include "seastar/fmt/include/fmt/format.h"
patches/src_rgw_rgw_lc.cc.diff:+#include "fmt/format.h"

I'll run this full PR in jenkins

@adamemerson

Copy link
Copy Markdown
Contributor Author

@tchaikov It could also give the compiler more opportunities for optimization as each translation unit gets to see the full source of the libformat function's it's calling into.

@tchaikov

Copy link
Copy Markdown
Contributor

@tchaikov It gets around issues I've found (and I think others have, too, considering they were doing it in a few places) with clashing/missing symbols in libraries.

where did you get the "libraries"? are they from fedora repo? and what are exactly the issues ? what are the clashing/missing symbols? if you get them from fedora, could you file a bug on bugzilla and point me to it?

Also according to @wjwithagen it helps portability to BSD.

i'd like to understand more details on this.

@tchaikov It could also give the compiler more opportunities for optimization as each translation unit gets to see the full source of the libformat function's it's calling into.

i'd like to see the profiling results when it comes to performance.

@tchaikov

tchaikov commented Aug 27, 2020 •

Copy link
Copy Markdown
Contributor

Also according to @wjwithagen it helps portability to BSD.

My general objection was largely with embedding yet another library in het ceph tree, and then have it build in yet again a way that requires fixing it if the system is too Linux specific.

And my specific peeve was more about this change I have for Nautilus:

patches/src_rgw_rgw_lc.cc.diff:-#include "seastar/fmt/include/fmt/format.h"
patches/src_rgw_rgw_lc.cc.diff:+#include "fmt/format.h"

this is not related to this chagne. nautilus uses the libfmt submodule in seastar, and in master, seastar removed libfmt, so we are using our own fmt submodule.

@wjwithagen wjwithagen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has passed Jenkins compile and test on FreeBSD/Clang

@wjwithagen

wjwithagen commented Aug 27, 2020 •

Copy link
Copy Markdown
Member

My general objection was largely with embedding yet another library in het ceph tree, and then have it build in yet again a way that requires fixing it if the system is too Linux specific.
And my specific peeve was more about this change I have for Nautilus:

patches/src_rgw_rgw_lc.cc.diff:-#include "seastar/fmt/include/fmt/format.h"
patches/src_rgw_rgw_lc.cc.diff:+#include "fmt/format.h"

this is not related to this chagne. nautilus uses the libfmt submodule in seastar, and in master, seastar removed libfmt, so we are using our own fmt submodule.

My inital reaction was that I liked this change.
Main reason for this is the suggestion of Adam to actually make libfmt less invasive by making it more manageble from cmake, which allows me to just use the packaged standard port. And for that I'll need to patch the CMake files.
You are correct is suggesting that the fixup above is only still needed in Nautilus

Perpendicular to that is the HEADER_ONLY, which makes FreeBSD live easier due to the way libfmt needed to be build in Seastar. I looked at that, and decided to just use the native FreeBSD port. Now if we end up with a src/libfmt/include then that will certainly work out of the box for FreeBSD when it uses HEADER_ONLY and otherwise I might have to look into the buildprocess for the lib.

The point of compilation is that sense is a lesser argument for me: The compiler does its work anyways, the code might even get better by having a larger optimsing surface, versus sinking the scarce time I have in something that is also available as a package.
So I have a small preference for HEADER_ONLY but if that does not happen I'll learn to deal with it

@tchaikov

Copy link
Copy Markdown
Contributor

My general objection was largely with embedding yet another library in het ceph tree, and then have it build in yet again a way that requires fixing it if the system is too Linux specific.
And my specific peeve was more about this change I have for Nautilus:

patches/src_rgw_rgw_lc.cc.diff:-#include "seastar/fmt/include/fmt/format.h"
patches/src_rgw_rgw_lc.cc.diff:+#include "fmt/format.h"

this is not related to this chagne. nautilus uses the libfmt submodule in seastar, and in master, seastar removed libfmt, so we are using our own fmt submodule.

My inital reaction was that I liked this change.
Main reason for this is the suggestion of Adam to actually make libfmt less invasive by making it more manageble from cmake, which allows me to just use the packaged standard port. And for that I'll need to patch the CMake files.
You are correct is suggesting that the fixup above is only still needed in Nautilus

this change has nothing to do with using packaged standard port or not. it's all about using header only library. currently in master, the libfmt installed in system is preferred over the one included as submodule. and again, this is not related to this change. i'd really want to focus on this change instead of starting using this PR as a venue of chitchat.

Perpendicular to that is the HEADER_ONLY, which makes FreeBSD live easier due to the way libfmt needed to be build in Seastar. I looked at that, and decided to just use the native FreeBSD port. Now if we end up with a src/libfmt/include

please note, Seastar does not support FreeBSD yet. i am not sure what you mean by "libfmt needed to be build in Seastar".

then that will certainly work out of the box for FreeBSD when it uses HEADER_ONLY and otherwise I might have to look into the buildprocess for the lib.

i am not sure what problem you ran into so you need to look into the build process of the lib?

The point of compilation is that sense is a lesser argument for me: The compiler does its work anyways, the code might even get better by having a larger optimsing surface, versus sinking the scarce time I have in something that is also available as a package.

i think i've explained my opinion regarding the possible optimisations. without the numbers, i can hardly understand why it matters.

So I have a small preference for HEADER_ONLY but if that does not happen I'll learn to deal with it

@wjwithagen

Copy link
Copy Markdown
Member

this change has nothing to do with using packaged standard port or not. it's all about using header only library. currently in master, the libfmt installed in system is preferred over the one included as submodule. and again, this is not related to this change.

i'd really want to focus on this change instead of starting using this PR as a venue of chitchat.

So I have a small preference for HEADER_ONLY but if that does not happen I'll learn to deal with it

I'll then leave it at that.

Comment thread src/rgw/rgw_lc.cc
// this seems safe to use, at least for now--arguably, we should
// prefer header-only fmt, in general
#undef FMT_HEADER_ONLY
#define FMT_HEADER_ONLY 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason i requested this PR is because we were repeating this stuff in each translation unit that includes fmt. whether or not we decide to go with header-only, this decision belongs in cmake so it can be applied consistently across the project

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cbodley so instead of hardwiring the cmake to -DFMT_HEADER_ONLY.

  1. i need to understand what does FMT_HEADER_ONLY offer to us? i asked for details, but no replies so far.
  2. if we need it for some use cases.
  • we should have an option for it.
  • fmt::fmt does not only carry the library linkage, it also contains INTERFACE_INCLUDE_DIRECTORIES. instead of removing all occurrences of fmt::fmt, we could have a fmt::header library instead which exposes -DFMT_HEADER_ONLY and the INTERFACE_INCLUDE_DIRECTORIES.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we should choose one and stick with it. if we provide both options, we're likely to end up with targets whose dependency chains include both. if there aren't good arguments for header-only, lets stick with static linkage

@mattbenjamin

Copy link
Copy Markdown
Contributor

In general, I expect header-only fmt to have better optimization. Not only don't we have those numbers, we have no numbers showing that doing so slows compilation significantly.

@adamemerson

Copy link
Copy Markdown
Contributor Author

Getting some numbers, I wrote a simple benchmark:

#include <iterator>
#include <string>

#include <fmt/format.h>

int main() {
  std::string x;
  std::string y;
  x.reserve(1024);
  y.reserve(1024);
  for (auto i = 0; i < 33554432; ++i) {
    x.clear();
    y.clear();
    fmt::format_to(std::back_inserter(x), "{}", i);
    fmt::format_to(std::back_inserter(y), "{}={}", i, x);
  }
}

And I compiled it both as header only and linked with a library:

% g++ -DFMT_HEADER_ONLY -Ofast -flto -o bench_fmt_header bench_fmt.cc
% g++ -Ofast -flto -o bench_fmt_lib -lfmt bench_fmt.cc

The header only version seems to be notably faster than the linked version

% time ./bench_fmt_header 

            Time spent in user mode:                    2.62s
            Time spent in kernel mode:                  0.00s
            Total time:                                 2.63s
            CPU utilisation:                            99%

% time ./bench_fmt_lib   

            Time spent in user mode:                    4.29s
            Time spent in kernel mode:                  0.00s
            Total time:                                 4.34s
            CPU utilisation:                            99%

@mattbenjamin

Copy link
Copy Markdown
Contributor

Thanks, @adamemerson. That does seem significant.

@cbodley

cbodley commented Sep 16, 2020

Copy link
Copy Markdown
Contributor

thanks @adamemerson, that looks like a good motivation for header-only!

@ronen-fr

Copy link
Copy Markdown
Contributor

Please note that when compiling with clang, I do not see this kind of speed up
(on a larger N: 14.296 vs 14.839 seconds).
This minor speedup might not be enough to compensate for the larger object size (in a real project)

@mattbenjamin

Copy link
Copy Markdown
Contributor

It seems to me the header-style is enabling a type of concrete optimization generally favored in modern c++. We've shown it's advantage with a real compiler. The objections are appealing to affects from object size which are not quantified, and on compilation times, which are also not quantified (but likely negligible).

@stale

stale Bot commented Nov 22, 2020

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs for another 30 days.
If you are a maintainer or core committer, please follow-up on this pull request to identify what steps should be taken by the author to move this proposed change forward.
If you are the author of this pull request, thank you for your proposed contribution. If you believe this change is still appropriate, please ensure that any feedback has been addressed and ask for a code review.

@stale stale Bot added the stale label Nov 22, 2020
@github-actions

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

@adamemerson adamemerson closed this Apr 1, 2021
@adamemerson
adamemerson deleted the wip-header-only-fmt branch April 1, 2021 21:52
@cbodley

cbodley commented Mar 10, 2023

Copy link
Copy Markdown
Contributor

this closed without any resolution, and a bunch of random files are still adding #define FMT_HEADER_ONLY 1 whether or not the WITH_FMT_HEADER_ONLY cmake option from #42464 is set

we're now seeing linker errors in libcls_rgw.so on centos9 in https://tracker.ceph.com/issues/58953 after #49052 added a non-header-only include to ceph_time.h

we really need to pick one or the other and be consistent about it. given @adamemerson's benchmark on gcc, enabling WITH_FMT_HEADER_ONLY globally seems like an obvious choice until we have link-time optimizations that can match it

currently we get the worst of both worlds: we're already paying for header-only compile times, and we're already linking both versions into cephfs, rbd, and rgw

@cbodley

cbodley commented Mar 10, 2023

Copy link
Copy Markdown
Contributor

in the meantime, i've opened #50479 to remove all of the #defines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants