From 35494bc81b59165ee9264cd1004bb05a120279a3 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Thu, 28 Sep 2023 10:54:58 +0100 Subject: Reduce allocations, the message takes ownership of the bytes read from the stream and is responsible for deallocating, rather than copying them to their own storage. --- src/proto/proto.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/proto/proto.zig') diff --git a/src/proto/proto.zig b/src/proto/proto.zig index 5e4489d..9025347 100644 --- a/src/proto/proto.zig +++ b/src/proto/proto.zig @@ -51,13 +51,13 @@ pub fn read_message(allocator: std.mem.Allocator, stream_reader: anytype) !Backe const tag = try stream_reader.readByte(); const len = try stream_reader.readIntBig(u32); const buf = try allocator.alloc(u8, @as(u32, @intCast(len - 4))); - defer allocator.free(buf); try stream_reader.readNoEof(buf); inline for (@typeInfo(BackendMessage).Union.fields) |field| { if (field.type.Tag == tag) { return @unionInit(BackendMessage, field.name, try field.type.read(allocator, buf)); } } else { + allocator.free(buf); return ProtocolError.InvalidMessageType; } } -- cgit v1.2.3-ZIG