Skip to content

Commit

Permalink
使用.net5中的新方法减少分配,优化性能 (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
colprog authored May 28, 2024
1 parent bc2a112 commit d1a7701
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
2 changes: 1 addition & 1 deletion csharp/rocketmq-client-csharp/MessageView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public static MessageView FromProtobuf(Proto.Message message, MessageQueue messa
{
case Proto.Encoding.Gzip:
{
body = Utilities.DecompressBytesGzip(message.Body.ToByteArray());
body = Utilities.DecompressBytesGzip(raw);
break;
}
case Proto.Encoding.Identity:
Expand Down
37 changes: 17 additions & 20 deletions csharp/rocketmq-client-csharp/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public static class Utilities
private static readonly string HostName = System.Net.Dns.GetHostName();
private static readonly byte[] RandomMacAddressBytes =
Enumerable.Range(0, 6).Select(_ => (byte)new Random().Next(256)).ToArray();
private static readonly ThreadLocal<MD5> Md5 = new ThreadLocal<MD5>(MD5.Create);
private static readonly ThreadLocal<SHA1> Sha1 = new ThreadLocal<SHA1>(SHA1.Create);

public const int MasterBrokerId = 0;

Expand Down Expand Up @@ -85,16 +83,24 @@ public static string GetClientId()
#if NET5_0_OR_GREATER
public static string ComputeMd5Hash(byte[] data)
{
var hashBytes = Md5.Value.ComputeHash(data);
var hashBytes = MD5.HashData(data);
return Convert.ToHexString(hashBytes);
}

public static string ComputeSha1Hash(byte[] data)
{
var hashBytes = Sha1.Value.ComputeHash(data);
var hashBytes = SHA1.HashData(data);
return Convert.ToHexString(hashBytes);
}

public static string ByteArrayToHexString(byte[] bytes)
{
return Convert.ToHexString(bytes);
}
#else
private static readonly ThreadLocal<MD5> Md5 = new ThreadLocal<MD5>(MD5.Create);
private static readonly ThreadLocal<SHA1> Sha1 = new ThreadLocal<SHA1>(SHA1.Create);

public static string ComputeMd5Hash(byte[] data)
{
var hashBytes = Md5.Value.ComputeHash(data);
Expand All @@ -106,6 +112,11 @@ public static string ComputeSha1Hash(byte[] data)
var hashBytes = Sha1.Value.ComputeHash(data);
return BitConverter.ToString(hashBytes).Replace("-", "");
}

public static string ByteArrayToHexString(byte[] bytes)
{
return BitConverter.ToString(bytes).Replace("-", "");
}
#endif

private static string DecimalToBase36(long decimalNumber)
Expand All @@ -122,20 +133,6 @@ private static string DecimalToBase36(long decimalNumber)
return result;
}

public static string ByteArrayToHexString(byte[] bytes)
{
var result = new StringBuilder(bytes.Length * 2);
const string hexAlphabet = "0123456789ABCDEF";

foreach (var b in bytes)
{
result.Append(hexAlphabet[(int)(b >> 4)]);
result.Append(hexAlphabet[(int)(b & 0xF)]);
}

return result.ToString();
}

public static byte[] CompressBytesGzip(byte[] src, CompressionLevel level)
{
using (var ms = new MemoryStream())
Expand All @@ -152,8 +149,8 @@ public static byte[] CompressBytesGzip(byte[] src, CompressionLevel level)
public static byte[] DecompressBytesGzip(byte[] src)
{
var inputStream = new MemoryStream(src);
var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress);
var outputStream = new MemoryStream();
using var gzipStream = new GZipStream(inputStream, CompressionMode.Decompress);
using var outputStream = new MemoryStream();
gzipStream.CopyTo(outputStream);
return outputStream.ToArray();
}
Expand Down

0 comments on commit d1a7701

Please sign in to comment.