Skip to content

Commit c22ecb4

Browse files
committed
Cache memoryview hash
1 parent 4b688cd commit c22ecb4

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_memoryview.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceSliceTest.test_delitem
2929
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceSliceTest.test_gc
3030
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceSliceTest.test_getbuf_fail
31+
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceSliceTest.test_hash
3132
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceSliceTest.test_hash_writable
3233
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceSliceTest.test_iter
3334
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceSliceTest.test_release
@@ -43,6 +44,7 @@
4344
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceTest.test_delitem
4445
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceTest.test_gc
4546
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceTest.test_getbuf_fail
47+
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceTest.test_hash
4648
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceTest.test_hash_writable
4749
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceTest.test_iter
4850
*graalpython.lib-python.3.test.test_memoryview.BytesMemorySliceTest.test_release
@@ -58,6 +60,7 @@
5860
*graalpython.lib-python.3.test.test_memoryview.BytesMemoryviewTest.test_delitem
5961
*graalpython.lib-python.3.test.test_memoryview.BytesMemoryviewTest.test_gc
6062
*graalpython.lib-python.3.test.test_memoryview.BytesMemoryviewTest.test_getbuf_fail
63+
*graalpython.lib-python.3.test.test_memoryview.BytesMemoryviewTest.test_hash
6164
*graalpython.lib-python.3.test.test_memoryview.BytesMemoryviewTest.test_hash_writable
6265
*graalpython.lib-python.3.test.test_memoryview.BytesMemoryviewTest.test_iter
6366
*graalpython.lib-python.3.test.test_memoryview.BytesMemoryviewTest.test_release

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/IntrinsifiedPMemoryView.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public class IntrinsifiedPMemoryView extends PythonBuiltinObject {
4545
private BufferReference reference;
4646
private int flags;
4747

48+
// Cached hash value, required to compy with CPython's semantics
49+
private int cachedHash = -1;
50+
4851
public IntrinsifiedPMemoryView(Object cls, Shape instanceShape, MemoryViewNodes.BufferReferences references, ManagedBuffer managedBuffer, Object owner,
4952
int len, boolean readonly, int itemsize, String formatString, int ndim, Object bufPointer,
5053
int offset, int[] shape, int[] strides, int[] suboffsets, int flags) {
@@ -225,6 +228,14 @@ public BufferReference getReference() {
225228
return reference;
226229
}
227230

231+
public int getCachedHash() {
232+
return cachedHash;
233+
}
234+
235+
public void setCachedHash(int cachedHash) {
236+
this.cachedHash = cachedHash;
237+
}
238+
228239
public void setReleased() {
229240
flags |= FLAG_RELEASED;
230241
if (reference != null) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/memoryview/MemoryViewBuiltins.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,15 +551,20 @@ static String repr(IntrinsifiedPMemoryView self) {
551551
public static abstract class HashNode extends PythonUnaryBuiltinNode {
552552
@Specialization
553553
int hash(IntrinsifiedPMemoryView self,
554+
@Cached ConditionProfile cachedProfile,
554555
@Cached ConditionProfile writableProfile,
555556
@Cached MemoryViewNodes.ToJavaBytesNode toJavaBytesNode) {
557+
if (cachedProfile.profile(self.getCachedHash() != -1)) {
558+
return self.getCachedHash();
559+
}
556560
self.checkReleased(this);
557561
if (writableProfile.profile(!self.isReadOnly())) {
558562
throw raise(ValueError, ErrorMessages.CANNOT_HASH_WRITEABLE_MEMORYVIEW);
559563
} else {
560564
// TODO avoid copying
561-
// TODO save the value
562-
return hashArray(toJavaBytesNode.execute(self));
565+
int hash = hashArray(toJavaBytesNode.execute(self));
566+
self.setCachedHash(hash);
567+
return hash;
563568
}
564569
}
565570

0 commit comments

Comments
 (0)