-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve ZLoggerInterpolatedStringHandler(use MagicalBox and etc)
- Loading branch information
Showing
8 changed files
with
479 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using System.Text.Json; | ||
using Utf8StringInterpolation; | ||
|
||
namespace ZLogger.Internal; | ||
|
||
internal unsafe struct MagicalBox | ||
{ | ||
byte[] storage; | ||
int written; | ||
|
||
public MagicalBox(byte[] storage) | ||
{ | ||
this.storage = storage; | ||
} | ||
|
||
public int Written => written; | ||
|
||
public ReadOnlySpan<byte> AsSpan() => storage.AsSpan(0, written); | ||
|
||
public bool TryWrite<T>(T value, out int offset) | ||
{ | ||
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>()) | ||
{ | ||
offset = 0; | ||
return false; | ||
} | ||
|
||
var size = Unsafe.SizeOf<T>(); | ||
if (storage.Length < written + size) | ||
{ | ||
offset = 0; | ||
return false; | ||
} | ||
|
||
Unsafe.WriteUnaligned(ref storage[written], value); | ||
offset = written; | ||
written += Unsafe.SizeOf<T>(); | ||
return true; | ||
} | ||
|
||
public bool TryRead<T>(int offset, out T value) | ||
{ | ||
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>() | ||
|| offset < 0 | ||
|| (storage.Length < offset + Unsafe.SizeOf<T>())) | ||
{ | ||
value = default!; | ||
return false; | ||
} | ||
|
||
value = Unsafe.ReadUnaligned<T>(ref storage[offset]); | ||
return true; | ||
} | ||
|
||
public T Read<T>(int offset) | ||
{ | ||
if (!TryRead<T>(offset, out var value)) | ||
{ | ||
ThrowArgumentOutOfRangeException(); | ||
} | ||
return value; | ||
} | ||
|
||
public object? Read(Type type, int offset) | ||
{ | ||
// TODO: return boxed. | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public bool TryReadTo(Type type, int offset, int alignment, string? format, ref Utf8StringWriter<IBufferWriter<byte>> handler) | ||
{ | ||
if (offset < 0) return false; | ||
// if (storage.Length < offset + Unsafe.SizeOf<T>()) | ||
|
||
|
||
//if (RuntimeHelpers.IsReferenceOrContainsReferences<T>()) | ||
//{ | ||
// return false; | ||
//} | ||
|
||
// TODO: many types...? | ||
if (type == typeof(int)) | ||
{ | ||
handler.AppendFormatted(Read<int>(offset), alignment, format); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
public bool TryReadTo(Type type, int offset, int alignment, string? format, ref DefaultInterpolatedStringHandler handler) | ||
{ | ||
if (offset < 0) return false; | ||
|
||
//if (RuntimeHelpers.IsReferenceOrContainsReferences<T>()) | ||
//{ | ||
// return false; | ||
//} | ||
|
||
if (type == typeof(int)) | ||
{ | ||
handler.AppendFormatted(Read<int>(offset), alignment, format); | ||
} | ||
return true; | ||
} | ||
|
||
public bool TryReadTo(Type type, int offset, string propertyName, Utf8JsonWriter jsonWriter) | ||
{ | ||
if (offset < 0) return false; | ||
|
||
|
||
|
||
|
||
if (type == typeof(int)) | ||
{ | ||
//jsonWriter.WriteNumberValue( | ||
|
||
//handler.AppendFormatted(Read<int>(offset), alignment, format); | ||
} | ||
return true; | ||
} | ||
|
||
static void ThrowArgumentOutOfRangeException() | ||
{ | ||
throw new ArgumentOutOfRangeException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.