gren-lang / gren-lang/compiler
Optimize record updates
Nobody has claimed this yet.
- Dominant language
- Haskell
- Stars
- 503
- Forks
- 29
- PR merge metrics
- No merged PRs in 30d
Description
Record updates like:
{ record | field = newValue }
Are compiled to a "magic" runtime function which uses "reflection" to create a copy of the object, and then set the new values. This plays havoc with the hidden class of the object, and is in addition a slow operation.
Because of extensible records, we cannot simply replace record updates with the resulting completed object at compile time. What we can do, however, is make use of JS prototype functions to implement a sort of "Clonable" interface.
So, for a record of type:
{ name : String
, age : Int
}
We could compile
function Record1(name, age) {
this.name = name;
this.age = age;
}
Record1.prototype.update = function(mutator) {
var clone = new Record1(this.name, this.age);
mutator(clone);
return clone;
}
And then we could turn this:
let
someObj =
{ name = "Robin"
, age = 33
}
in
{ someObj | age = someObj.age + 1 }
into
var someObj = new Record1("Robin", 33);
return someObj.update(function(old) {
old.age = someObj.age + 1;
});
This has been tested through elm-optimize-level-2 and found to be much, much, faster. The price to pay is increased asset size, but nothing too concerning.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing how record updates are lowered by the compiler and how their generated JavaScript runtime implementation works. Use the elm-optimize-level-2 result as a performance baseline, then verify that the prototype-based approach preserves record-update behavior while improving speed and keeping asset growth acceptable.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- elm, haskell, javascript
- Domain
- compilers, performance
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100