From 66526cbbaf4411af89bf96ab44ba6a7f58f3eaea Mon Sep 17 00:00:00 2001 From: Max Wipfli Date: Mon, 28 Jun 2021 11:35:58 +0200 Subject: [PATCH] AK: Return const& from JsonObject::get() This adds a static JsonValue* s_null_value, which allows JsonObject::get to return a reference instaed of copying the return value. Since JsonValue is only 16 bytes, this seems like a reasonable compromise. --- AK/JsonObject.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/AK/JsonObject.h b/AK/JsonObject.h index 62c9393b4f..01ef593b7b 100644 --- a/AK/JsonObject.h +++ b/AK/JsonObject.h @@ -47,10 +47,16 @@ public: int size() const { return m_members.size(); } bool is_empty() const { return m_members.is_empty(); } - JsonValue get(String const& key) const + JsonValue const& get(String const& key) const { auto* value = get_ptr(key); - return value ? *value : JsonValue(JsonValue::Type::Null); + static JsonValue* s_null_value { nullptr }; + if (!value) { + if (!s_null_value) + s_null_value = new JsonValue; + return *s_null_value; + } + return *value; } JsonValue get_or(String const& key, JsonValue const& alternative) const