authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-18 21:08:30-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-06-18 21:08:30-04:00
logf7bcc8e04087e2308582d8b6f068e2e71b65bef9
tree19cf169ef5797b19337ec1ba578097f6c2d01a08
parent8b49487c3345d6e203d8c72695e639b42a93c119

rework zig fmt to only make one allocation

taking advantage of the fstat size

3 files changed, 37 insertions(+), 9 deletions(-)

lib/std/fs.zig+2-7
......@@ -1229,14 +1229,9 @@ pub const Dir = struct {
12291229 var file = try self.openFile(file_path, .{});
12301230 defer file.close();
12311231
1232 const size = math.cast(usize, try file.getEndPos()) catch math.maxInt(usize);
1233 if (size > max_bytes) return error.FileTooBig;
1232 const stat_size = try file.getEndPos();
12341233
1235 const buf = try allocator.allocWithOptions(u8, size, alignment, optional_sentinel);
1236 errdefer allocator.free(buf);
1237
1238 try file.inStream().readNoEof(buf);
1239 return buf;
1234 return file.readAllAllocOptions(allocator, stat_size, max_bytes, alignment, optional_sentinel);
12401235 }
12411236
12421237 pub const DeleteTreeError = error{
lib/std/fs/file.zig+27
......@@ -306,6 +306,33 @@ pub const File = struct {
306306 try os.futimens(self.handle, &times);
307307 }
308308
309 /// On success, caller owns returned buffer.
310 /// If the file is larger than `max_bytes`, returns `error.FileTooBig`.
311 pub fn readAllAlloc(self: File, allocator: *mem.Allocator, stat_size: u64, max_bytes: usize) ![]u8 {
312 return self.readAllAllocOptions(allocator, stat_size, max_bytes, @alignOf(u8), null);
313 }
314
315 /// On success, caller owns returned buffer.
316 /// If the file is larger than `max_bytes`, returns `error.FileTooBig`.
317 /// Allows specifying alignment and a sentinel value.
318 pub fn readAllAllocOptions(
319 self: File,
320 allocator: *mem.Allocator,
321 stat_size: u64,
322 max_bytes: usize,
323 comptime alignment: u29,
324 comptime optional_sentinel: ?u8,
325 ) !(if (optional_sentinel) |s| [:s]align(alignment) u8 else []align(alignment) u8) {
326 const size = math.cast(usize, stat_size) catch math.maxInt(usize);
327 if (size > max_bytes) return error.FileTooBig;
328
329 const buf = try allocator.allocWithOptions(u8, size, alignment, optional_sentinel);
330 errdefer allocator.free(buf);
331
332 try self.inStream().readNoEof(buf);
333 return buf;
334 }
335
309336 pub const ReadError = os.ReadError;
310337 pub const PReadError = os.PReadError;
311338
src-self-hosted/main.zig+8-2
......@@ -707,7 +707,13 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
707707 };
708708 defer source_file.close();
709709
710 const source_code = source_file.reader().readAllAlloc(fmt.gpa, max_src_size) catch |err| {
710 const stat = source_file.stat() catch |err| {
711 std.debug.warn("unable to stat '{}': {}\n", .{ file_path, err });
712 fmt.any_error = true;
713 return;
714 };
715
716 const source_code = source_file.readAllAlloc(fmt.gpa, stat.size, max_src_size) catch |err| {
711717 std.debug.warn("unable to read '{}': {}\n", .{ file_path, err });
712718 fmt.any_error = true;
713719 return;
......@@ -736,7 +742,7 @@ fn fmtPath(fmt: *Fmt, file_path: []const u8, check_mode: bool) FmtError!void {
736742 fmt.any_error = true;
737743 }
738744 } else {
739 const baf = try io.BufferedAtomicFile.create(fmt.gpa, fs.cwd(), real_path, .{ .mode = try source_file.mode() });
745 const baf = try io.BufferedAtomicFile.create(fmt.gpa, fs.cwd(), real_path, .{ .mode = stat.mode });
740746 defer baf.destroy();
741747
742748 const anything_changed = try std.zig.render(fmt.gpa, baf.stream(), tree);