Переглянути джерело

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.
Daniel Bertalan 3 роки тому
батько
коміт
39a74676bd
2 змінених файлів з 4 додано та 2 видалено
  1. 3 1
      AK/Format.cpp
  2. 1 1
      AK/Format.h

+ 3 - 1
AK/Format.cpp

@@ -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>

+ 1 - 1
AK/Format.h

@@ -595,7 +595,7 @@ struct Formatter<FormatIfSupported<T>> : __FormatIfSupported<T, HasFormatter<T>>
 struct FormatString {
 };
 template<>
-struct Formatter<FormatString> : Formatter<String> {
+struct Formatter<FormatString> : Formatter<StringView> {
     template<typename... Parameters>
     void format(FormatBuilder& builder, StringView fmtstr, const Parameters&... parameters)
     {