Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: 2b8f7267635745ac735406185b3f36348c5e82fc
      
https://github.com/WebKit/WebKit/commit/2b8f7267635745ac735406185b3f36348c5e82fc
  Author: Devin Rousso <h...@devinrousso.com>
  Date:   2024-11-09 (Sat, 09 Nov 2024)

  Changed paths:
    A JSTests/stress/map-getOrInsert.js
    A JSTests/stress/map-getOrInsertComputed.js
    A JSTests/stress/weakmap-getOrInsert.js
    A JSTests/stress/weakmap-getOrInsertComputed.js
    M Source/JavaScriptCore/runtime/MapPrototype.cpp
    M Source/JavaScriptCore/runtime/OptionsList.h
    M Source/JavaScriptCore/runtime/OrderedHashTable.h
    M Source/JavaScriptCore/runtime/OrderedHashTableHelper.h
    M Source/JavaScriptCore/runtime/WeakMapImpl.h
    M Source/JavaScriptCore/runtime/WeakMapImplInlines.h
    M Source/JavaScriptCore/runtime/WeakMapPrototype.cpp

  Log Message:
  -----------
  [JSC] add support for `Map.prototype.getOrInsert` et al
https://bugs.webkit.org/show_bug.cgi?id=282014

Reviewed by Yusuke Suzuki.

It's an extremely common pattern to check for the existence of a key in a `Map` 
(or `WeakMap`) before adding a new entry.

```js
if (map.has(key))
    return map.get(key);
map.set(key, value);
return value;
```

The primary benefit of of `getOrInsert` is that it avoids having to re-hash the 
key.

The additional benefit of `getOrInsertComputed` beyond the above is that it 
doesn't have to compute the value unless the key is not present.

For both `Map` and `WeakMap` it was necesasry to create additional helper 
methods that returned the hash and/or existing entry.

Spec: <https://tc39.es/proposal-upsert/>
Proposal: <https://github.com/tc39/proposal-upsert>

* Source/JavaScriptCore/runtime/MapPrototype.cpp:
(JSC::MapPrototype::finishCreation):
(JSC::mapProtoFuncGetOrInsert): Added.
(JSC::mapProtoFuncGetOrInsertComputed): Added.
* Source/JavaScriptCore/runtime/OrderedHashTable.h:
(JSC::OrderedHashMap::getOrInsert): Added.
* Source/JavaScriptCore/runtime/OrderedHashTableHelper.h:
(JSC::OrderedHashTableHelper::addImpl):

* Source/JavaScriptCore/runtime/WeakMapPrototype.cpp:
(JSC::WeakMapPrototype::finishCreation):
(JSC::protoFuncWeakMapGetOrInsert): Added.
(JSC::protoFuncWeakMapGetOrInsertComputed): Added.
* Source/JavaScriptCore/runtime/WeakMapImpl.h:
(JSC::WeakMapImpl::getBucket): Added.
(JSC::WeakMapImpl::findBucketIndex): Added.
(JSC::WeakMapImpl::findBucket):
(JSC::WeakMapImpl::findBucketIndexAlreadyHashed): Added.
(JSC::WeakMapImpl::findBucketAlreadyHashed): Deleted.
* Source/JavaScriptCore/runtime/WeakMapImplInlines.h:
(JSC::WeakMapImpl<WeakMapBucket>::addBucket): Added.

* Source/JavaScriptCore/runtime/OptionsList.h:

* JSTests/stress/map-getOrInsert.js: Added.
* JSTests/stress/map-getOrInsertComputed.js: Added.
* JSTests/stress/weakmap-getOrInsert.js: Added.
* JSTests/stress/weakmap-getOrInsertComputed.js: Added.

Canonical link: https://commits.webkit.org/286377@main



To unsubscribe from these emails, change your notification settings at 
https://github.com/WebKit/WebKit/settings/notifications
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to