mirror of
https://git.robbyzambito.me/zits
synced 2026-02-04 03:34:48 +00:00
Significant speed improvement
This commit is contained in:
@@ -39,15 +39,14 @@ const Subscription = struct {
|
|||||||
if (self.queue_group) |g| alloc.free(g);
|
if (self.queue_group) |g| alloc.free(g);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn send(self: *Subscription, io: Io, bytes: []const []const u8) !void {
|
fn send(self: *Subscription, io: Io, buf: []u8, bytes: []const []const u8) !void {
|
||||||
|
var w: std.Io.Writer = .fixed(buf);
|
||||||
|
for (bytes) |chunk| {
|
||||||
|
w.writeAll(chunk) catch unreachable;
|
||||||
|
}
|
||||||
try self.queue_lock.lock(io);
|
try self.queue_lock.lock(io);
|
||||||
defer self.queue_lock.unlock(io);
|
defer self.queue_lock.unlock(io);
|
||||||
for (bytes) |chunk| {
|
try self.queue.putAll(io, w.buffered());
|
||||||
// Uncancelable because canceling the sender in the middle of this loop
|
|
||||||
// would put an invalid set series of bytes in the receivers queue.
|
|
||||||
_ = try self.queue.putUncancelable(io, chunk, chunk.len);
|
|
||||||
}
|
|
||||||
try io.checkCancel();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -181,6 +180,9 @@ fn handleConnection(
|
|||||||
var recv_queue: Queue(u8) = .init(qbuf);
|
var recv_queue: Queue(u8) = .init(qbuf);
|
||||||
defer recv_queue.close(io);
|
defer recv_queue.close(io);
|
||||||
|
|
||||||
|
const msg_write_buf: []u8 = try alloc.alignedAlloc(u8, .fromByteUnits(std.atomic.cache_line), 1 * 1024 * 1024);
|
||||||
|
defer alloc.free(msg_write_buf);
|
||||||
|
|
||||||
// Create client
|
// Create client
|
||||||
var client: Client = .init(null, &recv_queue, in, out);
|
var client: Client = .init(null, &recv_queue, in, out);
|
||||||
defer client.deinit(server_allocator);
|
defer client.deinit(server_allocator);
|
||||||
@@ -207,7 +209,7 @@ fn handleConnection(
|
|||||||
.PUB => {
|
.PUB => {
|
||||||
@branchHint(.likely);
|
@branchHint(.likely);
|
||||||
// log.debug("received a pub msg", .{});
|
// log.debug("received a pub msg", .{});
|
||||||
server.publishMessage(io, rand, server_allocator, &client, .@"pub") catch |err| switch (err) {
|
server.publishMessage(io, rand, server_allocator, msg_write_buf, &client, .@"pub") catch |err| switch (err) {
|
||||||
error.ReadFailed => return reader.err.?,
|
error.ReadFailed => return reader.err.?,
|
||||||
error.EndOfStream => return error.ClientDisconnected,
|
error.EndOfStream => return error.ClientDisconnected,
|
||||||
else => |e| return e,
|
else => |e| return e,
|
||||||
@@ -215,7 +217,7 @@ fn handleConnection(
|
|||||||
},
|
},
|
||||||
.HPUB => {
|
.HPUB => {
|
||||||
@branchHint(.likely);
|
@branchHint(.likely);
|
||||||
server.publishMessage(io, rand, server_allocator, &client, .hpub) catch |err| switch (err) {
|
server.publishMessage(io, rand, server_allocator, msg_write_buf, &client, .hpub) catch |err| switch (err) {
|
||||||
error.ReadFailed => return reader.err.?,
|
error.ReadFailed => return reader.err.?,
|
||||||
error.EndOfStream => return error.ClientDisconnected,
|
error.EndOfStream => return error.ClientDisconnected,
|
||||||
else => |e| return e,
|
else => |e| return e,
|
||||||
@@ -298,6 +300,7 @@ fn publishMessage(
|
|||||||
io: Io,
|
io: Io,
|
||||||
rand: std.Random,
|
rand: std.Random,
|
||||||
alloc: Allocator,
|
alloc: Allocator,
|
||||||
|
msg_write_buf: []u8,
|
||||||
source_client: *Client,
|
source_client: *Client,
|
||||||
comptime pub_or_hpub: enum { @"pub", hpub },
|
comptime pub_or_hpub: enum { @"pub", hpub },
|
||||||
) !void {
|
) !void {
|
||||||
@@ -384,7 +387,7 @@ fn publishMessage(
|
|||||||
);
|
);
|
||||||
msg_chunks.appendAssumeCapacity(msg.payload);
|
msg_chunks.appendAssumeCapacity(msg.payload);
|
||||||
|
|
||||||
subscription.send(io, msg_chunks.items[0..chunk_count]) catch |err| switch (err) {
|
subscription.send(io, msg_write_buf, msg_chunks.items[0..chunk_count]) catch |err| switch (err) {
|
||||||
error.Closed => {},
|
error.Closed => {},
|
||||||
error.Canceled => |e| return e,
|
error.Canceled => |e| return e,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -40,16 +40,6 @@ pub fn start(self: *Client, io: std.Io) !void {
|
|||||||
std.debug.assert(self.to_client.end == 0);
|
std.debug.assert(self.to_client.end == 0);
|
||||||
while (true) {
|
while (true) {
|
||||||
self.to_client.end = try self.recv_queue.get(io, self.to_client.buffer, 1);
|
self.to_client.end = try self.recv_queue.get(io, self.to_client.buffer, 1);
|
||||||
// Wait 1 nanosecond to see if more data is in the queue.
|
|
||||||
// If there is, add it to the write buffer before sending it.
|
|
||||||
// The reason for this is because if we send the first chunk as soon as we get it,
|
|
||||||
// we will likely be sending a partial message, which will end up being way slower.
|
|
||||||
try io.sleep(.fromNanoseconds(1), .awake);
|
|
||||||
self.to_client.end += try self.recv_queue.get(
|
|
||||||
io,
|
|
||||||
self.to_client.buffer[self.to_client.end..],
|
|
||||||
0,
|
|
||||||
);
|
|
||||||
try self.to_client.flush();
|
try self.to_client.flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user