From 6a4dc3334a5fa8dbdddbb9c8e48bd67413dd93b3 Mon Sep 17 00:00:00 2001 From: Timon Kruiper Date: Sun, 17 May 2020 19:26:02 +0200 Subject: [PATCH] Wrap stdout in BufferedOutStream for more speed gotta go fast right :D Also remove some unused code --- src/main.zig | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/main.zig b/src/main.zig index a5d78fe..c8a3750 100644 --- a/src/main.zig +++ b/src/main.zig @@ -11,7 +11,7 @@ const analysis = @import("analysis.zig"); // Code is largely based off of https://github.com/andersfr/zig-lsp/blob/master/server.zig -var stdout: std.fs.File.OutStream = undefined; +var stdout: std.io.BufferedOutStream(4096, std.fs.File.OutStream) = undefined; var allocator: *std.mem.Allocator = undefined; var document_store: DocumentStore = undefined; @@ -46,8 +46,11 @@ fn send(reqOrRes: var) !void { var mem_buffer: [1024 * 128]u8 = undefined; var fbs = std.io.fixedBufferStream(&mem_buffer); try std.json.stringify(reqOrRes, std.json.StringifyOptions{}, fbs.outStream()); - try stdout.print("Content-Length: {}\r\n\r\n", .{fbs.pos}); - try stdout.writeAll(fbs.getWritten()); + + const stdout_stream = stdout.outStream(); + try stdout_stream.print("Content-Length: {}\r\n\r\n", .{fbs.pos}); + try stdout_stream.writeAll(fbs.getWritten()); + try stdout.flush(); } fn log(comptime fmt: []const u8, args: var) !void { @@ -82,8 +85,11 @@ fn respondGeneric(id: i64, response: []const u8) !void { // Numbers of character that will be printed from this string: len - 3 brackets // 1 from the beginning (escaped) and the 2 from the arg {} const json_fmt = "{{\"jsonrpc\":\"2.0\",\"id\":{}"; - try stdout.print("Content-Length: {}\r\n\r\n" ++ json_fmt, .{ response.len + id_digits + json_fmt.len - 3, id }); - try stdout.writeAll(response); + + const stdout_stream = stdout.outStream(); + try stdout_stream.print("Content-Length: {}\r\n\r\n" ++ json_fmt, .{ response.len + id_digits + json_fmt.len - 3, id }); + try stdout_stream.writeAll(response); + try stdout.flush(); } // TODO: Is this correct or can we get a better end? @@ -609,17 +615,9 @@ pub fn main() anyerror!void { allocator = &debug_alloc_state.allocator; } - // Init buffer for stdin read - - var buffer = std.ArrayList(u8).init(allocator); - defer buffer.deinit(); - - try buffer.resize(4096); - // Init global vars - const stdin = std.io.getStdIn().inStream(); - stdout = std.io.getStdOut().outStream(); + stdout = std.io.bufferedOutStream(std.io.getStdOut().outStream()); // Read the configuration, if any. const config_parse_options = std.json.ParseOptions{ .allocator = allocator };