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

Minor reduction in memory allocations #1326

Merged
merged 4 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion SteamKit2/SteamKit2/Steam/CMClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ void HandleMulti( IPacketMsg packetMsg )
{
using var compressedStream = new MemoryStream( payload );
using var gzipStream = new GZipStream( compressedStream, CompressionMode.Decompress );
using var decompressedStream = new MemoryStream();
using var decompressedStream = new MemoryStream( capacity: ( int )msgMulti.Body.size_unzipped );
gzipStream.CopyTo( decompressedStream );
payload = decompressedStream.ToArray();
}
Expand Down
11 changes: 8 additions & 3 deletions SteamKit2/SteamKit2/Steam/SteamClient/CallbackMgr/CallbackMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,14 @@ void ICallbackMgrInternals.Register( CallbackBase call )

void Handle( ICallbackMsg call )
{
registeredCallbacks
.FindAll( callback => callback.CallbackType.IsAssignableFrom( call.GetType() ) ) // find handlers interested in this callback
.ForEach( callback => callback.Run( call ) ); // run them
// find handlers interested in this callback
foreach ( var callback in registeredCallbacks )
{
if ( callback.CallbackType.IsAssignableFrom( call.GetType() ) )
{
callback.Run( call );
}
}
}

void ICallbackMgrInternals.Unregister( CallbackBase call )
Expand Down
43 changes: 43 additions & 0 deletions SteamKit2/SteamKit2/Util/StreamHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
Expand Down Expand Up @@ -74,6 +75,11 @@ public static float ReadFloat( this Stream stream )

public static string ReadNullTermString( this Stream stream, Encoding encoding )
{
if ( encoding == Encoding.UTF8 )
{
return ReadNullTermUtf8String( stream );
}

int characterSize = encoding.GetByteCount( "e" );

using MemoryStream ms = new MemoryStream();
Expand All @@ -94,6 +100,43 @@ public static string ReadNullTermString( this Stream stream, Encoding encoding )
return encoding.GetString( ms.GetBuffer(), 0, ( int )ms.Length );
}

private static string ReadNullTermUtf8String( Stream stream )
{
var buffer = ArrayPool<byte>.Shared.Rent( 32 );

try
{
var position = 0;

do
{
var b = stream.ReadByte();

if ( b <= 0 ) // null byte or stream ended
{
break;
}

if ( position >= buffer.Length )
{
var newBuffer = ArrayPool<byte>.Shared.Rent( buffer.Length * 2 );
Buffer.BlockCopy( buffer, 0, newBuffer, 0, buffer.Length );
ArrayPool<byte>.Shared.Return( buffer );
buffer = newBuffer;
}

buffer[ position++ ] = (byte)b;
}
while ( true );

return Encoding.UTF8.GetString( buffer[ ..position ] );
}
finally
{
ArrayPool<byte>.Shared.Return( buffer );
}
}

public static void WriteNullTermString( this Stream stream, string value, Encoding encoding )
{
var dataLength = encoding.GetByteCount( value );
Expand Down
Loading