Skip to content

Commit 5805814

Browse files
committed
Going back to ref, in caused performance regressions
1 parent 4dd58cf commit 5805814

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

src/LightningDB/DatabaseConfiguration.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,28 @@ internal IDisposable ConfigureDatabase(LightningTransaction tx, LightningDatabas
3737
var pinnedComparer = new ComparerKeepAlive();
3838
if (_comparer != null)
3939
{
40-
CompareFunction compare = (in MDBValue left, in MDBValue right) => _comparer.Compare(left, right);
40+
CompareFunction compare = Compare;
4141
pinnedComparer.AddComparer(compare);
4242
mdb_set_compare(tx._handle, db._handle, compare);
4343
}
4444

4545
if (_duplicatesComparer == null) return pinnedComparer;
46-
CompareFunction dupCompare = (in MDBValue left, in MDBValue right) => _duplicatesComparer.Compare(left, right);
46+
CompareFunction dupCompare = IsDuplicate;
4747
pinnedComparer.AddComparer(dupCompare);
4848
mdb_set_dupsort(tx._handle, db._handle, dupCompare);
4949
return pinnedComparer;
5050
}
5151

52+
private int Compare(ref MDBValue left, ref MDBValue right)
53+
{
54+
return _comparer.Compare(left, right);
55+
}
56+
57+
private int IsDuplicate(ref MDBValue left, ref MDBValue right)
58+
{
59+
return _duplicatesComparer.Compare(left, right);
60+
}
61+
5262
/// <summary>
5363
/// Sets a custom comparer for database operations using the specified comparer.
5464
/// </summary>

src/LightningDB/LightningTransaction.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public unsafe (MDBResultCode resultCode, MDBValue key, MDBValue value) Get(Light
135135
{
136136
var mdbKey = new MDBValue(key.Length, keyBuffer);
137137

138-
return (mdb_get(_handle, db._handle, in mdbKey, out var mdbValue), mdbKey, mdbValue);
138+
return (mdb_get(_handle, db._handle, ref mdbKey, out var mdbValue), mdbKey, mdbValue);
139139
}
140140
}
141141

@@ -406,7 +406,7 @@ public unsafe int CompareKeys(LightningDatabase db, ReadOnlySpan<byte> a, ReadOn
406406
var mdbA = new MDBValue(a.Length, aPtr);
407407
var mdbB = new MDBValue(b.Length, bPtr);
408408

409-
return mdb_cmp(_handle, db._handle, in mdbA, in mdbB);
409+
return mdb_cmp(_handle, db._handle, ref mdbA, ref mdbB);
410410
}
411411
}
412412

@@ -428,7 +428,7 @@ public unsafe int CompareData(LightningDatabase db, ReadOnlySpan<byte> a, ReadOn
428428
var mdbA = new MDBValue(a.Length, aPtr);
429429
var mdbB = new MDBValue(b.Length, bPtr);
430430

431-
return mdb_dcmp(_handle, db._handle, in mdbA, in mdbB);
431+
return mdb_dcmp(_handle, db._handle, ref mdbA, ref mdbB);
432432
}
433433
}
434434

src/LightningDB/Native/CompareFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
namespace LightningDB.Native;
44

55
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
6-
public delegate int CompareFunction(in MDBValue left, in MDBValue right);
6+
public delegate int CompareFunction(ref MDBValue left, ref MDBValue right);

src/LightningDB/Native/Lmdb.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public static partial class Lmdb
378378
/// <returns>A result code indicating success or failure</returns>
379379
[LibraryImport(MDB_DLL_NAME)]
380380
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
381-
public static partial MDBResultCode mdb_get(nint txn, uint dbi, in MDBValue key, out MDBValue data);
381+
public static partial MDBResultCode mdb_get(nint txn, uint dbi, ref MDBValue key, out MDBValue data);
382382

383383
/// <summary>
384384
/// Returns count of duplicates for the current key in a cursor.
@@ -401,7 +401,7 @@ public static partial class Lmdb
401401
/// <returns>A result code indicating success or failure</returns>
402402
[LibraryImport(MDB_DLL_NAME)]
403403
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
404-
public static partial MDBResultCode mdb_put(nint txn, uint dbi, in MDBValue key, in MDBValue data, PutOptions flags);
404+
public static partial MDBResultCode mdb_put(nint txn, uint dbi, ref MDBValue key, ref MDBValue data, PutOptions flags);
405405

