Skip to content

Commit

Permalink
optimization: use Lock object
Browse files Browse the repository at this point in the history
  • Loading branch information
nan01ab committed Jan 15, 2025
1 parent ae6a0f2 commit 5e9d5bf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
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-2024 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>();
5 changes: 4 additions & 1 deletion src/Plugins/LevelDBStore/Plugins/Storage/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ internal class Snapshot : ISnapshot, IEnumerable<KeyValuePair<byte[], byte[]>>
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)
{
_db = db;
Expand Down Expand Up @@ -96,6 +98,7 @@ public IEnumerator<KeyValuePair<byte[], byte[]>> GetEnumerator()
yield return new KeyValuePair<byte[], byte[]>(iterator.Key(), iterator.Value());
}

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
}
}

0 comments on commit 5e9d5bf

Please sign in to comment.