Point72 / csp

csp is a high performance reactive stream processing library, written in C++ and Python
https://github.com/Point72/csp/wiki
Apache License 2.0
150 stars 27 forks source link

Add native to_dict method for structs #267

Closed arhamchopra closed 4 weeks ago

arhamchopra commented 1 month ago

This PR adds a new to_dict method for structs which is written fully in C++. The older python based to_dict method has been renamed to to_dict_depr.

Motivation: The newer C++ based to_dict method is faster than the original python based to_dict method. Some timing examples are shown below:

class A(csp.Struct):
  a: int
  b: str
  c: bool
  d: float
a = A(a=1, b="1", c=True, d=1.0)
%timeit a.to_dict()         # 0.888 microseconds
%timeit a.to_dict_depr()    # 4.98 microseconds

class E(csp.Struct):
  a: list
  b: dict
  c: tuple
  d: set
e = E(a=[1,2,3], b={1:1, 2:2, 3:3}, c=(1,2,3), d={1,2,3})
%timeit e.to_dict()        # 2.65 microseconds
%timeit e.to_dict_depr()   # 15.4 microseconds