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

[Change] DataCache to Match LevelDbStore & RocksDbStore #3709

Closed

Conversation

cschuchardt88
Copy link
Member

@cschuchardt88 cschuchardt88 commented Feb 3, 2025

Description

Mostly bug fixes and same data results as the Leveldb and Rocksdb.

BenchmarkDotNet v0.14.0, Windows 11 (10.0.26100.3037)
Intel Core i7-8700K CPU 3.70GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET SDK 9.0.102
  [Host]   : .NET 9.0.1 (9.0.124.61010), X64 RyuJIT AVX2
  .NET 9.0 : .NET 9.0.1 (9.0.124.61010), X64 RyuJIT AVX2

Job=.NET 9.0  OutlierMode=DontRemove  Runtime=.NET 9.0  
Concurrent=True  Force=False  Server=True  
Method Mean Error StdDev Median Min Max
TestOldDataCacheAdd 0.0009 ns 0.0023 ns 0.0021 ns 0.0000 ns 0.0000 ns 0.0080 ns
TestDataCacheAdd 0.0002 ns 0.0008 ns 0.0008 ns 0.0000 ns 0.0000 ns 0.0030 ns
TestOldDataCacheUpdate 0.0030 ns 0.0085 ns 0.0080 ns 0.0000 ns 0.0000 ns 0.0310 ns
TestDataCacheUpdate 0.0141 ns 0.0143 ns 0.0134 ns 0.0102 ns 0.0000 ns 0.0406 ns
TestOldDataCacheRemove 35.8684 ns 0.2957 ns 0.2766 ns 35.8496 ns 35.3293 ns 36.2868 ns
TestDataCacheRemove 78.9549 ns 0.5450 ns 0.5098 ns 78.7148 ns 78.3838 ns 80.0923 ns
TestOldDataCacheGet 0.0127 ns 0.0122 ns 0.0115 ns 0.0107 ns 0.0000 ns 0.0321 ns
TestDataCacheGet 0.0107 ns 0.0102 ns 0.0095 ns 0.0094 ns 0.0000 ns 0.0325 ns

Type of change

  • Optimization (the change is only an optimization)
  • Style (the change is only a code style for better maintenance or standard purpose)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

image

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@cschuchardt88 cschuchardt88 changed the title ['Change'] DataCache to Match LevelDbStore & RocksDbStore [Change] DataCache to Match LevelDbStore & RocksDbStore Feb 3, 2025
@cschuchardt88 cschuchardt88 added Discussion Initial issue state - proposed but not yet accepted Waiting for Review labels Feb 3, 2025
return y.AsSpan().SequenceCompareTo(x.AsSpan());
return x.AsSpan().SequenceCompareTo(y.AsSpan());

return x.AsSpan().SequenceCompareTo(y.AsSpan()) * _direction;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about submitting the changes in this first in another PR?
Perhaps this can be discussed faster and merged quicker.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something like

        public int Compare(byte[]? x, byte[]? y)
        {
            if (ReferenceEquals(x, y)) return 0;
            if (x is null) return y is null ? 0 : -_direction;
            if (y is null) return _direction;

            return _direction * x.AsSpan().SequenceCompareTo(y.AsSpan());
        }

Is the length multiplication really needed?

byte[] a = new byte[10];
byte[] b = new byte[20];
ByteArrayComparer comparer = ByteArrayComparer.Default;

Console.WriteLine(comparer.Compare(null, a)); // Expected: -1, Original: -10
Console.WriteLine(comparer.Compare(null, b)); // Expected: -1, Original: -20

Copy link
Member Author

@cschuchardt88 cschuchardt88 Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im sorry, I mean MemorySnapshot was returning all results for null.

@@ -547,7 +546,7 @@ public void TestBlockchain_ListContracts()
Assert.AreEqual(state.Hash, NativeContract.ContractManagement.GetContract(engine.SnapshotCache, state.Hash).Hash);

var list2 = NativeContract.ContractManagement.ListContracts(engine.SnapshotCache);
Assert.AreEqual(list.Count(), list2.Count());
Assert.AreEqual(list.Count(), list2.Count() - 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this changed?

Copy link
Member Author

@cschuchardt88 cschuchardt88 Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you look up a couple of lines there is a new contract that got added. So the test would be invalid.

10 != 11

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test didn't change, this line was before

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was an issue with DataCache being bugged. Let me find out what happening, so I can go into more detail about it.

@@ -290,7 +290,7 @@ public void TestFindRange()
Assert.IsTrue(items[0].Value.EqualsTo(value5));
Assert.AreEqual(key4, items[1].Key);
Assert.IsTrue(items[1].Value.EqualsTo(value4));
Assert.AreEqual(2, items.Length);
Assert.AreEqual(3, items.Length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this changed, and not in the other PR?

Copy link
Member Author

@cschuchardt88 cschuchardt88 Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it from starting key to ending end. It's not in between. We don't skip the 1st one.

// OLD
FindRange([0x00, 0x00, 0x04], [0x00, 0x00, 0x03], Backwards);
// Results: Empty

// NEW
FindRange([0x00, 0x00, 0x04], [0x00, 0x00, 0x03], Backwards);
// Results:
// [0x00, 0x00, 0x04]
// [0x00, 0x00, 0x03]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you move this fix into the other pr, and here only the DataCache refactor?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this part of the DataCache?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrote all the code. So it would be hard for me to put back the bug.

@shargon
Copy link
Member

shargon commented Feb 6, 2025

Replace the name, and change the logic, makes harder the review, I reverted the name replacement, you can do it in the next pr (i'm not against to) but in the same PR, makes this review harder and slower.

@@ -502,7 +502,7 @@ public ExecutionContext LoadScript(Script script, int rvcount = -1, int initialP
// Create and configure context
ExecutionContext context = CreateContext(script, rvcount, initialPosition);
ExecutionContextState state = context.GetState<ExecutionContextState>();
state.SnapshotCache = SnapshotCache?.CloneCache();
state.SnapshotCache = SnapshotCache;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't remove this clone, it affects the try/catch safety

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All CloneCache doesn't is the same as SnapshotCache. No data buffers are moved, neither any caches. CloneCache is just a wrapper class. I see the point of it. And if you are right why all tests work still?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All CloneCache doesn't is the same as SnapshotCache. No data buffers are moved, neither any caches. CloneCache is just a wrapper class. I see the point of it. And if you are right why all tests work still?
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neo-project/core I tried to remove the CloneCache before, but it's not an easy task.

StorageItem is a class, and if is not cloned it will be shared between both DataCache, if in the second one is replace calling FromReplica (for example), the content of this instance will be changed in both DataCache, and if the second will be discarded, it will change the main one..

Maybe we don't have this unit tests, but it's how it work.

DataCache a;
CloneCache b=new(a)
var before= a.Get(1);
var change=b.Get(1)
change.FromReplica(xxxx)
// before was changed and also a.Get(1)

Copy link
Member Author

@cschuchardt88 cschuchardt88 Feb 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I fixed the issue with StorageItem in another PR. Because your not at using a IComparer interface, you are just using the object as the key. It ends up throwing in dupes keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discussion Initial issue state - proposed but not yet accepted Waiting for Review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants