Skip to content

Improve extension creation runtime #6958

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

Merged
Merged
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 @@ -22,6 +22,7 @@
- Fixed tabulation for `throw new Error` bodies
- Removed empty line at the end of `switch` statement
- Removed empty `default` case from `switch` statement in the generated code
- Optimised the Type Extension runtime code and removed trailing `/1` from `RE_EXN_ID` https://github.com/rescript-lang/rescript-compiler/pull/6958

#### :bug: Bug Fix
- Fix issue where long layout break added a trailing comma in partial application `...`. https://github.com/rescript-lang/rescript-compiler/pull/6949
Expand Down
40 changes: 19 additions & 21 deletions jscomp/runtime/caml_exceptions.res
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,40 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

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

@new external make: unit => t<'k, 'v> = "Map"
module Dict = {
@obj
external empty: unit => dict<'a> = ""

@send external set: (t<'k, 'v>, 'k, 'v) => unit = "set"
@set_index
external set: (dict<'a>, string, 'a) => unit = ""

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

type t = {@as("RE_EXN_ID") id: string}

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

let create = (str: string): string => {
let id = switch idMap->Map.get(str) {
switch idMap->Dict.dangerouslyGetNonOption(str) {
| Some(v) => {
let id = v + 1
idMap->Map.set(str, id)
id
idMap->Dict.set(str, id)
str ++ ("/" ++ (Obj.magic((id: int)): string))
}
| None => {
let id = 1
idMap->Map.set(str, id)
id
idMap->Dict.set(str, 1)
str
}
}

str ++ ("/" ++ (Obj.magic((id: int)): string))
}

/**
Expand Down
19 changes: 8 additions & 11 deletions lib/es6/caml_exceptions.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@



let idMap = new Map();
let idMap = {};

function create(str) {
let v = idMap.get(str);
let id;
let v = idMap[str];
if (v !== undefined) {
let id$1 = v + 1 | 0;
idMap.set(str, id$1);
id = id$1;
} else {
idMap.set(str, 1);
id = 1;
let id = v + 1 | 0;
idMap[str] = id;
return str + ("/" + id);
}
return str + ("/" + id);
idMap[str] = 1;
return str;
}

function is_extension(e) {
Expand All @@ -34,4 +31,4 @@ export {
is_extension,
exn_slot_name,
}
/* idMap Not a pure module */
/* No side effect */
2 changes: 1 addition & 1 deletion lib/es6/caml_js_exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ export {
internalToOCamlException,
as_js_exn,
}
/* Caml_exceptions Not a pure module */
/* No side effect */
19 changes: 8 additions & 11 deletions lib/js/caml_exceptions.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
'use strict';


let idMap = new Map();
let idMap = {};

function create(str) {
let v = idMap.get(str);
let id;
let v = idMap[str];
if (v !== undefined) {
let id$1 = v + 1 | 0;
idMap.set(str, id$1);
id = id$1;
} else {
idMap.set(str, 1);
id = 1;
let id = v + 1 | 0;
idMap[str] = id;
return str + ("/" + id);
}
return str + ("/" + id);
idMap[str] = 1;
return str;
}

function is_extension(e) {
Expand All @@ -32,4 +29,4 @@ function exn_slot_name(x) {
exports.create = create;
exports.is_extension = is_extension;
exports.exn_slot_name = exn_slot_name;
/* idMap Not a pure module */
/* No side effect */
2 changes: 1 addition & 1 deletion lib/js/caml_js_exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ function as_js_exn(exn) {
exports.$$Error = $$Error;
exports.internalToOCamlException = internalToOCamlException;
exports.as_js_exn = as_js_exn;
/* Caml_exceptions Not a pure module */
/* No side effect */