Avoid unnecessary syscalls from checking the millitime in each call of isStillNeeded

Reduces lag caused by updating the priorities in the threadpool.
This commit is contained in:
IntegratedQuantum 2024-04-18 17:02:19 +02:00
parent 22a482eb65
commit c9f9dbf634
5 changed files with 8 additions and 8 deletions

View File

@ -110,7 +110,7 @@ const MusicLoadTask = struct {
return std.math.floatMax(f32); return std.math.floatMax(f32);
} }
pub fn isStillNeeded(_: *MusicLoadTask) bool { pub fn isStillNeeded(_: *MusicLoadTask, _: i64) bool {
return true; return true;
} }

View File

@ -539,7 +539,7 @@ pub const ChunkMesh = struct {
return 1000000; return 1000000;
} }
pub fn isStillNeeded(_: *LightRefreshTask) bool { pub fn isStillNeeded(_: *LightRefreshTask, _: i64) bool {
return true; // TODO: Is it worth checking for this? return true; // TODO: Is it worth checking for this?
} }

View File

@ -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. 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. 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; var maxRenderDistance = settings.renderDistance*chunk.chunkSize*self.mesh.pos.voxelSize;
maxRenderDistance += 2*self.mesh.pos.voxelSize*chunk.chunkSize; maxRenderDistance += 2*self.mesh.pos.voxelSize*chunk.chunkSize;

View File

@ -59,7 +59,7 @@ const ChunkManager = struct {
} }
} }
pub fn isStillNeeded(self: *ChunkLoadTask) bool { pub fn isStillNeeded(self: *ChunkLoadTask, milliTime: i64) bool {
// TODO: // TODO:
if(self.source) |source| { if(self.source) |source| {
_ = source; _ = source;
@ -75,7 +75,7 @@ const ChunkManager = struct {
// return false; // 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(); server.mutex.lock();
defer server.mutex.unlock(); defer server.mutex.unlock();
for(server.users.items) |user| { 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? _ = self; // TODO: Do these tasks need to be culled?
return true; return true;
} }

View File

@ -991,7 +991,7 @@ pub const ThreadPool = struct {
}; };
pub const VTable = struct { pub const VTable = struct {
getPriority: *const fn(*anyopaque) f32, getPriority: *const fn(*anyopaque) f32,
isStillNeeded: *const fn(*anyopaque) bool, isStillNeeded: *const fn(*anyopaque, milliTime: i64) bool,
run: *const fn(*anyopaque) void, run: *const fn(*anyopaque) void,
clean: *const fn(*anyopaque) void, clean: *const fn(*anyopaque) void,
}; };
@ -1080,7 +1080,7 @@ pub const ThreadPool = struct {
var i: u32 = 0; var i: u32 = 0;
while(i < self.loadList.size) { while(i < self.loadList.size) {
const task = &self.loadList.array[i]; const task = &self.loadList.array[i];
if(!task.vtable.isStillNeeded(task.self)) { if(!task.vtable.isStillNeeded(task.self, lastUpdate)) {
task.vtable.clean(task.self); task.vtable.clean(task.self);
self.loadList.removeIndex(i); self.loadList.removeIndex(i);
} else { } else {