From 58cb0f7216941823cffbcc50f3a6adf30af88a70 Mon Sep 17 00:00:00 2001 From: Martin Hans Date: Fri, 23 Aug 2024 18:32:34 +0200 Subject: [PATCH] Always return the count of actually copied items. Further, reduce duplicated code by introduce `Decode(...) where T : struct`. --- src/Parquet/Encodings/ParquetPlainEncoder.cs | 36 +++----------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/src/Parquet/Encodings/ParquetPlainEncoder.cs b/src/Parquet/Encodings/ParquetPlainEncoder.cs index 953e5f62..85fef7f5 100644 --- a/src/Parquet/Encodings/ParquetPlainEncoder.cs +++ b/src/Parquet/Encodings/ParquetPlainEncoder.cs @@ -1,8 +1,10 @@ using System; using System.Buffers; +using System.Drawing; using System.IO; using System.Linq; using System.Numerics; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using Parquet.Data; using Parquet.Extensions; @@ -593,54 +595,30 @@ public static void Encode(ReadOnlySpan data, Stream destination) { Write(destination, bytes); } - public static int Decode(Span source, Span data) { - Span dataBytes = MemoryMarshal.AsBytes(data); - source.CopyWithLimitTo(dataBytes); - return data.Length; - } - public static void Encode(ReadOnlySpan data, Stream destination) { ReadOnlySpan bytes = MemoryMarshal.AsBytes(data); Write(destination, bytes); } - public static int Decode(Span source, Span data) { - Span bytes = MemoryMarshal.AsBytes(data); - source.CopyWithLimitTo(bytes); - return data.Length; - } - public static void Encode(ReadOnlySpan data, Stream destination) { ReadOnlySpan bytes = MemoryMarshal.AsBytes(data); Write(destination, bytes); } - public static int Decode(Span source, Span data) { - Span bytes = MemoryMarshal.AsBytes(data); - source.CopyWithLimitTo(bytes); - return data.Length; - } - public static void Encode(ReadOnlySpan data, Stream destination) { ReadOnlySpan bytes = MemoryMarshal.AsBytes(data); Write(destination, bytes); } - public static int Decode(Span source, Span data) { - Span bytes = MemoryMarshal.AsBytes(data); - source.CopyWithLimitTo(bytes); - return data.Length; - } - public static void Encode(ReadOnlySpan data, Stream destination) { ReadOnlySpan bytes = MemoryMarshal.AsBytes(data); Write(destination, bytes); } - public static int Decode(Span source, Span data) { + public static int Decode(Span source, Span data) where T : struct { Span bytes = MemoryMarshal.AsBytes(data); source.CopyWithLimitTo(bytes); - return data.Length; + return Math.Min(data.Length, source.Length / Unsafe.SizeOf()); } public static void Encode(ReadOnlySpan data, Stream destination, SchemaElement tse) { @@ -752,12 +730,6 @@ public static void Encode(ReadOnlySpan data, Stream destination) { Write(destination, bytes); } - public static int Decode(Span source, Span data) { - Span bytes = MemoryMarshal.AsBytes(data); - source.CopyWithLimitTo(bytes); - return data.Length; - } - public static void Encode(ReadOnlySpan data, Stream destination) { ReadOnlySpan bytes = MemoryMarshal.AsBytes(data); Write(destination, bytes);