Skip to content

include/{encoding,denc}: move container-specific templates to separate headers - #72769

Open
MaxKellermann wants to merge 23 commits into
ceph:mainfrom
MaxKellermann:encoding__split_containers
Open

MaxKellermann wants to merge 23 commits into
ceph:mainfrom
MaxKellermann:encoding__split_containers

Conversation

@MaxKellermann

Copy link
Copy Markdown
Member

This reduces header dependencies because including encoding.h no longer pulls in all those other headers.

To take advantage of ADL (argument-dependent lookup), the specializations are placed in the namespace of the first argument. That eliminates the need for the forward-declared function prototypes and properly fixes ordering problems that were worked around using these forward declarations. This workaround doesn't work anymore after the split.

Another PR split from #60490

Since this patch depends on various other patches I submitted, it contains parts of other unmerged PRs. I will keep this PR rebased the others are merged, dropping merged commits.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands
  • jenkins retest this please
  • jenkins test classic perf
  • jenkins test crimson perf
  • jenkins test signed
  • jenkins test make check
  • jenkins test make check arm64
  • jenkins test submodules
  • jenkins test dashboard
  • jenkins test dashboard cephadm
  • jenkins test api
  • jenkins test docs
  • jenkins render docs
  • jenkins test ceph-volume all
  • jenkins test ceph-volume tox
  • jenkins test windows
  • jenkins test rook e2e

@MaxKellermann

Copy link
Copy Markdown
Member Author

jenkins test make check arm64

@MaxKellermann

Copy link
Copy Markdown
Member Author

jenkins test windows

@MaxKellermann

Copy link
Copy Markdown
Member Author

"Check for Incompatible Licenses" has a bogus failure because:

HTTP 406: Sorry, the diff exceeded the maximum number of files (300).

@MaxKellermann

Copy link
Copy Markdown
Member Author

jenkins test make check arm64

Comment thread src/include/encoding_string_view.h Resolved
@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

@MaxKellermann
MaxKellermann force-pushed the encoding__split_containers branch from 8a106d8 to 5eef80d Compare October 19, 2026 12:23
@MaxKellermann
MaxKellermann force-pushed the encoding__split_containers branch 3 times, most recently from 6d9360a to 2b7cfd6 Compare October 19, 2026 13:03
@MaxKellermann
MaxKellermann force-pushed the encoding__split_containers branch from ed5ff99 to 9312ed1 Compare November 15, 2026 11:17

@chardan chardan left a comment

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 took some ball-park measurements and I think we're just fine (happy to get you the methodology if you want, but it's more of a ball-park than truly comprehensive set of measurements):

  • Allocation counts and requested bytes were unchanged across all 42 message/KV cases.
  • Readdir-style decoding was about 19% faster for small contiguous inputs and 4% faster for fragmented inputs, with fewer retired instructions.
  • Monitor-command encoding increased from about 80 to 88 ns, with variability between runs and no increase in instructions or allocations.
  • CephFS metadata throughput differences were within approximately ±1.6%, with overlapping ranges and no clear application-level regression.
  • DirStat construction: initialization moves from inline stores to a function call. The assembly explains approximately 6 extra instructions, matching the measured increase from 1.3 to 2.4 ns.
  • KV iterator helpers: the compiler loses inline forwarding and introduces an out-of-line base-helper call. This closely matches the ~41 extra instructions and ~1.6–1.8 ns per combined adapter-probe iteration.

The performance /improvement/ for readdir is interesting: in the PR build, several string/vector/bufferlist decode wrappers are inlined within the decoder, and buffer operations use direct library calls instead of PLT indirection.

In any case, I'm pretty confident any of the actual system operations will indeed dominate even a busy machine here and I expect the build time improvement will be worth it.

I think this looks really good-- please look at the slight sprawl of files, but generally I think this is a welcome improvement! As a bonus, a lot of header files IMO are actually cleaned up from this.

I like your idea of looking into libcommon for some savings by fiddling with the linkage, that sounds worthwhile!

Changing gears slightly, you might peek at this PR series, focused on improving some low-hanging fruit: #72322
As for the rest, don't worry-- it won't be the last time I'm accused of some pedantry.

Comment thread src/include/cpp_lib_backport.h Outdated

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'm a bit conflicted here, because although the contents of this header are no longer needed, there are certainly still things in the C++23 standard library that we have to provide by other means for some compilers. This is probably the most straightforward way available in Ceph to do that, and it's already plumbed into the build system, etc.., so while I'm totally for removing a genuinely obsolete header, I'm not really sure this one doesn't have some utility. It might be good to have other Ceph folks chime in on this.

#include "include/Context.h"
#include "include/encoding_boost_optional.h"
#include "include/encoding_set.h"
#include "include/encoding_string.h"

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.

It may be asking a lot for programmers to remember to pull in a specific header. I think it's excellent to build the option, though!

My other thought here is an organizational one: have you considered making encoding a bit more modular/library-like and going with something like "include/encoding.h" -> [fans out to] < "include/encoding/*h", something like that?

