Skip to content

Commit

Permalink
Remove unnecessary preprocessor directives (#146)
Browse files Browse the repository at this point in the history
* Remove remaining .NET Standard conditional compilation from Tsavorite

* Remove unnecessary preprocessor directives from Garnet

---------

Co-authored-by: Badrish Chandramouli <badrishc@microsoft.com>
Co-authored-by: vazois <96085550+vazois@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 27, 2024
1 parent cc9877b commit 0a9f6b3
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 197 deletions.
32 changes: 4 additions & 28 deletions libs/client/LightEpoch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@ public sealed unsafe class LightEpoch
/// </summary>
Entry[] tableRaw;
Entry* tableAligned;
#if !NET5_0_OR_GREATER
GCHandle tableHandle;
#endif

static readonly Entry[] threadIndex;
static readonly Entry* threadIndexAligned;
#if !NET5_0_OR_GREATER
static GCHandle threadIndexHandle;
#endif

/// <summary>
/// List of action, epoch pairs containing actions to performed
Expand Down Expand Up @@ -96,17 +90,10 @@ public sealed unsafe class LightEpoch
/// </summary>
static LightEpoch()
{
long p;

// Over-allocate to do cache-line alignment
#if NET5_0_OR_GREATER
threadIndex = GC.AllocateArray<Entry>(kTableSize + 2, true);
p = (long)Unsafe.AsPointer(ref threadIndex[0]);
#else
threadIndex = new Entry[kTableSize + 2];
threadIndexHandle = GCHandle.Alloc(threadIndex, GCHandleType.Pinned);
p = (long)threadIndexHandle.AddrOfPinnedObject();
#endif
long p = (long)Unsafe.AsPointer(ref threadIndex[0]);

// Force the pointer to align to 64-byte boundaries
long p2 = (p + (kCacheLineBytes - 1)) & ~(kCacheLineBytes - 1);
threadIndexAligned = (Entry*)p2;
Expand All @@ -117,17 +104,9 @@ static LightEpoch()
/// </summary>
public LightEpoch()
{
long p;

#if NET5_0_OR_GREATER
tableRaw = GC.AllocateArray<Entry>(kTableSize + 2, true);
p = (long)Unsafe.AsPointer(ref tableRaw[0]);
#else
// Over-allocate to do cache-line alignment
tableRaw = new Entry[kTableSize + 2];
tableHandle = GCHandle.Alloc(tableRaw, GCHandleType.Pinned);
p = (long)tableHandle.AddrOfPinnedObject();
#endif
long p = (long)Unsafe.AsPointer(ref tableRaw[0]);

// Force the pointer to align to 64-byte boundaries
long p2 = (p + (kCacheLineBytes - 1)) & ~(kCacheLineBytes - 1);
tableAligned = (Entry*)p2;
Expand All @@ -145,9 +124,6 @@ public LightEpoch()
/// </summary>
public void Dispose()
{
#if !NET5_0_OR_GREATER
tableHandle.Free();
#endif
CurrentEpoch = 1;
SafeToReclaimEpoch = 0;
}
Expand Down
43 changes: 0 additions & 43 deletions libs/cluster/Server/ClusterManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,49 +31,6 @@ public int GetHashCode(Worker key)
}
}

#if NET5_0
static class MyExtensions
{
public static Task<TResult> WaitAsync<TResult>(this Task<TResult> task, int millisecondsTimeout) =>
WaitAsync(task, TimeSpan.FromMilliseconds(millisecondsTimeout), default);

public static Task<TResult> WaitAsync<TResult>(this Task<TResult> task, TimeSpan timeout) =>
WaitAsync(task, timeout, default);

public static Task<TResult> WaitAsync<TResult>(this Task<TResult> task, CancellationToken cancellationToken) =>
WaitAsync(task, Timeout.InfiniteTimeSpan, cancellationToken);

public static async Task<TResult> WaitAsync<TResult>(this Task<TResult> task, TimeSpan timeout, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<TResult>();
using (new Timer(s => ((TaskCompletionSource<TResult>)s).TrySetException(new TimeoutException()), tcs, timeout, Timeout.InfiniteTimeSpan))
using (cancellationToken.Register(s => ((TaskCompletionSource<TResult>)s).TrySetCanceled(), tcs))
{
return await (await Task.WhenAny(task, tcs.Task).ConfigureAwait(false)).ConfigureAwait(false);
}
}

public static Task WaitAsync(this Task task, int millisecondsTimeout) =>
WaitAsync(task, TimeSpan.FromMilliseconds(millisecondsTimeout), default);

public static Task WaitAsync(this Task task, TimeSpan timeout) =>
WaitAsync(task, timeout, default);

public static Task WaitAsync(this Task task, CancellationToken cancellationToken) =>
WaitAsync(task, Timeout.InfiniteTimeSpan, cancellationToken);

public async static Task WaitAsync(this Task task, TimeSpan timeout, CancellationToken cancellationToken)
{
var tcs = new TaskCompletionSource<bool>();
using (new Timer(s => ((TaskCompletionSource<bool>)s).TrySetException(new TimeoutException()), tcs, timeout, Timeout.InfiniteTimeSpan))
using (cancellationToken.Register(s => ((TaskCompletionSource<bool>)s).TrySetCanceled(), tcs))
{
await (await Task.WhenAny(task, tcs.Task).ConfigureAwait(false)).ConfigureAwait(false);
}
}
}
#endif

/// <summary>
/// Cluster manager
/// </summary>
Expand Down
8 changes: 0 additions & 8 deletions libs/common/RespReadUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,7 @@ public static bool ReadStringWithLengthHeader(out string result, ref byte* ptr,
Debug.Assert(*(ptr - 2) == '\r');
Debug.Assert(*(ptr - 1) == '\n');

#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
result = Encoding.UTF8.GetString(new Span<byte>(keyPtr, ksize));
#else
result = Encoding.UTF8.GetString(new Span<byte>(keyPtr, ksize).ToArray());
#endif
return true;
}

Expand Down Expand Up @@ -691,11 +687,7 @@ private static bool ReadString(out string result, ref byte* ptr, byte* end)
{
if (*ptr == (byte)'\r' && *(ptr + 1) == (byte)'\n')
{
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
result = Encoding.UTF8.GetString(new ReadOnlySpan<byte>(start, (int)(ptr - start)));
#else
result = Encoding.UTF8.GetString(new ReadOnlySpan<byte>(start, (int)(ptr - start)).ToArray());
#endif
ptr += 2;
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,7 @@ private void RecoverFiles()
bool gotHandle;
int numBytes = 0;

#if NETSTANDARD2_1 || NET
UnmanagedMemoryManager<byte> umm = default;
#else
SectorAlignedMemory memory = default;
#endif

try
{
Interlocked.Increment(ref numPending);
Expand All @@ -153,26 +148,18 @@ private void RecoverFiles()
if (gotHandle)
{
logReadHandle.Seek((long)sourceAddress, SeekOrigin.Begin);
#if NETSTANDARD2_1 || NET
unsafe
{
umm = new UnmanagedMemoryManager<byte>((byte*)destinationAddress, (int)readLength);
}
readTask = logReadHandle.ReadAsync(umm.Memory).AsTask();
#else
memory = pool.Get((int)readLength);
readTask = logReadHandle.ReadAsync(memory.buffer, 0, (int)readLength);
#endif
}
}
catch
{
Interlocked.Decrement(ref numPending);

// Perform pool returns and disposals
#if !(NETSTANDARD2_1 || NET)
memory?.Return();
#endif
if (logReadHandle != null) streampool?.Return(logReadHandle);

// Issue user callback
Expand All @@ -188,26 +175,17 @@ private void RecoverFiles()
{
logReadHandle = await streampool.GetAsync().ConfigureAwait(false);
logReadHandle.Seek((long)sourceAddress, SeekOrigin.Begin);
#if NETSTANDARD2_1 || NET
unsafe
{
umm = new UnmanagedMemoryManager<byte>((byte*)destinationAddress, (int)readLength);
}
readTask = logReadHandle.ReadAsync(umm.Memory).AsTask();
#else
memory = pool.Get((int)readLength);
readTask = logReadHandle.ReadAsync(memory.buffer, 0, (int)readLength);
#endif
}
catch
{
Interlocked.Decrement(ref numPending);
// Perform pool returns and disposals
#if !(NETSTANDARD2_1 || NET)
memory?.Return();
#endif
if (logReadHandle != null) streampool?.Return(logReadHandle);
// Issue user callback
Expand All @@ -219,14 +197,6 @@ private void RecoverFiles()
try
{
numBytes = await readTask.ConfigureAwait(false);
#if !(NETSTANDARD2_1 || NET)
unsafe
{
fixed (void* source = memory.buffer)
Buffer.MemoryCopy(source, (void*)destinationAddress, numBytes, numBytes);
}
#endif
}
catch (Exception ex)
{
Expand All @@ -240,10 +210,6 @@ private void RecoverFiles()
{
Interlocked.Decrement(ref numPending);
// Perform pool returns and disposals
#if !(NETSTANDARD2_1 || NET)
memory?.Return();
#endif
// Sequentialize all reads from same handle
streampool?.Return(logReadHandle);
Expand Down Expand Up @@ -275,12 +241,7 @@ private void RecoverFiles()
Task writeTask = default;
bool gotHandle;

#if NETSTANDARD2_1 || NET
UnmanagedMemoryManager<byte> umm = default;
#else
SectorAlignedMemory memory = default;
#endif

HandleCapacity(segmentId);

try
Expand All @@ -291,34 +252,19 @@ private void RecoverFiles()
if (gotHandle)
{
logWriteHandle.Seek((long)destinationAddress, SeekOrigin.Begin);
#if NETSTANDARD2_1 || NET
unsafe
{
umm = new UnmanagedMemoryManager<byte>((byte*)sourceAddress, (int)numBytesToWrite);
}

writeTask = logWriteHandle.WriteAsync(umm.Memory).AsTask();
#else
memory = pool.Get((int)numBytesToWrite);
unsafe
{
fixed (void* destination = memory.buffer)
{
Buffer.MemoryCopy((void*)sourceAddress, destination, numBytesToWrite, numBytesToWrite);
}
}
writeTask = logWriteHandle.WriteAsync(memory.buffer, 0, (int)numBytesToWrite);
#endif
}
}
catch
{
Interlocked.Decrement(ref numPending);

// Perform pool returns and disposals
#if !(NETSTANDARD2_1 || NET)
memory?.Return();
#endif
if (logWriteHandle != null) streampool?.Return(logWriteHandle);

// Issue user callback
Expand All @@ -334,33 +280,18 @@ private void RecoverFiles()
{
logWriteHandle = await streampool.GetAsync().ConfigureAwait(false);
logWriteHandle.Seek((long)destinationAddress, SeekOrigin.Begin);
#if NETSTANDARD2_1 || NET
unsafe
{
umm = new UnmanagedMemoryManager<byte>((byte*)sourceAddress, (int)numBytesToWrite);
}
writeTask = logWriteHandle.WriteAsync(umm.Memory).AsTask();
#else
memory = pool.Get((int)numBytesToWrite);
unsafe
{
fixed (void* destination = memory.buffer)
{
Buffer.MemoryCopy((void*)sourceAddress, destination, numBytesToWrite, numBytesToWrite);
}
}
writeTask = logWriteHandle.WriteAsync(memory.buffer, 0, (int)numBytesToWrite);
#endif
}
catch
{
Interlocked.Decrement(ref numPending);
// Perform pool returns and disposals
#if !(NETSTANDARD2_1 || NET)
memory?.Return();
#endif
if (logWriteHandle != null) streampool?.Return(logWriteHandle);
// Issue user callback
Expand All @@ -385,10 +316,6 @@ private void RecoverFiles()
{
Interlocked.Decrement(ref numPending);
// Perform pool returns and disposals
#if !(NETSTANDARD2_1 || NET)
memory?.Return();
#endif
// Sequentialize all writes to same handle
await ((FileStream)logWriteHandle).FlushAsync().ConfigureAwait(false);
streampool?.Return(logWriteHandle);
Expand Down
2 changes: 0 additions & 2 deletions libs/storage/Tsavorite/cs/test/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,7 @@ internal static IDevice CreateTestDevice(DeviceType testDeviceType, string filen
case DeviceType.LSD:
bool useIoCompletionPort = false;
bool disableFileBuffering = true;
#if NETSTANDARD || NET
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) // avoids CA1416 // Validate platform compatibility
#endif
device = new LocalStorageDevice(filename, preallocateFile, deleteOnClose, disableFileBuffering, capacity, recoverDevice, useIoCompletionPort);
break;
#endif
Expand Down

0 comments on commit 0a9f6b3

Please sign in to comment.