You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For context I have a library written in Python designed for Python. Parts of it are now written in Rust (where it has enhanced performance). I would like to transition more to Rust to continue to speed things up. Creating a fully functioning Rust replicable library is, however, only a secondary goal and not the primary.
The challenge I have is that the bottlenecks end up being in a high layer so to get to those I need to port every lower layer and dependent object to Rust first in order to both maintain compatibility and allow calculations to occur in Rust. But this can introduce slowdowns.
Here is a concrete example. Suppose I create some (supposed) immutable data object in Python and time its data fetching usage...
If I now port these objects to Rust, there is overall degradation of performance of using them due to the TypeConversion cost and memory clone. Unless the ...calculation part of this makes up some speed it is not really a very good atomic addition.
For simple float types this isn't that bad but the datetime/ NaiveDateTime is much weaker (and these lists are very small).
I am considering providing an interim solution by using cached Python wrappers.
fromfunctoolsimportcached_propertyclassPyRsDataProvider:
vals=cached_property(lambdaself: self.obj.vals)
val=cached_property(lambdaself: self.obj.val)
dates=cached_property(lambdaself: self.obj.dates)
def__init__(self):
self.obj=RsDataProvider()
obj=PyRsDataProvider()
%timeitobj.dates[3] # ONLY 1.5x WORSE: 42.1 ns ± 1.96 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)%timeitobj.vals[3] # ONLY 1.5x WORSE: 39.4 ns ± 1.83 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)%timeitobj.val# ONLY 1.5x WORSE: 32.6 ns ± 0.958 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
This allows reasonable preservation of speed in Python but the struct is directly available in Rust to continue to perform calculations there. Problem is its quite a bit of work and requires very careful structuring.
Is there another design pattern besides this that I am missing, or is my line of thinking broadly on the right track?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
For context I have a library written in Python designed for Python. Parts of it are now written in Rust (where it has enhanced performance). I would like to transition more to Rust to continue to speed things up. Creating a fully functioning Rust replicable library is, however, only a secondary goal and not the primary.
The challenge I have is that the bottlenecks end up being in a high layer so to get to those I need to port every lower layer and dependent object to Rust first in order to both maintain compatibility and allow calculations to occur in Rust. But this can introduce slowdowns.
Here is a concrete example. Suppose I create some (supposed) immutable data object in Python and time its data fetching usage...
If I now port these objects to Rust, there is overall degradation of performance of using them due to the TypeConversion cost and memory
clone
. Unless the...calculation
part of this makes up some speed it is not really a very good atomic addition.In Python I now achieve the speeds:
For simple float types this isn't that bad but the
datetime
/NaiveDateTime
is much weaker (and these lists are very small).I am considering providing an interim solution by using cached Python wrappers.
This allows reasonable preservation of speed in Python but the struct is directly available in Rust to continue to perform calculations there. Problem is its quite a bit of work and requires very careful structuring.
Is there another design pattern besides this that I am missing, or is my line of thinking broadly on the right track?
Beta Was this translation helpful? Give feedback.
All reactions