faster writes performance

Former-commit-id: 2c212929f8
This commit is contained in:
Jarred Sumner
2021-05-27 00:42:02 -07:00
parent 84be179f7c
commit feb7adcd08
2 changed files with 30 additions and 7 deletions

View File

@@ -105,7 +105,7 @@ pub fn hash_eqlFn(a: HashKeyType, b: HashKeyType) bool {
return a == b;
}
pub const ItemStatus = packed enum(u3) {
pub const ItemStatus = enum(u3) {
unknown,
exists,
not_found,
@@ -589,6 +589,6 @@ pub fn BSSMap(comptime ValueType: type, comptime count: anytype, store_keys: boo
};
}
fn constStrToU8(s: []const u8) []u8 {
pub fn constStrToU8(s: []const u8) []u8 {
return @intToPtr([*]u8, @ptrToInt(s.ptr))[0..s.len];
}

View File

@@ -322,18 +322,41 @@ pub const Cli = struct {
if (args.write) |write| {
if (write) {
var open_file_limit: usize = 32;
if (std.os.getrlimit(.NOFILE)) |limit| {
open_file_limit = limit.cur;
} else |err| {}
const do_we_need_to_close = open_file_limit > result.output_files.len * 2;
did_write = true;
var root_dir = try std.fs.openDirAbsolute(args.absolute_working_dir.?, std.fs.Dir.OpenDirOptions{});
var root_dir = try std.fs.openDirAbsolute(result.outbase, std.fs.Dir.OpenDirOptions{});
defer root_dir.close();
for (result.output_files) |f| {
try root_dir.makePath(std.fs.path.dirname(f.path) orelse unreachable);
var fp = f.path;
if (fp[0] == std.fs.path.sep) {
fp = fp[1..];
}
var _handle = try std.fs.createFileAbsolute(f.path, std.fs.File.CreateFlags{
var _handle = root_dir.createFile(fp, std.fs.File.CreateFlags{
.truncate = true,
});
}) catch |err| brk: {
// Only bother to create the directory if there's an error because that's probably why it errored
if (std.fs.path.dirname(fp)) |dirname| {
root_dir.makePath(dirname) catch {};
}
// Then, retry!
break :brk (root_dir.createFile(fp, std.fs.File.CreateFlags{
.truncate = true,
}) catch |err2| return err2);
};
try _handle.seekTo(0);
defer _handle.close();
defer {
if (do_we_need_to_close) {
_handle.close();
}
}
try _handle.writeAll(f.contents);
}