Skip to content

Make Stdlib.Dict more performant #7517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 16 additions & 13 deletions lib/es6/Stdlib_Dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 16 additions & 13 deletions lib/js/Stdlib_Dict.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 25 additions & 16 deletions runtime/Stdlib_Dict.res
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some comments on why we are using %raw for these functions (link to benchmarks)?

(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"

Expand Down