-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Added StoreCache
#3701
Closed
cschuchardt88
wants to merge
10
commits into
neo-project:master
from
cschuchardt88:fix/storage-caching
Closed
Added StoreCache
#3701
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dc4effe
Added `StoreCache`
cschuchardt88 27e4ebc
Update tests/Neo.UnitTests/Collections/Caching/UT_StoreCache.cs
shargon 0406d34
Updates and fixes
cschuchardt88 a4d3b89
Merge branch 'fix/storage-caching' of https://github.com/cschuchardt8…
cschuchardt88 1baf832
Merge branch 'master' into fix/storage-caching
Jim8y b99d252
Added `StorageCache`
cschuchardt88 7e0aa54
Merge branch 'fix/storage-caching' of https://github.com/cschuchardt8…
cschuchardt88 10a69d8
typo
cschuchardt88 51572d8
Bug fixes and optimizations
cschuchardt88 a724168
Fixed memory allocation problems
cschuchardt88 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
110 changes: 110 additions & 0 deletions
110
benchmarks/Neo.Benchmarks/Collections/Caching/Benchmarks.StoreCache.cs
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 |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// Copyright (C) 2015-2025 The Neo Project. | ||
// | ||
// Benchmarks.StoreCache.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.Collections.Caching; | ||
using Neo.Persistence; | ||
using Neo.SmartContract; | ||
using System.Diagnostics; | ||
|
||
namespace Neo.Benchmark.Collections.Caching | ||
{ | ||
[MemoryDiagnoser] // Enabling Memory Diagnostics | ||
[CsvMeasurementsExporter] // Export results in CSV format | ||
[MarkdownExporter] // Exporting results in Markdown format | ||
public class Benchmarks_StoreCache | ||
{ | ||
private static readonly MemoryStore s_memoryStore = new(); | ||
private static readonly StoreCache<StorageKey, StorageItem> s_storeCache = new(s_memoryStore); | ||
private static readonly DataCache s_dataCache = new SnapshotCache(s_memoryStore); | ||
|
||
private static byte[] s_data = []; | ||
private static StorageKey s_key; | ||
private static StorageItem s_value; | ||
|
||
[GlobalSetup] | ||
public void Setup() | ||
{ | ||
if (s_data.Length == 0) | ||
{ | ||
s_data = new byte[4096]; | ||
Random.Shared.NextBytes(s_data); | ||
s_key = new StorageKey(s_data); | ||
s_value = new StorageItem(s_data); | ||
} | ||
} | ||
|
||
[Benchmark] | ||
public void TestStoreCacheAddAndUpdate() | ||
{ | ||
s_storeCache.Add(s_key, s_value); | ||
s_storeCache[s_key] = new(s_data); | ||
} | ||
|
||
[Benchmark] | ||
public void TestStoreCacheRemove() | ||
{ | ||
s_storeCache.Add(s_key, s_value); | ||
Debug.Assert(s_storeCache.Remove(s_key, out _)); | ||
} | ||
|
||
[Benchmark] | ||
public void TestStoreCacheGetAlreadyCachedData() | ||
{ | ||
s_storeCache.Add(s_key, s_value); | ||
Debug.Assert(s_storeCache.TryGetValue(s_key, out _)); | ||
Debug.Assert(s_storeCache.ContainsKey(s_key)); | ||
_ = s_storeCache[s_key]; | ||
} | ||
|
||
[Benchmark] | ||
public void TestStoreCacheGetNonCachedData() | ||
{ | ||
s_memoryStore.Put(s_key.ToArray(), s_key.ToArray()); | ||
Debug.Assert(s_storeCache.TryGetValue(s_key, out _)); | ||
Debug.Assert(s_storeCache.ContainsKey(s_key)); | ||
_ = s_storeCache[s_key]; | ||
} | ||
|
||
[Benchmark] | ||
public void TestDataCacheAddAndUpdate() | ||
{ | ||
_ = s_dataCache.GetOrAdd(s_key, () => s_value); | ||
_ = s_dataCache.GetAndChange(s_key, () => new(s_data)); | ||
} | ||
|
||
|
||
[Benchmark] | ||
public void TestDataCacheRemove() | ||
{ | ||
_ = s_dataCache.GetOrAdd(s_key, () => s_value); | ||
s_dataCache.Delete(s_key); | ||
} | ||
|
||
[Benchmark] | ||
public void TestDataCacheGetAlreadyCachedData() | ||
{ | ||
_ = s_dataCache.GetOrAdd(s_key, () => s_value); | ||
_ = s_dataCache.GetAndChange(s_key); | ||
Debug.Assert(s_dataCache.Contains(s_key)); | ||
_ = s_dataCache[s_key]; | ||
} | ||
|
||
[Benchmark] | ||
public void TestDataCacheGetNonCachedData() | ||
{ | ||
s_memoryStore.Put(s_key.ToArray(), s_key.ToArray()); | ||
_ = s_dataCache.GetAndChange(s_key); | ||
Debug.Assert(s_dataCache.Contains(s_key)); | ||
_ = s_dataCache[s_key]; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (C) 2015-2025 The Neo Project. | ||
// | ||
// IKeySerializable.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. | ||
|
||
namespace Neo.IO | ||
{ | ||
public interface IKeySerializable | ||
{ | ||
byte[] ToArray(); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the benchmarks are not the same but changing the interface? it looks like the methods are totally different
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Methods do the same thing. However,
StoreCache
class is a collection, so you can convertStoreCache
to any collection you want. This is howDataCache
and other classes inneo
should of been developed.StoreCache
inherits from