Skip to content

Commit

Permalink
[GR-59866] Add ObjectSpace::WeakMap#delete
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/4443
  • Loading branch information
andrykonchin committed Jan 8, 2025
2 parents b709cfb + 453a03e commit adb9fd1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Compatibility:
* Support `symbolize_names` argument to `MatchData#named_captures` (#3681, @rwstauner).
* Support `Proc#initialize_{dup,copy}` for subclasses (#3681, @rwstauner).
* Remove deprecated `Encoding#replicate` method (#3681, @rwstauner).
* Add `ObjectSpace::WeakMap#delete` (#3681, @andrykonchin).

Performance:

Expand Down
3 changes: 0 additions & 3 deletions spec/tags/core/objectspace/weakmap/delete_tags.txt

This file was deleted.

7 changes: 7 additions & 0 deletions src/main/java/org/truffleruby/collections/WeakValueCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@ public int size() {
return size;
}

@TruffleBoundary
public Value remove(Key key) {
removeStaleEntries();
final KeyedReference<Key, Value> reference = map.remove(key);
return reference == null ? null : reference.get();
}

@TruffleBoundary
public Collection<Key> keys() {
removeStaleEntries();
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/truffleruby/core/objectspace/WeakMapNodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.truffleruby.core.objectspace;

import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
import org.truffleruby.RubyContext;
import org.truffleruby.annotations.CoreMethod;
import org.truffleruby.annotations.CoreModule;
Expand Down Expand Up @@ -105,6 +106,35 @@ RubyArray getValues(RubyWeakMap map) {
}
}

@CoreMethod(names = "delete", required = 1, needsBlock = true)
public abstract static class DeleteNode extends CoreMethodArrayArgumentsNode {

@Specialization
Object delete(RubyWeakMap map, Object key, RubyProc block,
@Cached InlinedConditionProfile isContainedProfile,
@Cached CallBlockNode yieldNode) {
Object value = map.storage.remove(new CompareByRubyIdentityWrapper(key));

if (isContainedProfile.profile(this, value != null)) {
return value;
} else {
return yieldNode.yield(this, block, key);
}
}

@Specialization
Object delete(RubyWeakMap map, Object key, Nil block,
@Cached InlinedConditionProfile isContainedProfile) {
Object value = map.storage.remove(new CompareByRubyIdentityWrapper(key));

if (isContainedProfile.profile(this, value != null)) {
return value;
} else {
return nil;
}
}
}

@CoreMethod(names = { "each_key" }, needsBlock = true)
public abstract static class EachKeyNode extends CoreMethodArrayArgumentsNode {

Expand Down

0 comments on commit adb9fd1

Please sign in to comment.