Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(python): Directly access the key-value pairs of a dict (#1970)
## What does this PR do? In Python, to implement a linear memory structure that stores key-value pairs, we can traverse them in the order of insertion like accessing an array. However, Cython does not provide a direct access interface, and these interfaces are internal in CPython, requiring compatibility work to use them correctly. Nevertheless, we can still use the`PyDict_Next` interface to replace the `items` method. Essentially, `items` use `PyDict_Next` to append to a list. Doing so can reduce the copying overhead. ## Related issues ## Does this PR introduce any user-facing change? - [ ] Does this PR introduce any public API change? - [ ] Does this PR introduce any binary protocol compatibility change? ## Benchmark For large dict ``` [dict_item] 541 us +- 39 us -> [dict_next] 535 us +- 35 us: 1.00x faster [dict_item] 119.8 MiB +- 1344.0 KiB -> [dict_next] 118.8 MiB +- 1338.4 KiB: 1.01x faster ```
- Loading branch information