Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: write utf8 bytes directly into destination. #128

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions AerospikeClient/Command/ByteUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright 2012-2023 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
Expand Down Expand Up @@ -96,7 +96,7 @@ public static object BytesToParticle(ParticleType type, byte[] buf, int offset,
}

/// <summary>
/// Estimate size of Utf8 encoded bytes without performing the actual encoding.
/// Estimate size of Utf8 encoded bytes without performing the actual encoding.
/// </summary>
public static int EstimateSizeUtf8(string s)
{
Expand Down Expand Up @@ -130,10 +130,9 @@ public static int StringToUtf8(string s, byte[] buf, int offset)
{
return 0;
}

// The system library encoding is optimized, so there is no need to write a custom implementation.
byte[] data = Encoding.UTF8.GetBytes(s);
Array.Copy(data, 0, buf, offset, data.Length);
return data.Length;
return Encoding.UTF8.GetBytes(s, 0, s.Length, buf, offset);
}

/// <summary>
Expand Down Expand Up @@ -250,7 +249,7 @@ public static object BytesToNumber(byte[] buf, int offset, int len)
// Handle other lengths just in case server changes.
if (len < 8)
{
// Handle variable length long.
// Handle variable length long.
long val = 0;

for (int i = 0; i < len; i++)
Expand Down Expand Up @@ -356,7 +355,7 @@ public static float BytesToFloat(byte[] buf, int offset)
public static int LongToBytes(ulong v, byte[] buf, int offset)
{
// Benchmarks show that custom conversion is faster than System.BitConverter.GetBytes().
// Assume little endian machine and reverse/convert in one pass.
// Assume little endian machine and reverse/convert in one pass.
buf[offset++] = (byte)(v >> 56);
buf[offset++] = (byte)(v >> 48);
buf[offset++] = (byte)(v >> 40);
Expand All @@ -375,7 +374,7 @@ public static int LongToBytes(ulong v, byte[] buf, int offset)
public static int LongToLittleBytes(ulong v, byte[] buf, int offset)
{
// Benchmarks show that custom conversion is faster than System.BitConverter.GetBytes().
// Assume little endian machine.
// Assume little endian machine.
buf[offset++] = (byte)(v >> 0);
buf[offset++] = (byte)(v >> 8);
buf[offset++] = (byte)(v >> 16);
Expand All @@ -393,7 +392,7 @@ public static int LongToLittleBytes(ulong v, byte[] buf, int offset)
public static long BytesToLong(byte[] buf, int offset)
{
// Benchmarks show that custom conversion is slightly faster than System.BitConverter.ToInt64().
// Assume little endian machine and reverse/convert in one pass.
// Assume little endian machine and reverse/convert in one pass.
return (long)(
((ulong)(buf[offset]) << 56) |
((ulong)(buf[offset + 1]) << 48) |
Expand Down Expand Up @@ -436,7 +435,7 @@ public static long LittleBytesToLong(byte[] buf, int offset)
public static int IntToBytes(uint v, byte[] buf, int offset)
{
// Benchmarks show that custom conversion is faster than System.BitConverter.GetBytes().
// Assume little endian machine and reverse/convert in one pass.
// Assume little endian machine and reverse/convert in one pass.
buf[offset++] = (byte)(v >> 24);
buf[offset++] = (byte)(v >> 16);
buf[offset++] = (byte)(v >> 8);
Expand All @@ -463,7 +462,7 @@ public static int IntToLittleBytes(uint v, byte[] buf, int offset)
public static int BytesToInt(byte[] buf, int offset)
{
// Benchmarks show that custom conversion is faster than System.BitConverter.ToInt32().
// Assume little endian machine and reverse/convert in one pass.
// Assume little endian machine and reverse/convert in one pass.
return (((buf[offset]) << 24) |
((buf[offset + 1]) << 16) |
((buf[offset + 2]) << 8) |
Expand Down Expand Up @@ -507,7 +506,7 @@ public static uint BytesToUInt(byte[] buf, int offset)
public static int ShortToBytes(ushort v, byte[] buf, int offset)
{
// Benchmarks show that custom conversion is faster than System.BitConverter.GetBytes().
// Assume little endian machine and reverse/convert in one pass.
// Assume little endian machine and reverse/convert in one pass.
buf[offset++] = (byte)(v >> 8);
buf[offset] = (byte)(v >> 0);
return 2;
Expand All @@ -520,7 +519,7 @@ public static int ShortToBytes(ushort v, byte[] buf, int offset)
public static int ShortToLittleBytes(ushort v, byte[] buf, int offset)
{
// Benchmarks show that custom conversion is faster than System.BitConverter.GetBytes().
// Assume little endian machine and reverse/convert in one pass.
// Assume little endian machine and reverse/convert in one pass.
buf[offset++] = (byte)(v >> 0);
buf[offset] = (byte)(v >> 8);
return 2;
Expand All @@ -532,7 +531,7 @@ public static int ShortToLittleBytes(ushort v, byte[] buf, int offset)
public static int BytesToShort(byte[] buf, int offset)
{
// Benchmarks show that custom conversion is faster than System.BitConverter.ToInt16().
// Assume little endian machine and reverse/convert in one pass.
// Assume little endian machine and reverse/convert in one pass.
return (
((buf[offset]) << 8) |
((buf[offset + 1]) << 0)
Expand All @@ -545,7 +544,7 @@ public static int BytesToShort(byte[] buf, int offset)
public static int LittleBytesToShort(byte[] buf, int offset)
{
// Benchmarks show that custom conversion is faster than System.BitConverter.ToInt16().
// Assume little endian machine and reverse/convert in one pass.
// Assume little endian machine and reverse/convert in one pass.
return (
((buf[offset]) << 0) |
((buf[offset + 1]) << 8)
Expand All @@ -559,7 +558,7 @@ public static int LittleBytesToShort(byte[] buf, int offset)
/// <summary>
/// Encode an integer in variable 7-bit format.
/// The high bit indicates if more bytes are used.
/// Return byte size of integer.
/// Return byte size of integer.
/// </summary>
public static int IntToVarBytes(uint v, byte[] buf, int offset)
{
Expand Down Expand Up @@ -633,4 +632,4 @@ public static void Decompress(byte[] srcBuf, int srcOffset, int srcSize, byte[]
}
}
}
}
}
Loading