-
Notifications
You must be signed in to change notification settings - Fork 1k
Serialized storage cache #3669
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
Open
shargon
wants to merge
59
commits into
master
Choose a base branch
from
storage-cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Serialized storage cache #3669
Changes from 36 commits
Commits
Show all changes
59 commits
Select commit
Hold shift + click to select a range
37217b6
Storage cache
shargon f0bfc3b
reduce
shargon 038a7c4
clean
shargon ccce7a4
Fix discard
shargon 4943659
Merge branch 'master' into storage-cache
shargon cca66ba
Clean
shargon 54901ed
clean
shargon 75f5a06
Merge branch 'master' into storage-cache
shargon ecdfe09
Merge branch 'master' into storage-cache
shargon 83d13cc
Clean code
shargon 5230d9c
fix cache
shargon df7ad8e
remove lock
shargon be5db33
Merge branch 'master' into storage-cache
shargon c12767a
Clean and Fix UT
shargon 601c366
Clean
shargon 6bdcc21
Anna's feedback
shargon 9c45d94
Merge branch 'master' into storage-cache
shargon 5fb4b62
Add fee values
shargon 3d25c9f
Merge branch 'storage-cache' of https://github.com/neo-project/neo in…
shargon 413d605
Merge branch 'master' into storage-cache
cschuchardt88 2dd2301
Merge branch 'master' into storage-cache
shargon 336cbac
Merge branch 'master' into storage-cache
9decaf0
```
shargon bb6ca9c
Merge branch 'master' into storage-cache
shargon a84c163
Fix conflicts
shargon 3c6d292
Optimize
shargon bf97fab
Fix
shargon 62ae410
Clean changes
shargon ac22a4d
Merge branch 'master' into storage-cache
shargon a21526f
add UT
Jim8y f0a9add
Merge branch 'storage-cache' of github.com:neo-project/neo into stora…
Jim8y a917235
Merge branch 'master' into storage-cache
shargon f4b5bcd
Merge branch 'master' into storage-cache
shargon 6d1d8a0
Clean changes
shargon 6bd4f42
Update IReadOnlyStore.cs
shargon aa3b705
Merge branch 'master' into storage-cache
shargon e4e8130
Update src/Neo/SmartContract/Native/NeoToken.cs
shargon b5f03e2
Merge branch 'master' into storage-cache
d31d182
Merge branch 'master' into storage-cache
shargon c6a7c00
Clean Store and Snapshot
shargon e503094
Clean code
shargon 13b3e50
Add ut
shargon a96bdbc
Merge branch 'master' into storage-cache
shargon 3bf2240
Update src/Neo/Persistence/SerializedCache.cs
shargon 53215b7
Update src/Neo/Persistence/SerializedCache.cs
shargon 64b2796
Update benchmarks/Neo.Benchmarks/Benchmarks.Cache.cs
shargon 898d3e6
Merge remote-tracking branch 'origin/master' into storage-cache
shargon 1d971bf
Rename to Upsert
shargon 894f979
Fix clone
shargon 3a7bf7a
Fix
shargon 1f5af5c
Merge branch 'master' into storage-cache
shargon 5f80417
Merge branch 'master' into storage-cache
shargon fa7ec14
Update Program.cs
shargon ce5ec54
Merge branch 'master' into storage-cache
shargon f7ae9de
Merge branch 'master' into storage-cache
shargon 6f30414
Merge branch 'master' into storage-cache
shargon c277a80
Fix constructor
shargon 6af2e0c
tKey
shargon 3bc447a
Update tests/Neo.Cryptography.MPTTrie.Tests/Cryptography/MPTTrie/UT_T…
shargon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // Benchmarks.Cache.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using BenchmarkDotNet.Attributes; | ||
| using Neo.Persistence; | ||
| using Neo.SmartContract; | ||
| using Neo.SmartContract.Native; | ||
| using System.Numerics; | ||
|
|
||
| namespace Neo.Benchmark | ||
| { | ||
| public class Benchmarks_Cache | ||
| { | ||
| // 256 KiB | ||
| readonly MemoryStore _store; | ||
| readonly SnapshotCache _snapshot; | ||
|
|
||
| public Benchmarks_Cache() | ||
| { | ||
| _store = new MemoryStore(); | ||
| _snapshot = new(_store.GetSnapshot()); | ||
|
|
||
| // Ledger.CurrentIndex | ||
|
|
||
| _snapshot.GetAndChange(new KeyBuilder(NativeContract.Ledger.Id, 12), () => new StorageItem(new HashIndexState() { Hash = UInt256.Zero, Index = 2 })); | ||
|
|
||
| // Gas Per block | ||
|
|
||
| _snapshot.GetAndChange(new KeyBuilder(NativeContract.NEO.Id, 29).AddBigEndian(0), () => new StorageItem(0)); | ||
| _snapshot.GetAndChange(new KeyBuilder(NativeContract.NEO.Id, 29).AddBigEndian(1), () => new StorageItem(1)); | ||
| _snapshot.GetAndChange(new KeyBuilder(NativeContract.NEO.Id, 29).AddBigEndian(2), () => new StorageItem(2)); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public void WithCache() | ||
| { | ||
| for (var x = 0; x < 1_000; x++) | ||
| { | ||
| var ret = NativeContract.NEO.GetGasPerBlock(_snapshot); | ||
| if (ret != 2) throw new Exception("Test error"); | ||
| } | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public void WithoutCache() | ||
| { | ||
| for (var x = 0; x < 1_000; x++) | ||
| { | ||
| var ret = OldCode(); | ||
| if (ret != 2) throw new Exception("Test error"); | ||
| } | ||
| } | ||
|
|
||
| private BigInteger OldCode() | ||
| { | ||
| var end = NativeContract.Ledger.CurrentIndex(_snapshot) + 1; | ||
| var last = NativeContract.NEO.GetSortedGasRecords(_snapshot, end).First(); | ||
| return last.GasPerBlock; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -11,12 +11,10 @@ | |
|
|
||
| using BenchmarkDotNet.Running; | ||
| using Neo.Benchmark; | ||
| using Neo.Benchmarks.Persistence.Benchmarks; | ||
| using Neo.SmartContract.Benchmark; | ||
|
|
||
| // BenchmarkRunner.Run<Benchmarks_PoCs>(); | ||
| BenchmarkRunner.Run<Benchmarks_UInt160>(); | ||
| BenchmarkRunner.Run<Benchmarks_Hash>(); | ||
| BenchmarkRunner.Run<Benchmarks_StorageKey>(); | ||
| BenchmarkRunner.Run<Bechmarks_ReadOnlyStoreView>(); | ||
| BenchmarkRunner.Run<Bechmarks_LevelDB>(); | ||
| BenchmarkRunner.Run<Benchmarks_Cache>(); | ||
| //BenchmarkRunner.Run<Benchmarks_UInt160>(); | ||
|
||
| //BenchmarkRunner.Run<Benchmarks_Hash>(); | ||
| //BenchmarkRunner.Run<Benchmarks_StorageKey>(); | ||
| //BenchmarkRunner.Run<Bechmarks_ReadOnlyStoreView>(); | ||
| //BenchmarkRunner.Run<Bechmarks_LevelDB>(); | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // IStorageCacheEntry.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| using Neo.SmartContract; | ||
|
|
||
| namespace Neo.Persistence | ||
| { | ||
| public interface IStorageCacheEntry | ||
| { | ||
| public StorageItem GetStorageItem(); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright (C) 2015-2025 The Neo Project. | ||
| // | ||
| // SerializedCache.cs file belongs to the neo project and is free | ||
| // software distributed under the MIT software license, see the | ||
| // accompanying file LICENSE in the main directory of the | ||
| // repository or http://www.opensource.org/licenses/mit-license.php | ||
| // for more details. | ||
| // | ||
| // Redistribution and use in source and binary forms with or without | ||
| // modifications are permitted. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace Neo.Persistence | ||
| { | ||
| public class SerializedCache | ||
| { | ||
| private readonly Dictionary<Type, IStorageCacheEntry> _cache = []; | ||
|
|
||
| /// <summary> | ||
| /// Get cached entry | ||
| /// </summary> | ||
| /// <typeparam name="T">Type</typeparam> | ||
| /// <returns>Cache</returns> | ||
shargon marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public T? Get<T>() | ||
| { | ||
| if (_cache.TryGetValue(typeof(T), out var ret)) | ||
| { | ||
| return (T)ret; | ||
| } | ||
|
|
||
| return default; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Set entry | ||
| /// </summary> | ||
| /// <typeparam name="T">Type</typeparam> | ||
| /// <param name="value">Value</param> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public void Set<T>(T? value) where T : IStorageCacheEntry | ||
| { | ||
| Set(typeof(T), value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Set entry | ||
| /// </summary> | ||
| /// <param name="type">Type</param> | ||
| /// <param name="value">Value</param> | ||
| public void Set(Type type, IStorageCacheEntry? value) | ||
| { | ||
| if (value == null) | ||
| { | ||
| Remove(type); | ||
| } | ||
| else | ||
| { | ||
| lock (_cache) | ||
| { | ||
| _cache[type] = value; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Remove entry | ||
| /// </summary> | ||
| /// <param name="type">Type</param> | ||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public void Remove(Type type) | ||
| { | ||
| lock (_cache) | ||
| { | ||
| _cache.Remove(type, out _); | ||
| } | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
| public void Clear() | ||
| { | ||
| lock (_cache) | ||
| { | ||
| _cache.Clear(); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Copy from | ||
| /// </summary> | ||
| /// <param name="value">Value</param> | ||
| public void CopyFrom(SerializedCache value) | ||
| { | ||
shargon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| lock (_cache) lock (value._cache) | ||
| { | ||
| foreach (var serialized in value._cache) | ||
| { | ||
| _cache[serialized.Key] = serialized.Value; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.