Skip to content

Commit 4e7465a

Browse files
committed
Reuse hash
1 parent 0595ee7 commit 4e7465a

1 file changed

Lines changed: 26 additions & 6 deletions

File tree

Modules/_json.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "Python.h"
1212
#include "pycore_ceval.h" // _Py_EnterRecursiveCall()
13+
#include "pycore_dict.h" // _PyDict_SetItem_KnownHash()
1314
#include "pycore_global_strings.h" // _Py_ID()
1415
#include "pycore_pyerrors.h" // _PyErr_FormatNote
1516
#include "pycore_runtime.h" // _PyRuntime
@@ -1523,16 +1524,23 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
15231524
}
15241525
else {
15251526
PyObject *ident = NULL;
1527+
Py_hash_t ident_hash = -1;
15261528
if (s->markers != Py_None) {
15271529
Py_ssize_t len;
15281530

15291531
ident = PyLong_FromVoidPtr(obj);
15301532
if (ident == NULL)
15311533
return -1;
15321534

1535+
ident_hash = PyObject_Hash(ident);
1536+
if (ident_hash == -1) {
1537+
Py_DECREF(ident);
1538+
return -1;
1539+
}
1540+
15331541
len = PyDict_GET_SIZE(s->markers);
15341542

1535-
if (PyDict_SetItem(s->markers, ident, obj)) {
1543+
if (_PyDict_SetItem_KnownHash(s->markers, ident, obj, ident_hash)) {
15361544
Py_DECREF(ident);
15371545
return -1;
15381546
}
@@ -1564,7 +1572,7 @@ encoder_listencode_obj(PyEncoderObject *s, PyUnicodeWriter *writer,
15641572
return -1;
15651573
}
15661574
if (ident != NULL) {
1567-
if (PyDict_DelItem(s->markers, ident)) {
1575+
if (_PyDict_DelItem_KnownHash(s->markers, ident, ident_hash)) {
15681576
Py_XDECREF(ident);
15691577
return -1;
15701578
}
@@ -1655,6 +1663,7 @@ encoder_listencode_dict(PyEncoderObject *s, PyUnicodeWriter *writer,
16551663
PyObject *ident = NULL;
16561664
PyObject *items = NULL;
16571665
PyObject *key, *value;
1666+
Py_hash_t ident_hash = -1;
16581667
bool first = true;
16591668

16601669
if (PyDict_GET_SIZE(dct) == 0) {
@@ -1669,9 +1678,14 @@ encoder_listencode_dict(PyEncoderObject *s, PyUnicodeWriter *writer,
16691678
if (ident == NULL)
16701679
return -1;
16711680

1681+
ident_hash = PyObject_Hash(ident);
1682+
if (ident_hash == -1) {
1683+
goto bail;
1684+
}
1685+
16721686
len = PyDict_GET_SIZE(s->markers);
16731687

1674-
if (PyDict_SetItem(s->markers, ident, dct)) {
1688+
if (_PyDict_SetItem_KnownHash(s->markers, ident, dct, ident_hash)) {
16751689
goto bail;
16761690
}
16771691

@@ -1726,7 +1740,7 @@ encoder_listencode_dict(PyEncoderObject *s, PyUnicodeWriter *writer,
17261740
}
17271741

17281742
if (ident != NULL) {
1729-
if (PyDict_DelItem(s->markers, ident))
1743+
if (_PyDict_DelItem_KnownHash(s->markers, ident, ident_hash))
17301744
goto bail;
17311745
Py_CLEAR(ident);
17321746
}
@@ -1756,6 +1770,7 @@ encoder_listencode_list(PyEncoderObject *s, PyUnicodeWriter *writer,
17561770
PyObject *ident = NULL;
17571771
PyObject *s_fast = NULL;
17581772
Py_ssize_t i;
1773+
Py_hash_t ident_hash = -1;
17591774

17601775
ident = NULL;
17611776
s_fast = PySequence_Fast(seq, "_iterencode_list needs a sequence");
@@ -1773,9 +1788,14 @@ encoder_listencode_list(PyEncoderObject *s, PyUnicodeWriter *writer,
17731788
if (ident == NULL)
17741789
return -1;
17751790

1791+
ident_hash = PyObject_Hash(ident);
1792+
if (ident_hash == -1) {
1793+
goto bail;
1794+
}
1795+
17761796
len = PyDict_GET_SIZE(s->markers);
17771797

1778-
if (PyDict_SetItem(s->markers, ident, seq)) {
1798+
if (_PyDict_SetItem_KnownHash(s->markers, ident, seq, ident_hash)) {
17791799
goto bail;
17801800
}
17811801

@@ -1811,7 +1831,7 @@ encoder_listencode_list(PyEncoderObject *s, PyUnicodeWriter *writer,
18111831
}
18121832
}
18131833
if (ident != NULL) {
1814-
if (PyDict_DelItem(s->markers, ident))
1834+
if (_PyDict_DelItem_KnownHash(s->markers, ident, ident_hash))
18151835
goto bail;
18161836
Py_CLEAR(ident);
18171837
}

0 commit comments

Comments
 (0)