Merge branch 'main' into mwp
This commit is contained in:
@@ -47,6 +47,7 @@ pub fn build(b: *std.Build) void {
|
|||||||
.root_source_file = b.path("src/files.zig"),
|
.root_source_file = b.path("src/files.zig"),
|
||||||
.target = target,
|
.target = target,
|
||||||
.optimize = optimize,
|
.optimize = optimize,
|
||||||
|
.link_libc = true,
|
||||||
});
|
});
|
||||||
files_module.addImport("core", core_module);
|
files_module.addImport("core", core_module);
|
||||||
files_module.addImport("protocol", protocol_module);
|
files_module.addImport("protocol", protocol_module);
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ pub const RateLimiter = struct {
|
|||||||
max_tokens: u64,
|
max_tokens: u64,
|
||||||
/// Token refill rate (tokens per second)
|
/// Token refill rate (tokens per second)
|
||||||
refill_rate: u64,
|
refill_rate: u64,
|
||||||
/// Last refill timestamp (milliseconds) - using i64 for cross-platform atomic support
|
/// Last refill timestamp (nanoseconds)
|
||||||
last_refill: std.atomic.Value(i64),
|
last_refill: std.atomic.Value(i64),
|
||||||
/// Mutex for non-atomic operations
|
/// Mutex for non-atomic operations
|
||||||
mutex: std.Thread.Mutex = .{},
|
mutex: std.Thread.Mutex = .{},
|
||||||
@@ -26,7 +26,7 @@ pub const RateLimiter = struct {
|
|||||||
.tokens = std.atomic.Value(u64).init(config.burst_size),
|
.tokens = std.atomic.Value(u64).init(config.burst_size),
|
||||||
.max_tokens = config.burst_size,
|
.max_tokens = config.burst_size,
|
||||||
.refill_rate = config.tokens_per_second,
|
.refill_rate = config.tokens_per_second,
|
||||||
.last_refill = std.atomic.Value(i64).init(time.milliTimestamp()),
|
.last_refill = std.atomic.Value(i64).init(@intCast(time.nanoTimestamp())),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,14 +79,15 @@ pub const RateLimiter = struct {
|
|||||||
|
|
||||||
/// Refill tokens based on elapsed time
|
/// Refill tokens based on elapsed time
|
||||||
fn refill(self: *RateLimiter) void {
|
fn refill(self: *RateLimiter) void {
|
||||||
const now = time.milliTimestamp();
|
const now: i64 = @intCast(time.nanoTimestamp());
|
||||||
const last = self.last_refill.load(.acquire);
|
const last = self.last_refill.load(.acquire);
|
||||||
const elapsed_ms = now - last;
|
const elapsed_ns = now - last;
|
||||||
|
|
||||||
if (elapsed_ms <= 0) return;
|
if (elapsed_ns <= 0) return;
|
||||||
|
|
||||||
// Calculate tokens to add (using milliseconds)
|
// Calculate tokens to add (using nanoseconds for precision)
|
||||||
const elapsed_seconds = @as(u64, @intCast(@divFloor(elapsed_ms, time.ms_per_s)));
|
const elapsed_ns_u64: u64 = @intCast(elapsed_ns);
|
||||||
|
const elapsed_seconds = elapsed_ns_u64 / time.ns_per_s;
|
||||||
if (elapsed_seconds == 0) return;
|
if (elapsed_seconds == 0) return;
|
||||||
|
|
||||||
const new_tokens = elapsed_seconds * self.refill_rate;
|
const new_tokens = elapsed_seconds * self.refill_rate;
|
||||||
|
|||||||
@@ -1113,8 +1113,13 @@ pub const FileWriter = struct {
|
|||||||
if (builtin.os.tag == .linux) {
|
if (builtin.os.tag == .linux) {
|
||||||
// FALLOC_FL_KEEP_SIZE = 0x01 - allocate without changing file size
|
// FALLOC_FL_KEEP_SIZE = 0x01 - allocate without changing file size
|
||||||
const FALLOC_FL_KEEP_SIZE = 0x01;
|
const FALLOC_FL_KEEP_SIZE = 0x01;
|
||||||
const fd: i32 = @intCast(file.handle);
|
const fd = file.handle;
|
||||||
const result = std.os.linux.fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, @intCast(size));
|
const result = std.os.linux.syscall4(.fallocate,
|
||||||
|
@as(usize, @intCast(fd)),
|
||||||
|
@as(usize, FALLOC_FL_KEEP_SIZE),
|
||||||
|
@as(usize, 0), // offset
|
||||||
|
@as(usize, @intCast(size)),
|
||||||
|
);
|
||||||
if (result == 0) {
|
if (result == 0) {
|
||||||
std.log.debug("PERF: Pre-allocated {} MB with fallocate", .{size / (1024 * 1024)});
|
std.log.debug("PERF: Pre-allocated {} MB with fallocate", .{size / (1024 * 1024)});
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user