Skip to content
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

Maps and Sets are incredibly slow #176

Open
lightmare opened this issue Aug 29, 2021 · 2 comments
Open

Maps and Sets are incredibly slow #176

lightmare opened this issue Aug 29, 2021 · 2 comments

Comments

@lightmare
Copy link
Contributor

I know this engine is not focused on speed, however the spec contains this paragraph (and similar for WeakMap, Set and WeakSet):

Maps must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structure used in this specification is only intended to describe the required observable semantics of Maps. It is not intended to be a viable implementation model.

The current implementation here strictly follows the spec algorithms, meaning access times are not sublinear, and are not even proportional to the number of elements in the collection. They are linear with respect to the total number of successful insertions over the collection's lifetime.

@devsnek
Copy link
Member

devsnek commented Aug 29, 2021

indeed, the point is to validate the spec's required observable semantics, not to be fast here.

you might be interested in https://github.com/engine262/boost/, something I've been planning to continue work on at some point but haven't gotten around to it.

@lightmare
Copy link
Contributor Author

That boost thing doesn't sound right. I'd have to somehow replace bootstrapMap((Iterator)?Prototype)? and basically copy the whole Map class just to change a few lines in each method.

I'm not asking for fast. I've been thinking how it could be made reasonably slow while still following the spec (almost) step by step. For WeakMap/WeakSet it's trivial — since these can only store non-primitive keys, they could simply store their elements directly in a (node) Map/Set, instead of records in an array.

For Map/Set it's not so straightforward, but doable. The List that stores [[MapData/SetData]] doesn't have to be backed by an array. It is only required to be an ordered sequence. Could be backed by a (node) Map with integer indices, or a linked list, either of which would achieve linear-time operations with respect to the number of elements in the collection. Especially with a backing Map, there would be very few diversions from the current implementation — mostly in steps mentioning empty values, which would not be present.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants