Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(python): Pre-allocate size for the dictionary #1949

Merged
merged 2 commits into from
Nov 20, 2024
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
4 changes: 4 additions & 0 deletions integration_tests/cpython_benchmark/fury_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"view_count": 7,
"zip": "",
}
LARGE_DICT = {str(i): i for i in range(2**10 + 1)}

TUPLE = (
[
Expand Down Expand Up @@ -177,6 +178,9 @@ def micro_benchmark():
runner.parse_args()
language = pyfury.Language.XLANG if args.xlang else pyfury.Language.PYTHON
runner.bench_func("fury_dict", fury_object, language, not args.no_ref, DICT)
runner.bench_func(
"fury_large_dict", fury_object, language, not args.no_ref, LARGE_DICT
)
runner.bench_func(
"fury_dict_group", fury_object, language, not args.no_ref, DICT_GROUP
)
Expand Down
7 changes: 4 additions & 3 deletions python/pyfury/_serialization.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ cdef extern from *:
"""
object int2obj(int64_t obj_addr)
int64_t obj2int(object obj_ref)
dict _PyDict_NewPresized(Py_ssize_t minused)


cdef int8_t NULL_FLAG = -3
Expand Down Expand Up @@ -2081,9 +2082,9 @@ cdef class MapSerializer(Serializer):
cpdef inline read(self, Buffer buffer):
cdef MapRefResolver ref_resolver = self.ref_resolver
cdef ClassResolver class_resolver = self.class_resolver
cdef dict map_ = {}
ref_resolver.reference(map_)
cdef int32_t len_ = buffer.read_varint32()
cdef dict map_ = _PyDict_NewPresized(len_)
ref_resolver.reference(map_)
cdef int32_t ref_id
cdef ClassInfo key_classinfo
cdef ClassInfo value_classinfo
Expand Down Expand Up @@ -2131,7 +2132,7 @@ cdef class MapSerializer(Serializer):

cpdef inline xread(self, Buffer buffer):
cdef int32_t len_ = buffer.read_varint32()
cdef dict map_ = {}
cdef dict map_ = _PyDict_NewPresized(len_)
self.fury.ref_resolver.reference(map_)
for i in range(len_):
k = self.fury.xdeserialize_ref(
Expand Down
Loading