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/data_row.zig | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) (limited to 'src/proto/data_row.zig') diff --git a/src/proto/data_row.zig b/src/proto/data_row.zig index c20b794..43c4526 100644 --- a/src/proto/data_row.zig +++ b/src/proto/data_row.zig @@ -12,25 +12,23 @@ const DataRow = @This(); buf: ?[]const u8 = null, // owned columns: [][]const u8, // also owned -pub fn read(a: std.mem.Allocator, b: []const u8) !DataRow { - if (b.len < 2) return ProtocolError.InvalidMessageLength; - var buf = try a.dupe(u8, b); - var res: DataRow = undefined; - res.buf = buf; - errdefer res.deinit(a); - +pub fn read(a: std.mem.Allocator, buf: []const u8) !DataRow { + if (buf.len < 2) return ProtocolError.InvalidMessageLength; + errdefer a.free(buf); const n_columns = std.mem.readIntBig(u16, buf[0..2]); const columns = try a.alloc([]const u8, n_columns); errdefer a.free(columns); var pos: usize = 2; for (0..n_columns) |col| { - const len = std.mem.readIntBig(u32, buf[pos..(pos+4)][0..4]); // second slice forces the slice size to be known at comptime and satisfy the type check on readIntBig! - const data = if (len > 0) buf[(pos+4)..(pos+4+len)] else &[_]u8{}; + const len = std.mem.readIntBig(u32, buf[pos..(pos + 4)][0..4]); // second slice forces the slice size to be known at comptime and satisfy the type check on readIntBig! + const data = if (len > 0) buf[(pos + 4)..(pos + 4 + len)] else &[_]u8{}; columns[col] = data; - pos += (4+len); + pos += (4 + len); } - res.columns = columns; - return res; + return .{ + .buf = buf, + .columns = columns, + }; } pub fn write(self: DataRow, a: std.mem.Allocator, stream_writer: anytype) !void { @@ -77,7 +75,6 @@ test "round trip" { try std.testing.expectEqual(Tag, tag); const len = try reader.readIntBig(u32); const buf = try allocator.alloc(u8, len - 4); - defer allocator.free(buf); try reader.readNoEof(buf); var sm2 = try DataRow.read(allocator, buf); defer sm2.deinit(allocator); -- cgit v1.2.3-ZIG