AK: Add fast path for formatting common types

This commit changes `AK::TypeErasedParameter` to store integer,
floating-point and string types as well as characters and booleans
directly instead of through a type-erased `void const*`.

This is advantageous for binary size:
- The compiler no longer needs to push both the parameter value and a
  pointer to it to the stack (which contains the argument array);
  storing just the value is enough.
- Instead of instantiating `__format_value` for these types and taking
  its address (which generates a GOT entry and an ADRP+LDR pair in
  assembly), we just store a constant `TypeErasedParameter::Type` value.
- The biggest saving comes from the fact that we used to instantiate a
  distinct `__format_value` for each length of string literal. For
  LibJS, this meant 69 different functions! We can now just store them
  as a `char const*`.

I opted to use a manual tagged union instead of `Variant`: the code
wouldn't be much shorter if we used a `Variant` since we'd have to
handle converting the standard integer types to `u64`/`i64` and custom
types to the type erased wrapper via explicit constructors anyway. And
compile time overhead is smaller this way.

This gives us some nice binary size savings (numbers are from arm64
macOS LibJS):

     FILE SIZE        VM SIZE
  --------------  --------------
    +52% +10.3Ki   +52% +10.3Ki    [__TEXT]
   +5.2%    +768  +5.2%    +768    [__DATA_CONST]
   -0.0%      -7  -0.0%      -7    __TEXT,__cstring
   -3.0%    -144  -3.0%    -144    __TEXT,__stubs
   -1.2%    -176  -1.2%    -176    Function Start Addresses
  -11.6%    -432 -11.6%    -432    Indirect Symbol Table
   -1.0%    -448  -1.0%    -448    Code Signature
  -18.1%    -768 -18.1%    -768    __DATA_CONST,__got
   -0.8% -6.83Ki  -0.8% -6.83Ki    Symbol Table
   -1.0% -11.2Ki  -1.0% -11.2Ki    String Table
   -0.9% -26.1Ki  -0.9% -26.1Ki    __TEXT,__text
   -7.2% -20.9Ki  -9.6% -28.9Ki    [__LINKEDIT]
   -1.0% -56.0Ki  -1.1% -64.0Ki    TOTAL
This commit is contained in:
Daniel Bertalan 2025-05-24 09:06:42 +02:00 committed by Andrew Kaster
commit 04fac0031c
Notes: github-actions[bot] 2025-05-26 19:03:37 +00:00
2 changed files with 125 additions and 75 deletions

View file

@ -106,7 +106,13 @@ ErrorOr<void> vformat_impl(TypeErasedFormatParams& params, FormatBuilder& builde
auto& parameter = params.parameters().at(specifier.index);
FormatParser argparser { specifier.flags };
TRY(parameter.formatter(params, builder, argparser, parameter.value));
TRY(parameter.visit([&]<typename T>(T const& value) {
if constexpr (IsSame<T, TypeErasedParameter::CustomType>) {
return value.formatter(params, builder, argparser, value.value);
} else {
return __format_value<T>(params, builder, argparser, &value);
}
}));
TRY(vformat_impl(params, builder, parser));
return {};
}