From 7b2a754d81d1ebe3f8980afd2b62fe4c98184582 Mon Sep 17 00:00:00 2001 From: MrBounty Date: Sun, 17 Nov 2024 20:45:58 +0100 Subject: [PATCH] Check if additional data count is reached at each iteration Before I was checking after the filter.evaluate is true. So I can end up with the limit reach, but the thread will continue parsing until it find one that is true, add it and then check if the limit is reach. Which mean I endup with more than what I want and the thread could stop before, making things faster. --- src/fileEngine.zig | 16 +++++++++++++++- src/schemaEngine.zig | 1 - src/threadEngine.zig | 8 +++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/fileEngine.zig b/src/fileEngine.zig index f717fdb..1760810 100644 --- a/src/fileEngine.zig +++ b/src/fileEngine.zig @@ -279,7 +279,7 @@ pub const FileEngine = struct { } /// Use a struct name and filter to populate a map with all UUID bytes as key and void as value - /// This map is use as value for the link array, so I can do a `contains` on it. + /// This map is use as value for the ConditionValue of links, so I can do a `contains` on it. pub fn populateVoidUUIDMap( self: *FileEngine, struct_name: []const u8, @@ -330,6 +330,16 @@ pub const FileEngine = struct { for (thread_writer_list) |list| { for (list.items) |uuid| _ = map.getOrPut(uuid) catch return ZipponError.MemoryError; } + + if (additional_data.entity_count_to_find == 0) return; + + if (map.count() > additional_data.entity_count_to_find) { + log.err("Found {d} entity in populateVoidUUIDMap but max is: {d}", .{ map.count(), additional_data.entity_count_to_find }); + var iter = map.iterator(); + while (iter.next()) |entry| { + log.debug("{s}", .{UUID.format_bytes(entry.key_ptr.bytes)}); + } + } } fn populateVoidUUIDMapOneFile( @@ -358,6 +368,7 @@ pub const FileEngine = struct { defer iter.deinit(); while (iter.next() catch return) |row| { + if (sync_context.checkStructLimit()) break; if (filter == null or filter.?.evaluate(row)) { list.*.append(UUID{ .bytes = row[0].UUID }) catch |err| { sync_context.logError("Error initializing DataIterator", err); @@ -474,6 +485,7 @@ pub const FileEngine = struct { sync_context.logError("Error in iter next", err); return; }) |row| { + if (sync_context.checkStructLimit()) break; if (filter) |f| if (!f.evaluate(row)) continue; writeEntity( @@ -719,6 +731,7 @@ pub const FileEngine = struct { sync_context.logError("Parsing files", err); return; }) |row| { + if (sync_context.checkStructLimit()) break; if (filter == null or filter.?.evaluate(row)) { // Add the unchanged Data in the new_data_buff new_data_buff[0] = row[0]; @@ -872,6 +885,7 @@ pub const FileEngine = struct { sync_context.logError("Error during iter", err); return; }) |row| { + if (sync_context.checkStructLimit()) break; if (filter == null or filter.?.evaluate(row)) { writer.print("{{\"{s}\"}},", .{UUID.format_bytes(row[0].UUID)}) catch |err| { sync_context.logError("Error writting", err); diff --git a/src/schemaEngine.zig b/src/schemaEngine.zig index dc7a70b..033cf23 100644 --- a/src/schemaEngine.zig +++ b/src/schemaEngine.zig @@ -125,7 +125,6 @@ pub const SchemaEngine = struct { /// Chech if the name of a struct is in the current schema pub fn isStructNameExists(self: *SchemaEngine, struct_name: []const u8) bool { var i: u16 = 0; - log.debug("\n\n{any}\n\n", .{self.struct_array}); while (i < self.struct_array.len) : (i += 1) if (std.mem.eql(u8, self.struct_array[i].name, struct_name)) return true; return false; } diff --git a/src/threadEngine.zig b/src/threadEngine.zig index 0c9efdb..0a90a51 100644 --- a/src/threadEngine.zig +++ b/src/threadEngine.zig @@ -34,7 +34,13 @@ pub const ThreadSyncContext = struct { pub fn incrementAndCheckStructLimit(self: *ThreadSyncContext) bool { if (self.max_struct == 0) return false; const new_count = self.processed_struct.fetchAdd(1, .monotonic); - return new_count >= self.max_struct; + return (new_count + 1) >= self.max_struct; + } + + pub fn checkStructLimit(self: *ThreadSyncContext) bool { + if (self.max_struct == 0) return false; + const count = self.processed_struct.load(.monotonic); + return (count) >= self.max_struct; } pub fn logError(self: *ThreadSyncContext, message: []const u8, err: anyerror) void {