Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# The Universal Permissive License (UPL), Version 1.0
#
# Subject to the condition set forth below, permission is hereby granted to any
# person obtaining a copy of this software, associated documentation and/or
# data (collectively the "Software"), free of charge and under any and all
# copyright rights in the Software, and any and all patent rights owned or
# freely licensable by each licensor hereunder covering either (i) the
# unmodified Software as contributed to or provided by such licensor, or (ii)
# the Larger Works (as defined below), to deal in both
#
# (a) the Software, and
#
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
# one is included with the Software each a "Larger Work" to which the Software
# is contributed by such licensors),
#
# without restriction, including without limitation the rights to copy, create
# derivative works of, display, perform, and distribute the Software and make,
# use, sell, offer for sale, import, export, have made, and have sold the
# Software and the Larger Work(s), and to sublicense the foregoing rights on
# either these or other terms.
#
# This license is subject to the following condition:
#
# The above copyright notice and either this complete permission notice or at a
# minimum a reference to the UPL must be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


"""Same workload as object-dir, rejected only by the final namespace-access guard."""


_type_bases = type.__dict__["__bases__"]


class Meta(type):
@property
def __bases__(cls):
# Preserve the hierarchy, but require Python descriptor dispatch.
# Calling type.__getattribute__(cls, "__bases__") would recurse here.
return _type_bases.__get__(cls)


class Base(metaclass=Meta):
pass


class Config(Base):
pass


# Overlapping class and instance attributes exercise duplicate elimination.
# Use setattr, not a replacement __dict__, to retain DynamicObjectStorage.
for i in range(32):
setattr(Base, "attr_%02d" % i, i)
setattr(Config, "attr_%02d" % (i + 16), i)

objects = []
results = []
expected = []


def meta_attribute(name):
for cls in Meta.__mro__:
if name in cls.__dict__:
return cls.__dict__[name]
return None


def __setup__(num=10000):
if num < 1:
raise ValueError("num must be positive")
# Mirror hasStandardNamespaceAccess without invoking the bases property.
assert meta_attribute("__getattribute__") is type.__getattribute__
assert meta_attribute("__getattr__") is None
assert meta_attribute("__dict__") is type.__dict__["__dict__"]
assert meta_attribute("__bases__") is not type.__dict__["__bases__"]

objects.clear()
results.clear()
expected.clear()
for index in range(4):
obj = Config()
for i in range(32, 64):
setattr(obj, "attr_%02d" % i, i)
setattr(obj, "instance_%d" % index, index)
obj.removed = None
del obj.removed
objects.append(obj)
results.append(None)
names = set(obj.__dict__)
for cls in Config.__mro__:
names.update(cls.__dict__)
expected.append(sorted(names))


def __benchmark__(num=10000):
# Retain the actual lists, not just their lengths, so names must be produced.
# Object/class construction and result validation are outside the timed loop.
for _ in range(num):
for index, obj in enumerate(objects):
results[index] = dir(obj)
return results


def __cleanup__(num=10000):
assert results == expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# The Universal Permissive License (UPL), Version 1.0
#
# Subject to the condition set forth below, permission is hereby granted to any
# person obtaining a copy of this software, associated documentation and/or
# data (collectively the "Software"), free of charge and under any and all
# copyright rights in the Software, and any and all patent rights owned or
# freely licensable by each licensor hereunder covering either (i) the
# unmodified Software as contributed to or provided by such licensor, or (ii)
# the Larger Works (as defined below), to deal in both
#
# (a) the Software, and
#
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
# one is included with the Software each a "Larger Work" to which the Software
# is contributed by such licensors),
#
# without restriction, including without limitation the rights to copy, create
# derivative works of, display, perform, and distribute the Software and make,
# use, sell, offer for sale, import, export, have made, and have sold the
# Software and the Larger Work(s), and to sublicense the foregoing rights on
# either these or other terms.
#
# This license is subject to the following condition:
#
# The above copyright notice and either this complete permission notice or at a
# minimum a reference to the UPL must be included in all copies or substantial
# portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


"""Repeated instance dir() with an eligible custom metaclass."""


class Meta(type):
pass


class Base(metaclass=Meta):
pass


class Config(Base):
pass


