From 6de11b858970a187fa5aafdc412e24dbbc24dae0 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Sun, 5 Jan 2025 12:37:22 -0800 Subject: [PATCH] cut unconditional string-key indirection in FormattedKeyHolder Summary: There are two optimizations within this one change in return type, assuming that the callee `FormattedKeyHolder::getFormattedKeyWithExtra` is not inline-expanded into the caller. * An indirection occurs within the callee, but the indirection would be needed only in the caller and only conditionally in a rare, slow path. If the callee is not inline-expanded into the caller, there may be no opportunity for the compiler to make the indirection itself conditional just like the usage of the indirection. This change returns the reference from the callee before indirection and allows the caller to indirect only conditionally. * The return type of the callee is too large to be passed via registers in the common ABIs, and so must be spilled to the stack. The maximum size is 2 pointers, but the return type is 3 pointers in size. This change modifies the return type to be 2 pointers in size, allowing the return value to be passed via registers and to skip the stack. Reviewed By: dddmello, DenisYaroshevskiy Differential Revision: D66175277 fbshipit-source-id: 7f3e7d8d48c40b6201458777b6702197168e307c --- fb303/ThreadCachedServiceData.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fb303/ThreadCachedServiceData.h b/fb303/ThreadCachedServiceData.h index 688d5b58b..49ec05be0 100644 --- a/fb303/ThreadCachedServiceData.h +++ b/fb303/ThreadCachedServiceData.h @@ -642,9 +642,13 @@ class FormattedKeyHolder { /** * Given a subkey (e.g. "bar"), returns the full stat key (e.g. "foo.bar") * and registers the stats export types if not registered already. + * + * Key is returned as a reference v.s. as a string_view since callers need it + * only in the cold path. This allows passing the return value in registers + * v.s. via the stack and defers the dereference operation to the cold path. */ template - std::pair> + std::pair> getFormattedKeyWithExtra(Args&&... subkeys) { auto& v = getFormattedEntry(std::forward(subkeys)...); return {*v.key, v.cached};