I guess the problem there is that folks will probably just use include/encoding.h still and not reach for the specific header at all, somewhat derailing the point of the effort. There may be no real winning on this one. But I think we should try not to add too many things to any given level so they're at least a bit human-friendly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think there should be a catch-all header that includes 'em all. That would defy the point of including only what you need, if there's an easy (and slow) path to avoid the decision of what you really need. If you want to encode strings, include the string-specific header.
How to organize the headers, I don't care. If you say you want a new subdirectory (instead of flat directory with underscore), fine, I can change it easily. The idea remains: not polluting the namespace (and the compiler) with headers nobody really needs.
And this idea should go much further: for a class that defines to be encodable (but doesn't have inline definitions of the encoding/deecoding methods), there should really only be a very minimal header with not much more than forward declarations.

/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2020 Red Hat, Inc.

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.

We might need to update this!

Comment thread src/auth/KeyRing.cc Resolved
@chardan chardan mentioned this pull request Nov 18, 2026
4 of 12 tasks
@github-actions

Copy link
Copy Markdown

@MaxKellermann Thank you for reviewing this pull request! As a member of the Ceph organization, please add yourself to .githubmap in the main branch if you haven't already.

Comment thread src/include/encoding_array.h Resolved
Signed-off-by: Max Kellermann <[email protected]>
…ound_encode()

The parameter was `const ceph::buffer::ptr&` instead of
`const health_status_t&`.  This only compiled because the enum was
implicitly converted to `unsigned` and then to a buffer::ptr via its
implicit `ptr(unsigned)` constructor, allocating a temporary buffer
of 0..2 bytes on every bound_encode() call.

Signed-off-by: Max Kellermann <[email protected]>
…trs()

setxattrs() encoded the attribute map into `bl`, but then passed
`bl.length()` instead of `bl` to add_xattr(), which was implicitly
converted to an empty buffer::list via its `list(unsigned prealloc)`
constructor, so the attributes were never sent.

This regression was caused 17 years ago by commit 7a624a4 ("osd:
hold op data on the specific op") which removed the "valuelen"
overload of add_xattr().  Nobody ever noticed that bug because this is
dead code.

Signed-off-by: Max Kellermann <[email protected]>
…icit

Constructing a buffer::ptr or a buffer::list from an `unsigned`
(allocation length / preallocation size) was allowed implicitly,
which means any integer could silently be converted to an empty
buffer, e.g. when passed to a function taking a buffer::ptr or
buffer::list parameter.

Signed-off-by: Max Kellermann <[email protected]>
Signed-off-by: Max Kellermann <[email protected]>
Signed-off-by: Max Kellermann <[email protected]>
Signed-off-by: Max Kellermann <[email protected]>
…rocksdb_option with kv

Both use KeyValueDB/RocksDBStore code from libkv, but only got it
transitively through `os`, which links `kv` only via bluestore.
With WITH_BLUESTORE=OFF, they fail to link.  Commit 9a0e3c7 fixed
the same problem for the other ObjectMap tests.

Signed-off-by: Max Kellermann <[email protected]>
urgent_msg_t is an unscoped enum without an encoder; it was implicitly
promoted to int and encoded with the legacy encode(int32_t) overload.
Use std::to_underlying() instead, so the wire format does not depend
on implicit conversions.  The underlying type is `unsigned int`,
which is encoded as the same 4 little-endian bytes (the receiver in
Background::handle_notify() decodes an int32_t).

Signed-off-by: Max Kellermann <[email protected]>
A comment in this header says "we used to use -1 for invalid release".
This number is (usuallu) represented in the `uint8_t` as `0xff`, and
casting the enum to `int` passes this `0xff` instead of `-1`, causing
to_string() to return "unknown" instead of "unspecified".

Signed-off-by: Max Kellermann <[email protected]>
To reduce header dependencies and compile times.

Signed-off-by: Max Kellermann <[email protected]>
Reduce header dependencies for sources that need only struct
MDSHealth.

Signed-off-by: Max Kellermann <[email protected]>
Signed-off-by: Max Kellermann <[email protected]>
Signed-off-by: Max Kellermann <[email protected]>
This decode() overload needs to be moved to the `std` namespace for
C++ ADL to work properly.  This however willresult in a build failure,
and the best way to solve this is to import the primary function with
`using` line.

Signed-off-by: Max Kellermann <[email protected]>
Signed-off-by: Max Kellermann <[email protected]>
…oders

The non-template encode() overloads for bool and integers take their
argument by value (or const reference), so any type that is implicitly
convertible to bool or to one of the integer types silently gets
encoded with them, e.g. a pointer as bool, an unscoped enum (promoted
to int) as int32_t or a class with a conversion operator.  This can
happen unnoticed when the proper overload is not visible, and changes
the wire format.

Add a deleted encode() template which is an exact match for such
types and therefore wins over all conversions.  It is constrained to
types without denc_traits, so it does not compete with the denc glue,
and the non-template overloads (the legacy fast path for bool and the
integer types, and those written for specific types) still win over it
on a tie.

Signed-off-by: Max Kellermann <[email protected]>
It was added 4 years ago by commit 6cc31cc to fix build
failures with a buggy GCC 10 standard library, but since Ceph has
switched to C++23 last year, building with GCC 10 is no longer
possible anyway.

Signed-off-by: Max Kellermann <[email protected]>
…e headers

This reduces header dependencies because including `encoding.h` no
longer pulls in all those other headers.

To take advantage of ADL (argument-dependent lookup), the overloads
for Boost types are placed in the namespace of the first argument.
The overloads for standard library types are placed in namespace
`ceph::buffer` (because adding declarations to namespace `std` is
undefined behavior), where ADL finds them via the `bufferlist`
parameter.  That eliminates the need for the forward-declared
function prototypes and properly fixes ordering problems that were
worked around using these forward declarations.  This workaround
doesn't work anymore after the split.

Signed-off-by: Max Kellermann <[email protected]>
@MaxKellermann
MaxKellermann force-pushed the encoding__split_containers branch from 9312ed1 to 945c784 Compare November 18, 2026 20:18
@MaxKellermann

Copy link
Copy Markdown
Member Author

jenkins test windows

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.

4 participants