# Overlapping class and instance attributes exercise duplicate elimination.
# Use setattr, not a replacement __dict__, to retain DynamicObjectStorage.
for i in range(32):
setattr(Base, "attr_%02d" % i, i)
setattr(Config, "attr_%02d" % (i + 16), i)

objects = []
results = []
expected = []


def meta_attribute(name):
for cls in Meta.__mro__:
if name in cls.__dict__:
return cls.__dict__[name]
return None


def __setup__(num=10000):
if num < 1:
raise ValueError("num must be positive")
# Mirror hasStandardNamespaceAccess without invoking the bases property.
assert meta_attribute("__getattribute__") is type.__getattribute__
assert meta_attribute("__getattr__") is None
assert meta_attribute("__dict__") is type.__dict__["__dict__"]
assert meta_attribute("__bases__") is type.__dict__["__bases__"]

objects.clear()
results.clear()
expected.clear()
for index in range(4):
obj = Config()
for i in range(32, 64):
setattr(obj, "attr_%02d" % i, i)
setattr(obj, "instance_%d" % index, index)
obj.removed = None
del obj.removed
objects.append(obj)
results.append(None)
names = set(obj.__dict__)
for cls in Config.__mro__:
names.update(cls.__dict__)
expected.append(sorted(names))


def __benchmark__(num=10000):
# Retain the actual lists, not just their lengths, so names must be produced.
# Object/class construction and result validation are outside the timed loop.
for _ in range(num):
for index, obj in enumerate(objects):
results[index] = dir(obj)
return results


def __cleanup__(num=10000):
assert results == expected
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,44 @@ public boolean execute(Frame frame, Node inliningTarget, Object a, Object b, Ric
}
}

@Test
public void testEnsureCapacity() {
ObjectHashMap map = new ObjectHashMap();
LinkedHashMap<Long, Object> expected = new LinkedHashMap<>();
map.ensureCapacity(0);
assertEqual("empty reservation", expected, map);

map.ensureCapacity(32);
for (long key = 0; key < 32; key++) {
Object value = newValue();
put(map, key, getKeyHash(key), value);
expected.put(key, value);
}
map.ensureCapacity(map.size());
map.ensureCapacity(33);
assertEqual("reservation within existing capacity", expected, map);

removeValues(map, expected, new Random(42), 4);
map.ensureCapacity(map.size());
map.ensureCapacity(map.size() + 20);
assertEqual("reservation after deletion", expected, map);
for (long key = 32; key < 52; key++) {
Object value = newValue();
put(map, key, getKeyHash(key), value);
expected.put(key, value);
}
assertEqual("insertions after reservation", expected, map);

ObjectHashMap fullMap = new ObjectHashMap();
Object value = newValue();
put(fullMap, 0L, getKeyHash(0L), value);
LinkedHashMap<Long, Object> singleEntry = new LinkedHashMap<>();
singleEntry.put(0L, value);
// Clamp speculative capacity, including requests exceeding Integer.MAX_VALUE.
fullMap.ensureCapacity((long) fullMap.size() + Integer.MAX_VALUE);
assertEqual("bounded reservation", singleEntry, fullMap);
}

@Test
public void testCollisionsByPuttingManyKeysWithSameHash() {
ObjectHashMap map = new ObjectHashMap();
Expand Down
29 changes: 29 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -1978,3 +1978,32 @@ class Test:
del o.foo

assert "foo" not in o.__dict__


def test_bulk_update_after_deletions():
d = {i: i for i in range(128)}
for i in range(0, 128, 2):
del d[i]
incoming = {i: -i for i in range(64, 256)}
expected_order = list(d) + [i for i in incoming if i not in d]
d.update(incoming)
assert list(d) == expected_order
assert all(d[i] == (-i if i >= 64 else i) for i in d)
snapshot = list(d.items())
d.update(d)
d.update({})
d.update(incoming)
assert list(d.items()) == snapshot


def test_bulk_value_update_preserves_iterators():
d = {i: i for i in range(128)}
del d[0]
expected = list(d)
it = iter(d)
reverse = reversed(d)
assert next(it) == expected[0]
assert next(reverse) == expected[-1]
d.update({k: -v for k, v in d.items()})
assert list(it) == expected[1:]
assert list(reverse) == expected[-2::-1]
Loading
Loading