Skip to content

Should composites be eagerly interned (=== equal) #15

@acutmore

Description

@acutmore

A question raised during yesterday's plenary was if the creation of Composite's should eagerly intern/canonicalize, such that Composite({ x: 1 }) === Composite({ x:1 }).

Upsides:

  • "pay to play"
    • not requiring any changes to existing equality checks avoiding adding new code branches which can have a cost even if the application is not using Composites
  • No new equality
    • Existing libraries/APIs would 'just work'
  • Checking if two composites are equal would be one pointer equality check

Downsides:

  • Composite creation becomes more expensive
  • This would mean that unique symbol keys would not be possible
    • Only sortable values are usable as keys
    • Symbol.iterator could not be a key
  • Could not do SameValueZero
    • Would either be SameValue or -0 is canonicalized to 0.
  • Some objects would need to be banned from Weak{Map,Set}
    • e.g. Composite({ x: 1, y: 4 }) has no constituents that provide a non-infinite lifetime
  • Potential for more garbage collection time due to added complexity
    • e.g. A WeakMap where Composite({ a: objectA, b: objectB, c: 42 }) is a key should remain in the WeakMap until either objectA or objectB becomes unreachable - this applies deeply for composites made from other composites.
  • Prototype equality checks:
    • This may close the door for non-null prototypes with cross-realm equality
    • In a world where there are built in ordinal Composites (Tuples) with a Tuple.prototype to provide Symbol.iterator and list methods such as .map, .some for these to still be equal across realms they would need to have a realm scoped [[prototype]] lookup similar to primitives and the one being discussed for Shared Structs.

Important

The quantity of the above lists should not be interpreted as weights on different sides of a scale. 10 downsides could still be trumped by 1 upside.

One good next step here IMO is to get some empirical data from a prototype implementation to measure some of performance impacts of different designs with different application use cases. Maybe this will be a good time for me to learn how to build v8 and get a custom binary on to some mobile phones!

cc: @syg

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions