Great Modules Refactor#274
Conversation
|
@aaumar25 |
614715c to
0968336
Compare
|
Thanks for the attempt. I want to point out the direction a little bit here as well.
pub fn init(c: Config) !MmcClient{}
EDIT: Wait, I just realized it is not possible to do that on current structure. However, my intent is to store everything on the I think, lets still make |
|
I transferred the history of |
|
There is still the following in the code: pub fn get() *MmcClient {
return &(current orelse std.debug.panic("Mmc_client module is not initialized", .{}));
}It is not ideal, but MmcClient commands are now registered and unregistered in However, I am looking into it to replace this. |
|
I only see briefly the changes you made. I saw in |
|
|
|
Then, is there a point to have |
|
It is not necessary, the intend was |
7f0518c to
4ebaf9f
Compare
ee383b4 to
d168fe3
Compare
|
Can you give me some way to review it? This PR is massive. It is so hard to review it. |
|
Also, try to squash small commits into one commit that still under the same scope, and put some thought on naming that commit title. Put some emphasize to the commit name of what is being changed or what is the impact of that commit. |
|
Okay, I will. Regarding the review, it looks extensive, but the majority of the changes are in |
| /// Module lifecycle hooks to load and unload modules and its module | ||
| /// commands set. | ||
| const ModuleSpec = struct { | ||
| init: *const fn (Config.ModuleConfig) anyerror!void, | ||
| deinit: *const fn () void, | ||
| commands: []const Command, | ||
| }; | ||
|
|
||
| /// Stub initializer for modules disabled at build time. | ||
| fn initModuleDisabled(_: Config.ModuleConfig) !void { | ||
| return error.ModuleDisabledAtBuildTime; | ||
| } | ||
|
|
||
| /// Stub deinitializer for modules disabled at build time. | ||
| fn deinitModuleDisabled() void {} | ||
|
|
||
| fn initMmcClient(module_config: Config.ModuleConfig) !void { | ||
| const cfg = switch (module_config) { | ||
| .mmc_client => |cfg| cfg, | ||
| else => unreachable, | ||
| }; | ||
| try mmc_client.init(cfg); | ||
| } | ||
|
|
||
| fn initMes07(module_config: Config.ModuleConfig) !void { | ||
| const cfg = switch (module_config) { | ||
| .mes07 => |cfg| cfg, | ||
| else => unreachable, | ||
| }; | ||
| try mes07.init(cfg); | ||
| } | ||
|
|
||
| const module_specs = std.EnumArray(Config.Module, ModuleSpec).init(.{ | ||
| .mmc_client = if (config.mmc_client) .{ | ||
| .init = initMmcClient, | ||
| .deinit = mmc_client.deinit, | ||
| .commands = mmc_client.module_commands[0..], | ||
| } else .{ | ||
| .init = initModuleDisabled, | ||
| .deinit = deinitModuleDisabled, | ||
| .commands = &.{}, | ||
| }, | ||
| .mes07 = if (config.mes07) .{ | ||
| .init = initMes07, | ||
| .deinit = mes07.deinit, | ||
| .commands = mes07.module_commands[0..], | ||
| } else .{ | ||
| .init = initModuleDisabled, | ||
| .deinit = deinitModuleDisabled, | ||
| .commands = &.{}, | ||
| }, | ||
| }); | ||
|
|
There was a problem hiding this comment.
These lines smells so bad. Whenever adding new modules or removing modules, requires developer to manually remove it.
There was a problem hiding this comment.
ㅜㅜㅜ, ok I will look into it.
Wait a little bit I am going to force push soon. I squashed quite a bit.
| const LoadedConfig = struct { | ||
| id: []const u8, | ||
| source_path: []const u8, | ||
| arena: *std.heap.ArenaAllocator, |
There was a problem hiding this comment.
what is this arena doing here?
| self.config.deinit(); | ||
| self.arena.deinit(); | ||
| std.heap.smp_allocator.destroy(self.arena); | ||
| std.heap.smp_allocator.free(self.id); | ||
| std.heap.smp_allocator.free(self.source_path); |
There was a problem hiding this comment.
In zig, whenever a function require an allocator, you should pass the allocator through the function or store the allocator as a field in the struct.
However, zig now moves to never store allocator as a field in the struct as you can see they are removing anything callled xxxManaged from their own standard library.
94b89e2 to
b419b93
Compare
There was a problem hiding this comment.
This PR has major problems with allocator usage. Remember, whenever a function requires an allocator, you have to pass an allocator.
Also, I am questioning the use of config id with string instead of just a number? Then, you do not need to use StringArrayHashMap. A slice with maximum number of config supported with nullable element is enough. This approach will remove a lot of dynamic allocation.
Dynamic allocation is expensive and have a risk of OOM. We want to avoid it as much as we can. Surely, trying to name unique config id with dynamic allocation can be avoided.
fix: #248