Skip to content

Commit 5e6c891

Browse files
DZakhcknitt
andauthored
Improve extension creation runtime (#6958)
* Improve extension creation runtime * Update changelog * Update jscomp/runtime/caml_exceptions.res Co-authored-by: Christoph Knittel <christoph@knittel.cc> --------- Co-authored-by: Christoph Knittel <christoph@knittel.cc>
1 parent 88c9a20 commit 5e6c891

6 files changed

+38
-45
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Fixed tabulation for `throw new Error` bodies
2323
- Removed empty line at the end of `switch` statement
2424
- Removed empty `default` case from `switch` statement in the generated code
25+
- Optimised the Type Extension runtime code and removed trailing `/1` from `RE_EXN_ID` https://github.com/rescript-lang/rescript-compiler/pull/6958
2526

2627
#### :bug: Bug Fix
2728
- Fix issue where long layout break added a trailing comma in partial application `...`. https://github.com/rescript-lang/rescript-compiler/pull/6949

jscomp/runtime/caml_exceptions.res

+19-21
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,40 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
2424

25-
module Map = {
26-
type t<'k, 'v>
25+
type t = {@as("RE_EXN_ID") id: string}
2726

28-
@new external make: unit => t<'k, 'v> = "Map"
27+
module Dict = {
28+
@obj
29+
external empty: unit => dict<'a> = ""
2930

30-
@send external set: (t<'k, 'v>, 'k, 'v) => unit = "set"
31+
@set_index
32+
external set: (dict<'a>, string, 'a) => unit = ""
3133

32-
@send external get: (t<'k, 'v>, 'k) => option<'v> = "get"
34+
@get_index
35+
/**
36+
It's the same as `Js.Dict.get` but it doesn't have runtime overhead to check if the key exists.
37+
*/
38+
external dangerouslyGetNonOption: (dict<'a>, string) => option<'a> = ""
3339
}
3440

35-
type t = {@as("RE_EXN_ID") id: string}
36-
3741
/**
38-
Could be exported for better inlining
39-
It's common that we have
40-
{[ a = caml_set_oo_id([248,"string",0]) ]}
41-
This can be inlined as
42-
{[ a = caml_set_oo_id([248,"string", caml_oo_last_id++]) ]}
42+
Needs to have unique extension ids when used with functors.
43+
See discussion in https://github.com/rescript-lang/rescript-compiler/pull/6570
4344
*/
44-
let idMap: Map.t<string, int> = Map.make()
45+
let idMap = Dict.empty()
4546

4647
let create = (str: string): string => {
47-
let id = switch idMap->Map.get(str) {
48+
switch idMap->Dict.dangerouslyGetNonOption(str) {
4849
| Some(v) => {
4950
let id = v + 1
50-
idMap->Map.set(str, id)
51-
id
51+
idMap->Dict.set(str, id)
52+
str ++ ("/" ++ (Obj.magic((id: int)): string))
5253
}
5354
| None => {
54-
let id = 1
55-
idMap->Map.set(str, id)
56-
id
55+
idMap->Dict.set(str, 1)
56+
str
5757
}
5858
}
59-
60-
str ++ ("/" ++ (Obj.magic((id: int)): string))
6159
}
6260

6361
/**

lib/es6/caml_exceptions.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11

22

33

4-
let idMap = new Map();
4+
let idMap = {};
55

66
function create(str) {
7-
let v = idMap.get(str);
8-
let id;
7+
let v = idMap[str];
98
if (v !== undefined) {
10-
let id$1 = v + 1 | 0;
11-
idMap.set(str, id$1);
12-
id = id$1;
13-
} else {
14-
idMap.set(str, 1);
15-
id = 1;
9+
let id = v + 1 | 0;
10+
idMap[str] = id;
11+
return str + ("/" + id);
1612
}
17-
return str + ("/" + id);
13+
idMap[str] = 1;
14+
return str;
1815
}
1916

2017
function is_extension(e) {
@@ -34,4 +31,4 @@ export {
3431
is_extension,
3532
exn_slot_name,
3633
}
37-
/* idMap Not a pure module */
34+
/* No side effect */

lib/es6/caml_js_exceptions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ export {
2828
internalToOCamlException,
2929
as_js_exn,
3030
}
31-
/* Caml_exceptions Not a pure module */
31+
/* No side effect */

lib/js/caml_exceptions.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
'use strict';
22

33

4-
let idMap = new Map();
4+
let idMap = {};
55

66
function create(str) {
7-
let v = idMap.get(str);
8-
let id;
7+
let v = idMap[str];
98
if (v !== undefined) {
10-
let id$1 = v + 1 | 0;
11-
idMap.set(str, id$1);
12-
id = id$1;
13-
} else {
14-
idMap.set(str, 1);
15-
id = 1;
9+
let id = v + 1 | 0;
10+
idMap[str] = id;
11+
return str + ("/" + id);
1612
}
17-
return str + ("/" + id);
13+
idMap[str] = 1;
14+
return str;
1815
}
1916

2017
function is_extension(e) {
@@ -32,4 +29,4 @@ function exn_slot_name(x) {
3229
exports.create = create;
3330
exports.is_extension = is_extension;
3431
exports.exn_slot_name = exn_slot_name;
35-
/* idMap Not a pure module */
32+
/* No side effect */

lib/js/caml_js_exceptions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ function as_js_exn(exn) {
2626
exports.$$Error = $$Error;
2727
exports.internalToOCamlException = internalToOCamlException;
2828
exports.as_js_exn = as_js_exn;
29-
/* Caml_exceptions Not a pure module */
29+
/* No side effect */

0 commit comments

Comments
 (0)