From 39a74676bd7b14750b133aaa05a4dbdc80f4d304 Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Thu, 21 Oct 2021 19:09:38 +0200 Subject: [PATCH] AK: Avoid temporary String allocation in Formatter 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. --- AK/Format.cpp | 4 +++- AK/Format.h | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/AK/Format.cpp b/AK/Format.cpp index c4ebe8e59f..ac73e68649 100644 --- a/AK/Format.cpp +++ b/AK/Format.cpp @@ -610,7 +610,9 @@ void Formatter::format(FormatBuilder& builder, StringView value) void Formatter::vformat(FormatBuilder& builder, StringView fmtstr, TypeErasedFormatParams& params) { - return Formatter::format(builder, String::vformatted(fmtstr, params)); + StringBuilder string_builder; + AK::vformat(string_builder, fmtstr, params); + return Formatter::format(builder, string_builder.string_view()); } template diff --git a/AK/Format.h b/AK/Format.h index 953e062b8b..c1c10d0b9f 100644 --- a/AK/Format.h +++ b/AK/Format.h @@ -595,7 +595,7 @@ struct Formatter> : __FormatIfSupported> struct FormatString { }; template<> -struct Formatter : Formatter { +struct Formatter : Formatter { template void format(FormatBuilder& builder, StringView fmtstr, const Parameters&... parameters) {