Skip to content

Commit

Permalink
Merge branch 'master' into HF_Echidna
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim8y authored Jan 21, 2025
2 parents 4237d6d + fe22fb8 commit 70326c2
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/devcontainer.dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# https://github.com/dotnet/dotnet-docker/blob/main/README.sdk.md
# https://mcr.microsoft.com/en-us/artifact/mar/dotnet/sdk/tags <-- this shows all images
FROM mcr.microsoft.com/dotnet/sdk:9.0.101-noble
FROM mcr.microsoft.com/dotnet/sdk:9.0.102-noble

# Install the libleveldb-dev package
RUN apt-get update && apt-get install -y libleveldb-dev
1 change: 1 addition & 0 deletions benchmarks/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<LangVersion>latest</LangVersion>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down
59 changes: 59 additions & 0 deletions benchmarks/Neo.Benchmarks/Persistence/Bechmarks_LevelDB.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (C) 2015-2025 The Neo Project.
//
// Bechmarks_LevelDB.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.Plugins.Storage;
using Neo.SmartContract;
using System.Diagnostics;

namespace Neo.Benchmarks.Persistence.Benchmarks
{
public class Bechmarks_LevelDB
{
// avoid allocations in benchmarks
private static StorageKey key1;
private static readonly byte[] value = new UInt256().GetSpan().ToArray();

private const string PathLevelDB = "Data_LevelDB_Benchmarks";

private static readonly LevelDBStore levelDb = new();
private static ISnapshot snapshot;

[GlobalSetup]
public void Setup()
{
if (Directory.Exists(PathLevelDB))
Directory.Delete(PathLevelDB, true);

key1 = new KeyBuilder(1, 1).Add(new UInt160());

var levelDbStore = levelDb.GetStore(PathLevelDB);
snapshot = levelDbStore.GetSnapshot();
}

[GlobalCleanup]
public void Cleanup()
{
snapshot.Dispose();
levelDb.Dispose();
if (Directory.Exists(PathLevelDB))
Directory.Delete(PathLevelDB, true);
}

[Benchmark]
public void LevelDBSnapshotWrites()
{
snapshot.Put(key1.ToArray(), value);
snapshot.Delete(key1.ToArray());
}
}
}
1 change: 1 addition & 0 deletions benchmarks/Neo.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
BenchmarkRunner.Run<Benchmarks_Hash>();
BenchmarkRunner.Run<Benchmarks_StorageKey>();
BenchmarkRunner.Run<Bechmarks_ReadOnlyStoreView>();
BenchmarkRunner.Run<Bechmarks_LevelDB>();
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "9.0.100",
"version": "9.0.102",
"rollForward": "latestFeature",
"allowPrerelease": false
}
Expand Down
7 changes: 4 additions & 3 deletions src/Neo/Persistence/MemorySnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

namespace Neo.Persistence
{
/// <summary>
/// <remarks>On-chain write operations on a snapshot cannot be concurrent.</remarks>
/// </summary>
internal class MemorySnapshot : ISnapshot
{
private readonly ConcurrentDictionary<byte[], byte[]> _innerData;
Expand Down Expand Up @@ -47,9 +50,7 @@ public void Delete(byte[] key)
_writeBatch[key] = null;
}

public void Dispose()
{
}
public void Dispose() { }

public void Put(byte[] key, byte[] value)
{
Expand Down
21 changes: 15 additions & 6 deletions src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,20 @@ namespace Neo.Plugins.Storage
{
/// <summary>
/// <code>Iterating over the whole dataset can be time-consuming. Depending upon how large the dataset is.</code>
/// <remarks>On-chain write operations on a snapshot cannot be concurrent.</remarks>
/// </summary>
internal class Snapshot : ISnapshot, IEnumerable<KeyValuePair<byte[], byte[]>>
{
private readonly DB _db;
private readonly LSnapshot _snapshot;
private readonly ReadOptions _readOptions;
private readonly WriteBatch _batch;

#if NET9_0_OR_GREATER
private readonly System.Threading.Lock _lock = new();
#else
private readonly object _lock = new();
#endif

public Snapshot(DB db)
{
Expand All @@ -36,18 +42,27 @@ public Snapshot(DB db)
_batch = new WriteBatch();
}

/// <inheritdoc/>
public void Commit()
{
lock (_lock)
_db.Write(WriteOptions.Default, _batch);
}

/// <inheritdoc/>
public void Delete(byte[] key)
{
lock (_lock)
_batch.Delete(key);
}

/// <inheritdoc/>
public void Put(byte[] key, byte[] value)
{
lock (_lock)
_batch.Put(key, value);
}

public void Dispose()
{
_snapshot.Dispose();
Expand All @@ -60,12 +75,6 @@ public void Dispose()
return _db.Seek(_readOptions, keyOrPrefix, direction);
}

public void Put(byte[] key, byte[] value)
{
lock (_lock)
_batch.Put(key, value);
}

public bool Contains(byte[] key)
{
return _db.Contains(_readOptions, key);
Expand Down
3 changes: 3 additions & 0 deletions src/Plugins/RocksDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

namespace Neo.Plugins.Storage
{
/// <summary>
/// <remarks>On-chain write operations on a snapshot cannot be concurrent.</remarks>
/// </summary>
internal class Snapshot : ISnapshot
{
private readonly RocksDb db;
Expand Down
2 changes: 1 addition & 1 deletion tests/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<PropertyGroup>
<LangVersion>12.0</LangVersion>
<LangVersion>latest</LangVersion>
<EnforceCodeStyleInBuild>false</EnforceCodeStyleInBuild>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
Expand Down

0 comments on commit 70326c2

Please sign in to comment.