406406
/// <summary>
407407
/// Deletes items from a database.
@@ -413,7 +413,7 @@ public static partial class Lmdb
413413
/// <returns>A result code indicating success or failure</returns>
414414
[LibraryImport(MDB_DLL_NAME)]
415415
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
416-
public static partial MDBResultCode mdb_del(nint txn, uint dbi, in MDBValue key, in MDBValue data);
416+
public static partial MDBResultCode mdb_del(nint txn, uint dbi, ref MDBValue key, ref MDBValue data);
417417

418418
/// <summary>
419419
/// Deletes items from a database.
@@ -425,7 +425,7 @@ public static partial class Lmdb
425425
/// <returns>A result code indicating success or failure</returns>
426426
[LibraryImport(MDB_DLL_NAME)]
427427
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
428-
public static partial MDBResultCode mdb_del(nint txn, uint dbi, in MDBValue key, nint data);
428+
public static partial MDBResultCode mdb_del(nint txn, uint dbi, ref MDBValue key, nint data);
429429

430430
/// <summary>
431431
/// Creates a cursor handle for the specified transaction and database.
@@ -496,7 +496,7 @@ public static partial class Lmdb
496496
/// <returns>A result code indicating success or failure</returns>
497497
[LibraryImport(MDB_DLL_NAME)]
498498
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
499-
public static partial MDBResultCode mdb_cursor_put(nint cursor, in MDBValue key, in MDBValue mdbValue, CursorPutOptions flags);
499+
public static partial MDBResultCode mdb_cursor_put(nint cursor, ref MDBValue key, ref MDBValue mdbValue, CursorPutOptions flags);
500500

501501
/// <summary>
502502
/// Deletes the current key/data pair to which the cursor refers.
@@ -539,7 +539,7 @@ public static partial class Lmdb
539539
/// <param name="b">The second item to compare</param>
540540
[LibraryImport(MDB_DLL_NAME)]
541541
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
542-
public static partial int mdb_cmp(nint txn, uint dbi, in MDBValue a, in MDBValue b);
542+
public static partial int mdb_cmp(nint txn, uint dbi, ref MDBValue a, ref MDBValue b);
543543

544544
/// <summary>
545545
/// Compares two data items according to a database's data comparison function.
@@ -550,7 +550,7 @@ public static partial class Lmdb
550550
/// <param name="b">The second item to compare</param>
551551
[LibraryImport(MDB_DLL_NAME)]
552552
[UnmanagedCallConv(CallConvs = [typeof(CallConvCdecl)])]
553-
public static partial int mdb_dcmp(nint txn, uint dbi, in MDBValue a, in MDBValue b);
553+
public static partial int mdb_dcmp(nint txn, uint dbi, ref MDBValue a, ref MDBValue b);
554554

555555
/// <summary>
556556
/// Lists all the readers in the environment and the transaction they're holding.
@@ -601,7 +601,7 @@ public static MDBResultCode mdb_env_set_mapsize(nint env, long size)
601601
/// <returns>A result code indicating success or failure</returns>
602602
public static MDBResultCode mdb_put(nint txn, uint dbi, MDBValue key, MDBValue value, PutOptions flags)
603603
{
604-
return mdb_put(txn, dbi, in key, in value, flags);
604+
return mdb_put(txn, dbi, ref key, ref value, flags);
605605
}
606606

