From fe5670f40b05aab8f00d0624f3deeede37bfef2d Mon Sep 17 00:00:00 2001 From: mizchi Date: Tue, 25 Aug 2026 01:36:01 +0900 Subject: [PATCH 1/2] perf(json): presize maps during FromJson decode --- json/from_json.mbt | 2 +- json/from_json_map_bench_test.mbt | 40 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 json/from_json_map_bench_test.mbt diff --git a/json/from_json.mbt b/json/from_json.mbt index f159f4645..9ff89708e 100644 --- a/json/from_json.mbt +++ b/json/from_json.mbt @@ -242,7 +242,7 @@ pub impl[V : FromJson] FromJson for Map[String, V] with fn from_json(json, path) guard json is Object(obj) else { decode_error(path, "Map::from_json: expected object") } - let res = Map([]) + let res = Map([], capacity=obj.length()) // As above: the test cannot fail, it just makes `key` assignable. guard! JsonPath::Key(path, key="") is (Key(_) as new_path) // Map::map_with_key diff --git a/json/from_json_map_bench_test.mbt b/json/from_json_map_bench_test.mbt new file mode 100644 index 000000000..3064d2b54 --- /dev/null +++ b/json/from_json_map_bench_test.mbt @@ -0,0 +1,40 @@ +// Copyright 2026 International Digital Economy Academy +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +///| +fn from_json_map_bench_input(size : Int) -> Json { + let object : Map[String, Json] = Map([], capacity=size) + for i in 0.. Date: Tue, 25 Aug 2026 01:44:34 +0900 Subject: [PATCH 2/2] fix(json): reuse source map capacity --- json/from_json.mbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json/from_json.mbt b/json/from_json.mbt index 9ff89708e..1bc801736 100644 --- a/json/from_json.mbt +++ b/json/from_json.mbt @@ -242,7 +242,7 @@ pub impl[V : FromJson] FromJson for Map[String, V] with fn from_json(json, path) guard json is Object(obj) else { decode_error(path, "Map::from_json: expected object") } - let res = Map([], capacity=obj.length()) + let res = Map([], capacity=obj.capacity()) // As above: the test cannot fail, it just makes `key` assignable. guard! JsonPath::Key(path, key="") is (Key(_) as new_path) // Map::map_with_key