Skip to content

Commit bcbb660

Browse files
committed
[Core] Use ReadOnlyMemoryContent for Highway to avoid copying
1 parent bb3dda4 commit bcbb660

1 file changed

Lines changed: 8 additions & 8 deletions

File tree

Lagrange.Core/Internal/Context/HighwayContext.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,16 @@ public async Task<bool> UploadFile(Stream stream, int commandId, ReadOnlyMemory<
102102

103103
bool end = currentBlockOffset + payload >= fileSize;
104104
var upload = ArrayPool<byte>.Shared.Rent(1 + 1 + 4 + 4 + headProto.Length + (int)payload);
105-
var uploadSpan = upload.AsSpan(0, 1 + 1 + 4 + 4 + headProto.Length + (int)payload);
105+
var memory = upload.AsMemory(0, 1 + 1 + 4 + 4 + headProto.Length + (int)payload);
106106

107-
uploadSpan[0] = 0x28;
108-
BinaryPrimitives.WriteUInt32BigEndian(uploadSpan[1..], (uint)headProto.Length);
109-
BinaryPrimitives.WriteUInt32BigEndian(uploadSpan[5..], (uint)payload);
110-
headProto.Span.CopyTo(uploadSpan[9..]);
111-
buffer.AsSpan(0, (int)payload).CopyTo(uploadSpan[(9 + headProto.Length)..]);
112-
uploadSpan[^1] = 0x29;
107+
memory.Span[0] = 0x28;
108+
BinaryPrimitives.WriteUInt32BigEndian(memory.Span[1..], (uint)headProto.Length);
109+
BinaryPrimitives.WriteUInt32BigEndian(memory.Span[5..], (uint)payload);
110+
headProto.Span.CopyTo(memory.Span[9..]);
111+
buffer.AsSpan(0, (int)payload).CopyTo(memory.Span[(9 + headProto.Length)..]);
112+
memory.Span[^1] = 0x29;
113113

114-
var content = new ByteArrayContent(upload[..(1 + 1 + 4 + 4 + headProto.Length + (int)payload)]);
114+
var content = new ReadOnlyMemoryContent(memory);
115115
var request = new HttpRequestMessage(HttpMethod.Post, $"http://{_url}")
116116
{
117117
Content = content, Headers = { { "Connection", end ? "close" : "keep-alive" } }

0 commit comments

Comments
 (0)