Skip to content

Commit

Permalink
Port the Unsafe.SizeOf Fixes from garnet (#900)
Browse files Browse the repository at this point in the history
* Port the Unsafe.SizeOf Fixes from garnet

* make static too
  • Loading branch information
Tornhoof authored Mar 24, 2024
1 parent 657f3f5 commit 1111326
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 21 deletions.
8 changes: 4 additions & 4 deletions cs/src/core/Allocator/BlittableAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ internal unsafe sealed class BlittableAllocator<Key, Value> : AllocatorBase<Key,
private readonly long* nativePointers;

// Record sizes
private static readonly int recordSize = Utility.GetSize(default(Record<Key, Value>));
private static readonly int recordInfoSize = Utility.GetSize(default(RecordInfo));
private static readonly int keySize = Utility.GetSize(default(Key));
private static readonly int valueSize = Utility.GetSize(default(Value));
private static readonly int recordSize = Unsafe.SizeOf<Record<Key, Value>>();
private static readonly int recordInfoSize = Unsafe.SizeOf<RecordInfo>();
private static readonly int keySize = Unsafe.SizeOf<Key>();
private static readonly int valueSize = Unsafe.SizeOf<Value>();

private readonly OverflowPool<PageUnit> overflowPagePool;

Expand Down
2 changes: 1 addition & 1 deletion cs/src/core/Allocator/GenericAllocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal unsafe sealed class GenericAllocator<Key, Value> : AllocatorBase<Key, V
// Tail offsets per segment, in object log
public readonly long[] segmentOffsets;
// Record sizes
private static readonly int recordSize = Utility.GetSize(default(Record<Key, Value>));
private static readonly int recordSize = Unsafe.SizeOf<Record<Key, Value>>();
private readonly SerializerSettings<Key, Value> SerializerSettings;
private readonly bool keyBlittable = Utility.IsBlittable<Key>();
private readonly bool valueBlittable = Utility.IsBlittable<Value>();
Expand Down
3 changes: 2 additions & 1 deletion cs/src/core/Allocator/GenericFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license.

using System;
using System.Runtime.CompilerServices;

namespace FASTER.core
{
Expand All @@ -12,7 +13,7 @@ internal sealed class GenericFrame<Key, Value> : IDisposable
{
private readonly Record<Key, Value>[][] frame;
public readonly int frameSize, pageSize;
private readonly int recordSize = Utility.GetSize(default(Record<Key, Value>));
private static readonly int recordSize = Unsafe.SizeOf<Record<Key, Value>>();

public GenericFrame(int frameSize, int pageSize)
{
Expand Down
12 changes: 0 additions & 12 deletions cs/src/core/Utilities/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,6 @@ public readonly struct Empty
/// </summary>
public static class Utility
{
/// <summary>
/// Get size of type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
internal static unsafe int GetSize<T>(this T value)
{
T[] arr = new T[2];
return (int)((long)Unsafe.AsPointer(ref arr[1]) - (long)Unsafe.AsPointer(ref arr[0]));
}

internal static bool IsBlittableType(Type t)
{
var mi = typeof(Utility).GetMethod("IsBlittable", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod);
Expand Down
2 changes: 1 addition & 1 deletion cs/src/core/VarLen/FixedLengthStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace FASTER.core
/// <typeparam name="T"></typeparam>
internal readonly struct FixedLengthStruct<T> : IVariableLengthStruct<T>
{
private static readonly int size = Utility.GetSize(default(T));
private static readonly int size = Unsafe.SizeOf<T>();

/// <summary>
/// Get average length
Expand Down
4 changes: 2 additions & 2 deletions cs/stress/TestLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using FASTER.core;
using NUnit.Framework;
using System.Diagnostics;
using System.Transactions;
using System.Runtime.CompilerServices;

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

Expand Down Expand Up @@ -133,7 +133,7 @@ static bool verifyOption(bool isValid, string message)
internal string MissingValueTypeHandler => $"Missing DataType handler for value type {this.Options.ValueType}";

// Averages for initial record size estimation
internal int AverageStringLength => Utility.GetSize(default(string));
internal int AverageStringLength => Unsafe.SizeOf<string>();
internal int AverageSpanByteLength => sizeof(int) + (this.UseRandom ? this.Options.ValueLength / 2 : this.Options.ValueLength);

// Actual value lengths on a per-operation basis
Expand Down

0 comments on commit 1111326

Please sign in to comment.