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
Reuse single shared allocation for ABI data (#970)
Currently whenever you create a new instance of a contract the entire
`Abi` gets cloned (which can be really huge) plus a bunch of signatures
get computed (a lot of hashing) and cached. This is really costly
considering that the `Abi` of a contract never changes so it would be an
easy win to share the same allocation of those immutable pieces of data
(`Abi` and computed hashes) across all instances of the same contract.
This would make the creation of subsequent instances of the same
contract mostly free.
This can already be achieved with ugly workarounds discussed and
implemented [here](cowprotocol/services#2628)
but if this issue gets resolved in the library itself manual allocation
optimization will not be needed and the performance improvement for any
code that creates a lot of contract instances would be huge.
The actual change is overall pretty small. I created a new type
`Interface` that contains all the immutable data derived from the `Abi`.
Then I gave it custom `(De)Serialize` implementations which mostly just
do the same thing it did before (defer to `Abi`'s implementation). The
`Deserialize` implementation of course also computes the important hash
and stores it's values together with the `Abi`.
Because the typesafe rust binding code generated by `ethcontract-rs`
already stores the raw contract in a static allocation (which now
contains the relevant immutable state `Arc`ed) we now get cheap contract
instantiations.
Thanks @nlordell for the idea of fixing the performance issue directly
in the library. 👍
The code also has a few unrelated changes to make the `nightly` CI build
pass.
### Breaking changes
* `Artifact::insert` can now run into annoying borrow checker issues
(that can be worked around, though)
* `Contract:abi` is now accessible with `Contract::interface::abi`
* `Contract::interface::abi` is harder to mutate than `Contract::abi`
due to the introduction of the `Arc`
Overall I don't know how many people actually rely on this crate and
whether or not these breaking changes would be a big deal for them. But
even if this should not get merged into the main release of the crate I
think this patch is still useful for anybody who needs to optimize
performance across the board.
### Test Plan
existing tests
ran cow protocol backend using the patched library and didn't see any
obvious issues
0 commit comments