mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-11 23:40:57 +00:00
Revert "Performance Increase of Vector of Structures using .NET BlockCopy (#4830)"
This reverts commit 7b50004ec9.
Change-Id: I09d6869c16aa3c7fadc537fc9c76eaa3cf7ee7ea
This commit is contained in:
@@ -30,7 +30,6 @@
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
@@ -92,78 +91,19 @@ namespace FlatBuffers
|
||||
|
||||
public byte[] ToArray(int pos, int len)
|
||||
{
|
||||
return ToArray<byte>(pos, len);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A lookup of type sizes. Used instead of Marshal.SizeOf() which has additional
|
||||
/// overhead, but also is compatible with generic functions for simplified code.
|
||||
/// </summary>
|
||||
private static Dictionary<Type, int> genericSizes = new Dictionary<Type, int>()
|
||||
{
|
||||
{ typeof(bool), sizeof(bool) },
|
||||
{ typeof(float), sizeof(float) },
|
||||
{ typeof(double), sizeof(double) },
|
||||
{ typeof(sbyte), sizeof(sbyte) },
|
||||
{ typeof(byte), sizeof(byte) },
|
||||
{ typeof(short), sizeof(short) },
|
||||
{ typeof(ushort), sizeof(ushort) },
|
||||
{ typeof(int), sizeof(int) },
|
||||
{ typeof(uint), sizeof(uint) },
|
||||
{ typeof(ulong), sizeof(ulong) },
|
||||
{ typeof(long), sizeof(long) },
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Get the wire-size (in bytes) of a type supported by flatbuffers.
|
||||
/// </summary>
|
||||
/// <param name="t">The type to get the wire size of</param>
|
||||
/// <returns></returns>
|
||||
public static int SizeOf<T>()
|
||||
{
|
||||
return genericSizes[typeof(T)];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the Type provided is supported as scalar value
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The Type to check</typeparam>
|
||||
/// <returns>True if the type is a scalar type that is supported, falsed otherwise</returns>
|
||||
public static bool IsSupportedType<T>()
|
||||
{
|
||||
return genericSizes.ContainsKey(typeof(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the wire-size (in bytes) of an typed array
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the array</typeparam>
|
||||
/// <param name="x">The array to get the size of</param>
|
||||
/// <returns>The number of bytes the array takes on wire</returns>
|
||||
public static int ArraySize<T>(T[] x)
|
||||
{
|
||||
return SizeOf<T>() * x.Length;
|
||||
}
|
||||
|
||||
// Get a portion of the buffer casted into an array of type T, given
|
||||
// the buffer position and length.
|
||||
public T[] ToArray<T>(int pos, int len)
|
||||
where T: struct
|
||||
{
|
||||
AssertOffsetAndLength(pos, len);
|
||||
T[] arr = new T[len];
|
||||
Buffer.BlockCopy(_buffer, pos, arr, 0, ArraySize(arr));
|
||||
byte[] arr = new byte[len];
|
||||
Buffer.BlockCopy(_buffer, pos, arr, 0, len);
|
||||
return arr;
|
||||
}
|
||||
|
||||
public byte[] ToSizedArray()
|
||||
{
|
||||
return ToArray<byte>(Position, Length - Position);
|
||||
return ToArray(Position, Length - Position);
|
||||
}
|
||||
|
||||
public byte[] ToFullArray()
|
||||
{
|
||||
return ToArray<byte>(0, Length);
|
||||
return ToArray(0, Length);
|
||||
}
|
||||
|
||||
public ArraySegment<byte> ToArraySegment(int pos, int len)
|
||||
@@ -428,56 +368,6 @@ namespace FlatBuffers
|
||||
WriteLittleEndian(offset, sizeof(double), ulonghelper[0]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies an array of type T into this buffer, ending at the given
|
||||
/// offset into this buffer. The starting offset is calculated based on the length
|
||||
/// of the array and is the value returned.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the input data (must be a struct)</typeparam>
|
||||
/// <param name="offset">The offset into this buffer where the copy will end</param>
|
||||
/// <param name="x">The array to copy data from</param>
|
||||
/// <returns>The 'start' location of this buffer now, after the copy completed</returns>
|
||||
public int Put<T>(int offset, T[] x)
|
||||
where T : struct
|
||||
{
|
||||
if(x == null)
|
||||
{
|
||||
throw new ArgumentNullException("Cannot put a null array");
|
||||
}
|
||||
|
||||
if(x.Length == 0)
|
||||
{
|
||||
throw new ArgumentException("Cannot put an empty array");
|
||||
}
|
||||
|
||||
if(!IsSupportedType<T>())
|
||||
{
|
||||
throw new ArgumentException("Cannot put an array of type "
|
||||
+ typeof(T) + " into this buffer");
|
||||
}
|
||||
|
||||
if (BitConverter.IsLittleEndian)
|
||||
{
|
||||
int numBytes = ByteBuffer.ArraySize(x);
|
||||
offset -= numBytes;
|
||||
AssertOffsetAndLength(offset, numBytes);
|
||||
// if we are LE, just do a block copy
|
||||
Buffer.BlockCopy(x, 0, _buffer, offset, numBytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotImplementedException("Big Endian Support not implemented yet " +
|
||||
"for putting typed arrays");
|
||||
// if we are BE, we have to swap each element by itself
|
||||
//for(int i = x.Length - 1; i >= 0; i--)
|
||||
//{
|
||||
// todo: low priority, but need to genericize the Put<T>() functions
|
||||
//}
|
||||
}
|
||||
return offset;
|
||||
}
|
||||
|
||||
|
||||
#endif // UNSAFE_BYTEBUFFER
|
||||
|
||||
public sbyte GetSbyte(int index)
|
||||
|
||||
@@ -179,18 +179,6 @@ namespace FlatBuffers
|
||||
_bb.PutFloat(_space -= sizeof(float), x);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Puts an array of type T into this builder at the
|
||||
/// current offset
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the input data </typeparam>
|
||||
/// <param name="x">The array to copy data from</param>
|
||||
public void Put<T>(T[] x)
|
||||
where T : struct
|
||||
{
|
||||
_space = _bb.Put(_space, x);
|
||||
}
|
||||
|
||||
public void PutDouble(double x)
|
||||
{
|
||||
_bb.PutDouble(_space -= sizeof(double), x);
|
||||
@@ -257,37 +245,6 @@ namespace FlatBuffers
|
||||
/// <param name="x">The `float` to add to the buffer.</param>
|
||||
public void AddFloat(float x) { Prep(sizeof(float), 0); PutFloat(x); }
|
||||
|
||||
/// <summary>
|
||||
/// Add an array of type T to the buffer (aligns the data and grows if necessary).
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the input data</typeparam>
|
||||
/// <param name="x">The array to copy data from</param>
|
||||
public void Add<T>(T[] x)
|
||||
where T : struct
|
||||
{
|
||||
if (x == null)
|
||||
{
|
||||
throw new ArgumentNullException("Cannot add a null array");
|
||||
}
|
||||
|
||||
if( x.Length == 0)
|
||||
{
|
||||
// don't do anything if the array is empty
|
||||
return;
|
||||
}
|
||||
|
||||
if(!ByteBuffer.IsSupportedType<T>())
|
||||
{
|
||||
throw new ArgumentException("Cannot add this Type array to the builder");
|
||||
}
|
||||
|
||||
int size = ByteBuffer.SizeOf<T>();
|
||||
// Need to prep on size (for data alignment) and then we pass the
|
||||
// rest of the length (minus 1) as additional bytes
|
||||
Prep(size, size * (x.Length - 1));
|
||||
Put(x);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a `double` to the buffer (aligns the data and grows if necessary).
|
||||
/// </summary>
|
||||
|
||||
@@ -94,29 +94,6 @@ namespace FlatBuffers
|
||||
return bb.ToArraySegment(pos, len);
|
||||
}
|
||||
|
||||
// Get the data of a vector whoses offset is stored at "offset" in this object as an
|
||||
// T[]. If the vector is not present in the ByteBuffer, then a null value will be
|
||||
// returned.
|
||||
public T[] __vector_as_array<T>(int offset)
|
||||
where T : struct
|
||||
{
|
||||
if(!BitConverter.IsLittleEndian)
|
||||
{
|
||||
throw new NotSupportedException("Getting typed arrays on a Big Endian " +
|
||||
"system is not support");
|
||||
}
|
||||
|
||||
var o = this.__offset(offset);
|
||||
if (0 == o)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var pos = this.__vector(o);
|
||||
var len = this.__vector_len(o);
|
||||
return bb.ToArray<T>(pos, len);
|
||||
}
|
||||
|
||||
// Initialize any Table-derived type to point to the union at the given offset.
|
||||
public T __union<T>(int offset) where T : struct, IFlatbufferObject
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user