-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
db4a20f
commit b75f193
Showing
21 changed files
with
219 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "ethcontract-common" | ||
version = "0.25.5" | ||
version = "0.25.6" | ||
authors = ["Gnosis developers <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
|
@@ -14,7 +14,7 @@ Common types for ethcontract-rs runtime and proc macro. | |
[dependencies] | ||
ethabi = "18.0" | ||
hex = "0.4" | ||
serde = "1.0" | ||
serde= { version = "1.0", features = ["rc"] } | ||
serde_derive = "1.0" | ||
serde_json = "1.0" | ||
thiserror = "1.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "ethcontract-derive" | ||
version = "0.25.5" | ||
version = "0.25.6" | ||
authors = ["Gnosis developers <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
|
@@ -20,8 +20,8 @@ proc-macro = true | |
|
||
[dependencies] | ||
anyhow = "1.0" | ||
ethcontract-common = { version = "0.25.5", path = "../ethcontract-common" } | ||
ethcontract-generate = { version = "0.25.5", path = "../ethcontract-generate", default-features = false } | ||
ethcontract-common = { version = "0.25.6", path = "../ethcontract-common" } | ||
ethcontract-generate = { version = "0.25.6", path = "../ethcontract-generate", default-features = false } | ||
proc-macro2 = "1.0" | ||
quote = "1.0" | ||
syn = "2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "ethcontract-generate" | ||
version = "0.25.5" | ||
version = "0.25.6" | ||
authors = ["Gnosis developers <[email protected]>"] | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
|
@@ -18,7 +18,7 @@ http = ["curl"] | |
[dependencies] | ||
anyhow = "1.0" | ||
curl = { version = "0.4", optional = true } | ||
ethcontract-common = { version = "0.25.5", path = "../ethcontract-common" } | ||
ethcontract-common = { version = "0.25.6", path = "../ethcontract-common" } | ||
Inflector = "0.11" | ||
proc-macro2 = "1.0" | ||
quote = "1.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.