Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.

using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
#if !USE_UNMANAGED
Expand Down Expand Up @@ -93,7 +94,7 @@ public unsafe IntPtr GetData(DatabaseFileProvider fileProvider)
var chunkBytes = MemoryUtilities.Allocate(Size);

var bufferCapacity = Math.Min(8192u, (uint)Size);
var buffer = new byte[bufferCapacity];
var buffer = ArrayPool<byte>.Shared.Rent((int)bufferCapacity);

var count = (uint)Size;
fixed (byte* bufferStart = buffer) // null if array is empty or null
Expand All @@ -109,9 +110,10 @@ public unsafe IntPtr GetData(DatabaseFileProvider fileProvider)
count -= read;
} while (count > 0);
}
ArrayPool<byte>.Shared.Return(buffer);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we want a try/finally to make sure it's released?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case it's fine since it won't be used after return. But there's no reason to use try-finally here.

#else
var bytes = new byte[Size];
stream.Read(bytes, 0, Size);
stream.ReadExactly(bytes, 0, Size);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just use stream.ReadExactly(bytes) when the offset is zero and the length is the exact buffer length.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I could do that. There are hundreds/thousands of mini performance and reliability improvements I could make on this repo just from analyzers alone.


handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
var chunkBytes = handle.AddrOfPinnedObject();
Expand Down
7 changes: 5 additions & 2 deletions sources/tools/Stride.FreeImage/Classes/FreeImageStreamIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

using System;
using System.IO;
using System.Buffers;
using System.Runtime.InteropServices;
using System.Diagnostics;

Expand Down Expand Up @@ -86,7 +87,7 @@ static unsafe uint streamRead(IntPtr buffer, uint size, uint count, fi_handle ha
}
uint readCount = 0;
byte* ptr = (byte*)buffer;
byte[] bufferTemp = new byte[size];
byte[] bufferTemp = ArrayPool<byte>.Shared.Rent((int)size);
int read;
while (readCount < count)
{
Expand All @@ -102,6 +103,7 @@ static unsafe uint streamRead(IntPtr buffer, uint size, uint count, fi_handle ha
}
readCount++;
}
ArrayPool<byte>.Shared.Return(bufferTemp);
return readCount;
}

Expand All @@ -116,7 +118,7 @@ static unsafe uint streamWrite(IntPtr buffer, uint size, uint count, fi_handle h
return 0;
}
uint writeCount = 0;
byte[] bufferTemp = new byte[size];
byte[] bufferTemp = ArrayPool<byte>.Shared.Rent((int)size);
byte* ptr = (byte*)buffer;
while (writeCount < count)
{
Expand All @@ -134,6 +136,7 @@ static unsafe uint streamWrite(IntPtr buffer, uint size, uint count, fi_handle h
}
writeCount++;
}
ArrayPool<byte>.Shared.Return(bufferTemp);
return writeCount;
}

Expand Down