diff --git a/CHANGELOG.md b/CHANGELOG.md index 8ac5645a55..830dd37dac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/jscomp/runtime/caml_exceptions.res b/jscomp/runtime/caml_exceptions.res index 07896a7810..0ca8fee7af 100644 --- a/jscomp/runtime/caml_exceptions.res +++ b/jscomp/runtime/caml_exceptions.res @@ -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 = 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)) } /** diff --git a/lib/es6/caml_exceptions.js b/lib/es6/caml_exceptions.js index 63b488abac..9db019af3c 100644 --- a/lib/es6/caml_exceptions.js +++ b/lib/es6/caml_exceptions.js @@ -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) { @@ -34,4 +31,4 @@ export { is_extension, exn_slot_name, } -/* idMap Not a pure module */ +/* No side effect */ diff --git a/lib/es6/caml_js_exceptions.js b/lib/es6/caml_js_exceptions.js index 878ee0080e..dccd3a3a10 100644 --- a/lib/es6/caml_js_exceptions.js +++ b/lib/es6/caml_js_exceptions.js @@ -28,4 +28,4 @@ export { internalToOCamlException, as_js_exn, } -/* Caml_exceptions Not a pure module */ +/* No side effect */ diff --git a/lib/js/caml_exceptions.js b/lib/js/caml_exceptions.js index 3150c3c6c0..cdff924b3f 100644 --- a/lib/js/caml_exceptions.js +++ b/lib/js/caml_exceptions.js @@ -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) { @@ -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 */ diff --git a/lib/js/caml_js_exceptions.js b/lib/js/caml_js_exceptions.js index 732c63689d..9fe06d0ffa 100644 --- a/lib/js/caml_js_exceptions.js +++ b/lib/js/caml_js_exceptions.js @@ -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 */