1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:24:57 +00:00

AK: Avoid temporary String allocation in Formatter<FormatString>

Creating a String object from the formatted data is unnecessary, as it
immediately gets turned into a StringView anyways.

With this change, we will no longer allocate on the heap when printing
`VirtualAddress`, `VirtualRange` and `InodeIdentifier` objects, which is
a step towards stronger OOM hardening.
This commit is contained in:
Daniel Bertalan 2021-10-21 19:09:38 +02:00 committed by Andreas Kling
parent 018c4e0e7e
commit 39a74676bd
2 changed files with 4 additions and 2 deletions

View file

@ -610,7 +610,9 @@ void Formatter<StringView>::format(FormatBuilder& builder, StringView value)
void Formatter<FormatString>::vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params)
{
return Formatter<String>::format(builder, String::vformatted(fmtstr, params));
StringBuilder string_builder;
AK::vformat(string_builder, fmtstr, params);
return Formatter<StringView>::format(builder, string_builder.string_view());
}
template<typename T>