amazon-ion / ion-python

A Python implementation of Amazon Ion.
https://amazon-ion.github.io/ion-docs/
Apache License 2.0
260 stars 51 forks source link

Optimize ionc_write_struct #334

Closed linlin-s closed 10 months ago

linlin-s commented 10 months ago

Issue #, if available:

Description of changes:

This PR optimized the handling of IonPyDict objects. In the previous implementation, we converted the PyObject map into a sequence for iteration. The updated approach introduced in this PR separates the handling into two cases based on the type of the map object:

  1. For Python dictionaries: When map is a standard Python dictionary, we iterate over it directly using PyDict_Next.
  2. For IonPyDict objects: In cases where map is an instance of IonPyDict, we now access its _ _store attribute. This attribute itself is a Python dictionary. By doing so, we can then use PyDict_Next to iterate over __store, similar to how we handle standard Python dictionaries.

This optimization eliminates the need to convert IonPyDict objects into sequences before iteration, leading to a more efficient processing process.

Here are the benchmark results from benchmarking ion-python writing using service_log_legacy: There is 31.2% performance improvement from the change.

To reproduce this results, please run: python ion_benchmark_cli.py write --iterations 30 --warmups 10 --io-type fileservice_log_legacy.ion --format ion_binary

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. name file_size(B) time_min(ns) time_mean(ns) memory_usage_peak(B)
Before 21270622 1715543333.40 1753941142.77 42597119
After 21270622 1174766083.40 1206556276.95 42582427