607607
/// <summary>
@@ -614,7 +614,7 @@ public static MDBResultCode mdb_put(nint txn, uint dbi, MDBValue key, MDBValue v
614614
/// <returns>A result code indicating success or failure</returns>
615615
public static MDBResultCode mdb_del(nint txn, uint dbi, MDBValue key, MDBValue value)
616616
{
617-
return mdb_del(txn, dbi, in key, in value);
617+
return mdb_del(txn, dbi, ref key, ref value);
618618
}
619619

620620
/// <summary>
@@ -626,7 +626,7 @@ public static MDBResultCode mdb_del(nint txn, uint dbi, MDBValue key, MDBValue v
626626
/// <returns>A result code indicating success or failure</returns>
627627
public static MDBResultCode mdb_del(nint txn, uint dbi, MDBValue key)
628628
{
629-
return mdb_del(txn, dbi, in key, 0);
629+
return mdb_del(txn, dbi, ref key, 0);
630630
}
631631

632632
/// <summary>
@@ -639,7 +639,7 @@ public static MDBResultCode mdb_del(nint txn, uint dbi, MDBValue key)
639639
/// <returns>A result code indicating success or failure</returns>
640640
public static MDBResultCode mdb_cursor_put(nint cursor, MDBValue key, MDBValue value, CursorPutOptions flags)
641641
{
642-
return mdb_cursor_put(cursor, in key, in value, flags);
642+
return mdb_cursor_put(cursor, ref key, ref value, flags);
643643
}
644644

645645
/// <summary>
@@ -655,7 +655,7 @@ public static MDBResultCode mdb_cursor_put(nint cursor, ref MDBValue key, ref Sp
655655
CursorPutOptions flags)
656656
{
657657
ref var dataRef = ref MemoryMarshal.GetReference(data);
658-
return mdb_cursor_put(cursor, in key, in dataRef, flags);
658+
return mdb_cursor_put(cursor, ref key, ref dataRef, flags);
659659
}
660660

661661
#if !NET7_0_OR_GREATER
@@ -1031,7 +1031,7 @@ public static MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopy
10311031
/// <param name="data">Address where the retrieved data will be stored</param>
10321032
/// <returns>A result code indicating success or failure</returns>
10331033
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
1034-
public static extern MDBResultCode mdb_get(nint txn, uint dbi, in MDBValue key, out MDBValue data);
1034+
public static extern MDBResultCode mdb_get(nint txn, uint dbi, ref MDBValue key, out MDBValue data);
10351035

10361036
/// <summary>
10371037
/// Returns count of duplicates for the current key in a cursor.
@@ -1052,7 +1052,7 @@ public static MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopy
10521052
/// <param name="flags">Special options for this operation</param>
10531053
/// <returns>A result code indicating success or failure</returns>
10541054
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
1055-
public static extern MDBResultCode mdb_put(nint txn, uint dbi, in MDBValue key, in MDBValue data, PutOptions flags);
1055+
public static extern MDBResultCode mdb_put(nint txn, uint dbi, ref MDBValue key, ref MDBValue data, PutOptions flags);
10561056

10571057
/// <summary>
10581058
/// Deletes items from a database.
@@ -1063,7 +1063,7 @@ public static MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopy
10631063
/// <param name="data">The data to delete (only needed for MDB_DUPSORT)</param>
10641064
/// <returns>A result code indicating success or failure</returns>
10651065
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
1066-
public static extern MDBResultCode mdb_del(nint txn, uint dbi, in MDBValue key, in MDBValue data);
1066+
public static extern MDBResultCode mdb_del(nint txn, uint dbi, ref MDBValue key, ref MDBValue data);
10671067

10681068
/// <summary>
10691069
/// Deletes items from a database.
@@ -1074,7 +1074,7 @@ public static MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopy
10741074
/// <param name="data">NULL pointer to delete all of the data items for the key</param>
10751075
/// <returns>A result code indicating success or failure</returns>
10761076
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
1077-
public static extern MDBResultCode mdb_del(nint txn, uint dbi, in MDBValue key, nint data);
1077+
public static extern MDBResultCode mdb_del(nint txn, uint dbi, ref MDBValue key, nint data);
10781078

10791079
/// <summary>
10801080
/// Creates a cursor handle for the specified transaction and database.
@@ -1122,7 +1122,7 @@ public static MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopy
11221122
/// <param name="flags">Special options for this operation</param>
11231123
/// <returns>A result code indicating success or failure</returns>
11241124
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
1125-
public static extern MDBResultCode mdb_cursor_put(nint cursor, in MDBValue key, in MDBValue mdbValue, CursorPutOptions flags);
1125+
public static extern MDBResultCode mdb_cursor_put(nint cursor, ref MDBValue key, ref MDBValue mdbValue, CursorPutOptions flags);
11261126

11271127
/// <summary>
11281128
/// Deletes the current key/data pair to which the cursor refers.
@@ -1161,7 +1161,7 @@ public static MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopy
11611161
/// <param name="a">The first item to compare</param>
11621162
/// <param name="b">The second item to compare</param>
11631163
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
1164-
public static extern int mdb_cmp(nint txn, uint dbi, in MDBValue a, in MDBValue b);
1164+
public static extern int mdb_cmp(nint txn, uint dbi, ref MDBValue a, ref MDBValue b);
11651165

11661166
/// <summary>
11671167
/// Compares two data items according to a database's data comparison function.
@@ -1171,7 +1171,7 @@ public static MDBResultCode mdb_env_copy2(nint env, string path, EnvironmentCopy
11711171
/// <param name="a">The first item to compare</param>
11721172
/// <param name="b">The second item to compare</param>
11731173
[DllImport(MDB_DLL_NAME, CallingConvention = CallingConvention.Cdecl)]
1174-
public static extern int mdb_dcmp(nint txn, uint dbi, in MDBValue a, in MDBValue b);
1174+
public static extern int mdb_dcmp(nint txn, uint dbi, ref MDBValue a, ref MDBValue b);
11751175

11761176
/// <summary>
11771177
/// Lists all the readers in the environment and the transaction they're holding.

0 commit comments

Comments
 (0)