From 64a91ff7e47ab9ba12a28d09f59e803560b7e522 Mon Sep 17 00:00:00 2001 From: Edward Dean Date: Fri, 5 Mar 2021 17:03:20 +0000 Subject: [PATCH] Improve build speed This improves the speed of clearing of the FAT image with using a bigger buffer Zig master --- mkfat32.zig | 29 ++++++++++++++++++++++++++--- test/runtime_test.zig | 2 +- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/mkfat32.zig b/mkfat32.zig index b8ea3a3..22f7ea3 100644 --- a/mkfat32.zig +++ b/mkfat32.zig @@ -555,6 +555,29 @@ pub const Fat32 = struct { }; } + /// + /// Clear the stream. This is the same as writeByteNTimes but with a bigger buffer (4096 bytes). + /// This improves performance a lot. + /// + /// Arguments: + /// IN stream: anytype - The stream to clear. + /// IN size: usize - The size to clear from the beginning + /// + /// Error: @TypeOf(stream).WriteError + /// @TypeOf(stream).WriteError - Error writing to the stream. + /// + fn clearStream(stream: anytype, size: usize) ErrorSet(@TypeOf(stream))!void { + comptime const buff_size = 4096; + comptime const bytes: [buff_size]u8 = [_]u8{0x00} ** buff_size; + + var remaining: usize = size; + while (remaining > 0) { + const to_write = std.math.min(remaining, bytes.len); + try stream.writer().writeAll(bytes[0..to_write]); + remaining -= to_write; + } + } + /// /// Make a FAT32 image. This will either use the default options or modified defaults from the /// user. The file will be saved to the path specified. If quick format is on, then the entire @@ -568,7 +591,7 @@ pub const Fat32 = struct { /// IN quick_format: bool - Whether to completely zero the stream initially or zero just /// the important sectors. /// - /// Error: @TypeOf(stream).WriteError || @TypeOf(stream).SeekError || Error + /// Error: @TypeOf(stream).WriteError || @TypeOf(stream).SeekError || Error /// @TypeOf(stream).WriteError - If there is an error when writing. See the relevant error for the stream. /// @TypeOf(stream).SeekError - If there is an error when seeking. See the relevant error for the stream. /// Error.InvalidOptionValue - In the user has provided invalid options. @@ -583,10 +606,10 @@ pub const Fat32 = struct { try stream.seekableStream().seekTo(0); if (quick_format) { // Zero just the reserved and FAT sectors - try stream.writer().writeByteNTimes(0x00, (fat32_header.reserved_sectors + (fat32_header.sectors_per_fat * 2)) * fat32_header.bytes_per_sector); + try clearStream(stream, (fat32_header.reserved_sectors + (fat32_header.sectors_per_fat * 2)) * fat32_header.bytes_per_sector); } else { const image_size = std.mem.alignBackward(options.image_size, fat32_header.bytes_per_sector); - try stream.writer().writeByteNTimes(0x00, image_size); + try clearStream(stream, image_size); } // Write the boot sector with the bootstrap code and header and the backup boot sector. diff --git a/test/runtime_test.zig b/test/runtime_test.zig index c8b5c8f..a12203d 100644 --- a/test/runtime_test.zig +++ b/test/runtime_test.zig @@ -206,7 +206,7 @@ pub const RuntimeStep = struct { try self.os_proc.spawn(); // Start up the read thread - var thread = try Thread.spawn(self, read_logs); + var thread = try Thread.spawn(read_logs, self); // Call the testing function const res = self.test_func(self);