From c9f9dbf634996eec05af80ebf22aa18d30bc75c0 Mon Sep 17 00:00:00 2001 From: IntegratedQuantum Date: Thu, 18 Apr 2024 17:02:19 +0200 Subject: [PATCH] Avoid unnecessary syscalls from checking the millitime in each call of `isStillNeeded` Reduces lag caused by updating the priorities in the threadpool. --- src/audio.zig | 2 +- src/renderer/chunk_meshing.zig | 2 +- src/renderer/mesh_storage.zig | 2 +- src/server/world.zig | 6 +++--- src/utils.zig | 4 ++-- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/audio.zig b/src/audio.zig index 3498d652..af8b8b0a 100644 --- a/src/audio.zig +++ b/src/audio.zig @@ -110,7 +110,7 @@ const MusicLoadTask = struct { return std.math.floatMax(f32); } - pub fn isStillNeeded(_: *MusicLoadTask) bool { + pub fn isStillNeeded(_: *MusicLoadTask, _: i64) bool { return true; } diff --git a/src/renderer/chunk_meshing.zig b/src/renderer/chunk_meshing.zig index f221d9a3..bc0ced4d 100644 --- a/src/renderer/chunk_meshing.zig +++ b/src/renderer/chunk_meshing.zig @@ -539,7 +539,7 @@ pub const ChunkMesh = struct { return 1000000; } - pub fn isStillNeeded(_: *LightRefreshTask) bool { + pub fn isStillNeeded(_: *LightRefreshTask, _: i64) bool { return true; // TODO: Is it worth checking for this? } diff --git a/src/renderer/mesh_storage.zig b/src/renderer/mesh_storage.zig index a00030f6..fdf1c36e 100644 --- a/src/renderer/mesh_storage.zig +++ b/src/renderer/mesh_storage.zig @@ -1011,7 +1011,7 @@ pub const MeshGenerationTask = struct { return self.mesh.pos.getPriority(game.Player.getPosBlocking()); // TODO: This is called in loop, find a way to do this without calling the mutex every time. } - pub fn isStillNeeded(self: *MeshGenerationTask) bool { + pub fn isStillNeeded(self: *MeshGenerationTask, _: i64) bool { const distanceSqr = self.mesh.pos.getMinDistanceSquared(game.Player.getPosBlocking()); // TODO: This is called in loop, find a way to do this without calling the mutex every time. var maxRenderDistance = settings.renderDistance*chunk.chunkSize*self.mesh.pos.voxelSize; maxRenderDistance += 2*self.mesh.pos.voxelSize*chunk.chunkSize; diff --git a/src/server/world.zig b/src/server/world.zig index ffdba3de..62d7e3af 100644 --- a/src/server/world.zig +++ b/src/server/world.zig @@ -59,7 +59,7 @@ const ChunkManager = struct { } } - pub fn isStillNeeded(self: *ChunkLoadTask) bool { + pub fn isStillNeeded(self: *ChunkLoadTask, milliTime: i64) bool { // TODO: if(self.source) |source| { _ = source; @@ -75,7 +75,7 @@ const ChunkManager = struct { // return false; // } } - if(std.time.milliTimestamp() - self.creationTime > 10000) { // Only remove stuff after 10 seconds to account for trouble when for example teleporting. + if(milliTime - self.creationTime > 10000) { // Only remove stuff after 10 seconds to account for trouble when for example teleporting. server.mutex.lock(); defer server.mutex.unlock(); for(server.users.items) |user| { @@ -132,7 +132,7 @@ const ChunkManager = struct { } } - pub fn isStillNeeded(self: *LightMapLoadTask) bool { + pub fn isStillNeeded(self: *LightMapLoadTask, _: i64) bool { _ = self; // TODO: Do these tasks need to be culled? return true; } diff --git a/src/utils.zig b/src/utils.zig index 6e0d440c..f95d5262 100644 --- a/src/utils.zig +++ b/src/utils.zig @@ -991,7 +991,7 @@ pub const ThreadPool = struct { }; pub const VTable = struct { getPriority: *const fn(*anyopaque) f32, - isStillNeeded: *const fn(*anyopaque) bool, + isStillNeeded: *const fn(*anyopaque, milliTime: i64) bool, run: *const fn(*anyopaque) void, clean: *const fn(*anyopaque) void, }; @@ -1080,7 +1080,7 @@ pub const ThreadPool = struct { var i: u32 = 0; while(i < self.loadList.size) { const task = &self.loadList.array[i]; - if(!task.vtable.isStillNeeded(task.self)) { + if(!task.vtable.isStillNeeded(task.self, lastUpdate)) { task.vtable.clean(task.self); self.loadList.removeIndex(i); } else {