File 6441-Optimize-json-object-encoding.patch of Package erlang
From b8c290471cb87d13099e1516455d36cde35f2edd Mon Sep 17 00:00:00 2001
From: sabiwara <sabiwara@gmail.com>
Date: Tue, 31 Dec 2024 09:28:37 +0900
Subject: [PATCH] Optimize json object encoding
Using characters rather than strings for curly brackets leads to
faster conversion of the generated iodata to binary downstream.
---
lib/stdlib/src/json.erl | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/lib/stdlib/src/json.erl b/lib/stdlib/src/json.erl
index 04a2014857..b2f0507d5b 100644
--- a/lib/stdlib/src/json.erl
+++ b/lib/stdlib/src/json.erl
@@ -323,7 +323,7 @@ key(Key, _Encode) when is_integer(Key) -> [$", encode_integer(Key), $"];
key(Key, _Encode) when is_float(Key) -> [$", encode_float(Key), $"].
encode_object([]) -> <<"{}">>;
-encode_object([[_Comma | Entry] | Rest]) -> ["{", Entry, Rest, "}"].
+encode_object([[_Comma | Entry] | Rest]) -> [${, Entry, Rest, $}].
-spec encode_binary(binary()) -> iodata().
encode_binary(Bin) when is_binary(Bin) ->
@@ -712,14 +712,14 @@ format_object([[_Comma,KeyIndent|Entry]], Indent) ->
{_, Rest} = string:take(Value, [$\s,$\n]),
[CP|_] = string:next_codepoint(Rest),
if CP =:= ${ ->
- ["{", KeyIndent, Entry, Indent, "}"];
+ [${, KeyIndent, Entry, Indent, $}];
CP =:= $[ ->
- ["{", KeyIndent, Entry, Indent, "}"];
+ [${, KeyIndent, Entry, Indent, $}];
true ->
["{ ", Entry, " }"]
end;
format_object([[_Comma,KeyIndent|Entry] | Rest], Indent) ->
- ["{", KeyIndent, Entry, Rest, Indent, "}"].
+ [${, KeyIndent, Entry, Rest, Indent, $}].
indent(#{level := Level, indent := Indent}) ->
Steps = Level * Indent,
--
2.43.0