Enable improvement of JavaScript custom type performance now and in future #4263
Replies: 5 comments 18 replies
-
I think that sounds good. I haven't done a whole lot of FFI yet so I'm not sure if there are any other APIs we want to expose, but seems like a good set of functions so far |
Beta Was this translation helpful? Give feedback.
-
Anything else we need to do on this before it gets converted into an issue? I keep running into performance issues with unlabelled fields on type like |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Hi! Thank you for looking into this!! I do have more thoughts on which runtime representations should be used, but I think making a stable API is an important first step to enable us to explore different representations in the first place. When designing this API, we should consider:
I think using functions and getters for everything would be ideal for maximum flexibility. Function wrappers are extremely low-overhead since modern JS engines inline well, and separate functions might even improve performance. Using standalone functions, bundlers will remove extra code that is not needed. Using getters as the stable public API makes sense because:
Of course, data and benchmarks would be needed here, but I don't like excluding this possibility from the future if we decide to stabilise the JavaScript FFI API. Concretely, I propose that Gleam should generate and export It's unsafe to assume JS values match Gleam types and vice versa even though it's a relatively wide-spread practice, so this doesn't make things worse in theory. If JavaScript consumes a value JavaScript should create it (and the other way around). Each side provides constructors/accessors for the other. This is already partially enforced by compiler warnings and best-practices. It's already next to impossible to write JS bindings code without extra FFI wrappers, so while this does make those somewhat unusual JavaScript code, this is also somewhat expected in my opinion. A future extension could allow marking Gleam types as |
Beta Was this translation helpful? Give feedback.
-
Just small things that might be useful: const someObj = { ... }
const obj = Object.assign(new Item(), someObj) To convert easily from object to class (maybe for inside ffi's)?. And for object comparison, because everything is autogenerated anyway you can do something like this which also really improves performance of object comparisons: function createThingyFirst(a, b) {
return {
_id: (autoincrement case id here),
"one":a ,
"two":b,
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
The JavaScript code we generate for custom types could be changed to improve performance with current JavaScript engines, and future changes to JavaScript engines may motivate further changes.
It's hard to make changes today as we have not made clear what the intended interface is for JavaScript code using Gleam data structures. I propose we create and document an interface for external code authors to use which will give them sufficient control while being abstract enough to permit changes to the underlying data structures.
Code generated by the Gleam compiler could opt to work directly with the underlying data structures if there is a performance benefit to that, but we would document that to do the same in external code is to use private APIs and could result in breakages in future.
I believe these to be the required capabilities:
Proposed API
Construction
For each variant provide a
makeVariantName
function which takes the field values and returns the constructed record.Variant checking
For each variant provide a
isVariantName
function which return a boolean indicating if the argument is of the specific variant.Access labelled fields
The typical JavaScript
.label
syntax can be used to access these fields. It is presumed that property access like this will continue to be very fast in JavaScript due to how common it is and thus important to engines.Access unlabelled fields
The
._0
,._1
,._2
syntax can be used to access these fields. This is to be used instead of[0]
as these string keys benchmark faster than number indexing, and the same assumption about future performance is made here.Example
Beta Was this translation helpful? Give feedback.
All reactions