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

Build dict for IonPyDict in ioncmodule #309

Closed rmarrowstone closed 1 year ago

rmarrowstone commented 1 year ago

Profiling revealed that a significant portion of time was being taken by adding items to the IonPyDict. This change optimizes the construction of IonPyDicts by building the dicts in the ioncmodule.

The impact appears to be an improvement of about 20-25% for parsing the "service_log" benchmark files.

In implementing the PyDict code in the ioncmodule I learned that since 3.7 the standard dict type is ordered. The equality of the standard dict aligns with Ion equality and is preferable. I also learned that PyDict_SetDefault (in the C API) and dict.setdefault() (in Python) are a more efficient way to implement get|set logic.

I updated the IonPyDict python class to reflect the above.

The definition of an internal _factory class method optimizes object construction and that pattern should be applied to the other classes as well.

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.