Skip to content

Commit 43b97ef

Browse files
committed
fix: return rent buffer
1 parent da64304 commit 43b97ef

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Editor/USGEngine.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,18 +332,26 @@ static bool TryEmit(string assetsRelPath, CachedTypeInfo info)
332332
using (var fs = new FileStream(context.OutputPath, FileMode.Create, FileAccess.Write))
333333
{
334334
//Span<byte> buffer = stackalloc byte[BUFFER_LENGTH];
335-
Span<byte> buffer = ArrayPool<byte>.Shared.Rent(BUFFER_LENGTH).AsSpan(0, BUFFER_LENGTH);
336-
var span = sb.ToString().AsSpan();
337-
int len, written;
338-
for (int start = 0; start < span.Length; start += BUFFER_MAX_CHAR_LENGTH)
335+
var rentBufferToReturn = ArrayPool<byte>.Shared.Rent(BUFFER_LENGTH);
336+
try
339337
{
340-
len = BUFFER_MAX_CHAR_LENGTH;
341-
if (len + start > span.Length) len = span.Length - start;
338+
var buffer = rentBufferToReturn.AsSpan(0, BUFFER_LENGTH);
339+
var span = sb.ToString().AsSpan();
340+
int len, written;
341+
for (int start = 0; start < span.Length; start += BUFFER_MAX_CHAR_LENGTH)
342+
{
343+
len = BUFFER_MAX_CHAR_LENGTH;
344+
if (len + start > span.Length) len = span.Length - start;
342345

343-
written = info.Attribute.OutputFileEncoding.GetBytes(span.Slice(start, len), buffer);
344-
fs.Write(buffer.Slice(0, written));
346+
written = info.Attribute.OutputFileEncoding.GetBytes(span.Slice(start, len), buffer);
347+
fs.Write(buffer.Slice(0, written));
348+
}
349+
fs.Flush();
350+
}
351+
finally
352+
{
353+
ArrayPool<byte>.Shared.Return(rentBufferToReturn);
345354
}
346-
fs.Flush();
347355
}
348356

349357
#else

0 commit comments

Comments
 (0)