From e087f369bfd33613eba6c43aabf324bcebf0c6d3 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Sat, 24 May 2025 18:14:37 +0400 Subject: [PATCH 1/2] Make Stdlib.Dict more performant --- lib/es6/Stdlib_Dict.js | 29 ++++++++++++++++------------- lib/js/Stdlib_Dict.js | 29 ++++++++++++++++------------- runtime/Stdlib_Dict.res | 41 +++++++++++++++++++++++++---------------- 3 files changed, 57 insertions(+), 42 deletions(-) diff --git a/lib/es6/Stdlib_Dict.js b/lib/es6/Stdlib_Dict.js index 51a70b3144..4e35a26d78 100644 --- a/lib/es6/Stdlib_Dict.js +++ b/lib/es6/Stdlib_Dict.js @@ -5,22 +5,25 @@ function $$delete$1(dict, string) { delete(dict[string]); } -function forEach(dict, f) { - Object.values(dict).forEach(value => f(value)); -} +let forEach = ((dict, f) => { + for (var key in dict) { + f(dict[key]); + } + }); -function forEachWithKey(dict, f) { - Object.keys(dict).forEach(key => f(dict[key], key)); -} +let forEachWithKey = ((dict, f) => { + for (var key in dict) { + f(dict[key], key); + } + }); -function mapValues(dict, f) { - let target = {}; - Object.keys(dict).forEach(key => { - let value = dict[key]; - target[key] = f(value); +let mapValues = ((dict, f) => { + var target = {}; + for (var key in dict) { + target[key] = f(dict[key]); + } + return target; }); - return target; -} export { $$delete$1 as $$delete, diff --git a/lib/js/Stdlib_Dict.js b/lib/js/Stdlib_Dict.js index ecb9c90844..1d59819861 100644 --- a/lib/js/Stdlib_Dict.js +++ b/lib/js/Stdlib_Dict.js @@ -5,22 +5,25 @@ function $$delete$1(dict, string) { delete(dict[string]); } -function forEach(dict, f) { - Object.values(dict).forEach(value => f(value)); -} +let forEach = ((dict, f) => { + for (var key in dict) { + f(dict[key]); + } + }); -function forEachWithKey(dict, f) { - Object.keys(dict).forEach(key => f(dict[key], key)); -} +let forEachWithKey = ((dict, f) => { + for (var key in dict) { + f(dict[key], key); + } + }); -function mapValues(dict, f) { - let target = {}; - Object.keys(dict).forEach(key => { - let value = dict[key]; - target[key] = f(value); +let mapValues = ((dict, f) => { + var target = {}; + for (var key in dict) { + target[key] = f(dict[key]); + } + return target; }); - return target; -} exports.$$delete = $$delete$1; exports.forEach = forEach; diff --git a/runtime/Stdlib_Dict.res b/runtime/Stdlib_Dict.res index 7604962689..0cd2a6bec7 100644 --- a/runtime/Stdlib_Dict.res +++ b/runtime/Stdlib_Dict.res @@ -24,22 +24,31 @@ let delete = (dict, string) => { @val external copy: (@as(json`{}`) _, dict<'a>) => dict<'a> = "Object.assign" -let forEach = (dict, f) => { - dict->valuesToArray->Stdlib_Array.forEach(value => f(value)) -} - -@inline -let forEachWithKey = (dict, f) => { - dict->keysToArray->Stdlib_Array.forEach(key => f(dict->getUnsafe(key), key)) -} - -let mapValues = (dict, f) => { - let target = make() - dict->forEachWithKey((value, key) => { - target->set(key, f(value)) - }) - target -} +let forEach: (dict<'a>, 'a => unit) => unit = %raw(` + (dict, f) => { + for (var key in dict) { + f(dict[key]); + } + } +`) + +let forEachWithKey: (dict<'a>, ('a, string) => unit) => unit = %raw(` + (dict, f) => { + for (var key in dict) { + f(dict[key], key); + } + } +`) + +let mapValues: (dict<'a>, 'a => 'b) => dict<'b> = %raw(` + (dict, f) => { + var target = {}; + for (var key in dict) { + target[key] = f(dict[key]); + } + return target; + } +`) external has: (dict<'a>, string) => bool = "%dict_has" From ccb9b62899fa54aae76d22e70505279b49990aa8 Mon Sep 17 00:00:00 2001 From: Dmitry Zakharov Date: Sat, 24 May 2025 18:15:58 +0400 Subject: [PATCH 2/2] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e25a06b4f..5ec52185e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - Improve error messages for JSX type mismatches, passing objects where record is expected, passing array literal where tuple is expected, and more. https://github.com/rescript-lang/rescript/pull/7500 - Show in error messages when coercion can be used to fix a type mismatch. https://github.com/rescript-lang/rescript/pull/7505 - Remove deprecated pipe last (|>) syntax. https://github.com/rescript-lang/rescript/pull/7512 +- Make `Dict` iterators more performant. https://github.com/rescript-lang/rescript/pull/7517 # 12.0.0-alpha.13