Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cache to native contract executions #1764

Closed

Conversation

shargon
Copy link
Member

@shargon shargon commented Jul 13, 2020

If we add a memory cache to the snapshot, we can improve the access to native contracts.

  • Tests was done with memory storage.
  • If you think that we can focus this cache in another way, I can change it.

Benchmarks:

        [TestMethod]
        public void TestGetValidators1()
        {
            for (int x = 0; x < 1_000; x++)
            {
                using (ApplicationEngine engine = NativeContract.NEO.TestCall("getValidators"))
                {
                    var result = engine.ResultStack.Peek();
                    result.GetType().Should().Be(typeof(VM.Types.Array));
                    ((VM.Types.Array)result).Count.Should().Be(7);
                    ((VM.Types.Array)result)[0].GetSpan().ToHexString().Should().Be("02486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a70");
                    ((VM.Types.Array)result)[1].GetSpan().ToHexString().Should().Be("024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d");
                    ((VM.Types.Array)result)[2].GetSpan().ToHexString().Should().Be("02aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e");
                    ((VM.Types.Array)result)[3].GetSpan().ToHexString().Should().Be("03b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c");
                    ((VM.Types.Array)result)[4].GetSpan().ToHexString().Should().Be("03b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a");
                    ((VM.Types.Array)result)[5].GetSpan().ToHexString().Should().Be("02ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba554");
                    ((VM.Types.Array)result)[6].GetSpan().ToHexString().Should().Be("02df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e895093");
                }
            }
        }

Without PR: Duration: 6.6 sec

Adding this at the begining

using (ApplicationEngine engine = NativeContract.NEO.TestCall("getValidators"))
                engine.Snapshot.Commit();

With PR: Duration: 1.1 sec

More than 500% of performance, we skip storage access, and serializations.

@shargon shargon requested a review from erikzhang July 13, 2020 10:05
@erikzhang
Copy link
Member

In a real-world scenario, we will share the snapshot every time we call, instead of creating a new one. So the internal cache of snapshot will improve performance.

@shargon
Copy link
Member Author

shargon commented Jul 14, 2020

Closed in favor of #1766

@shargon shargon closed this Jul 14, 2020
@shargon shargon deleted the cache-native-contract-execution branch July 14, 2020 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants