Skip to content

Great Modules Refactor#274

Open
solara404 wants to merge 12 commits into
mainfrom
great_modules_refactor_248
Open

Great Modules Refactor#274
solara404 wants to merge 12 commits into
mainfrom
great_modules_refactor_248

Conversation

@solara404
Copy link
Copy Markdown
Collaborator

fix: #248

@solara404 solara404 marked this pull request as draft March 31, 2026 07:48
@solara404
Copy link
Copy Markdown
Collaborator Author

@aaumar25
There is still issues that need to be worked on but i think this would give a good overview. I uploaded mmc_client.zig and MmcClient.zig. These files are pretty much the same, but mmc_client.zig allows comparison.

@solara404 solara404 force-pushed the great_modules_refactor_248 branch from 614715c to 0968336 Compare April 1, 2026 00:18
@aaumar25
Copy link
Copy Markdown
Member

aaumar25 commented Apr 1, 2026

Thanks for the attempt. I want to point out the direction a little bit here as well.

The init() function in each module will return its own type, for instance in MmcClient.zig:

pub fn init(c: Config) !MmcClient{}

In command.zig, we already have variable that track which module is already initialized, 'initialized_modules. The plan is to add modules we already load_configintoinitialized_modules, and create a function to deinitialize module in command.zig` by passing the module.

Right now, in loadConfig() in command.zig, we always deinitialized modules. Lets refactor the behavior to not deinitialize all modules that already initialized before. Instead, if the module that we are going to initialize is already initialized, we deinitialized what is already been initialized first.

From here, lets point out and resolve any issue that may appear with this approach.

EDIT: Wait, I just realized it is not possible to do that on current structure. However, my intent is to store everything on the command.zig. Lets figure out how to achieve that.

I think, lets still make init() function to return void similar as before

@solara404
Copy link
Copy Markdown
Collaborator Author

I transferred the history of mmc_client.zig to MmcClient.zig. I will look into the mentioned changes.

@solara404
Copy link
Copy Markdown
Collaborator Author

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 command.zig, so this situation should not occur. The commands which would trigger this would no longer be accessible when the module is deinitialized.

However, I am looking into it to replace this.

@aaumar25
Copy link
Copy Markdown
Member

aaumar25 commented Apr 3, 2026

I only see briefly the changes you made. I saw in MmcClient module it has create and init, while the init is only calling create. Is there a specific reason behind this? As a side note, the errdefer line in the init() of MmcClient is no op.

@solara404
Copy link
Copy Markdown
Collaborator Author

init() was just made for ModuleSpec. Oh yes the errordefer is pointless here, I will remove that.

@aaumar25
Copy link
Copy Markdown
Member

aaumar25 commented Apr 3, 2026

Then, is there a point to have create() function there?

@solara404
Copy link
Copy Markdown
Collaborator Author

It is not necessary, the intend was create() creates MmcClient and init() puts it into global current variable.
I will put everything into init() to remove the extra step.

@solara404 solara404 force-pushed the great_modules_refactor_248 branch from 7f0518c to 4ebaf9f Compare April 7, 2026 01:37
@solara404 solara404 marked this pull request as ready for review April 17, 2026 04:10
@solara404 solara404 force-pushed the great_modules_refactor_248 branch from ee383b4 to d168fe3 Compare April 17, 2026 04:41
@solara404 solara404 requested a review from aaumar25 April 21, 2026 00:03
@aaumar25
Copy link
Copy Markdown
Member

Can you give me some way to review it? This PR is massive. It is so hard to review it.

@aaumar25
Copy link
Copy Markdown
Member

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.

@solara404
Copy link
Copy Markdown
Collaborator Author

Okay, I will. Regarding the review, it looks extensive, but the majority of the changes are in command.zig.

Comment thread src/command.zig Outdated
Comment on lines +22 to +74
/// 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 = &.{},
},
});

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines smells so bad. Whenever adding new modules or removing modules, requires developer to manually remove it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅜㅜㅜ, ok I will look into it.
Wait a little bit I am going to force push soon. I squashed quite a bit.

Comment thread src/command.zig
const LoadedConfig = struct {
id: []const u8,
source_path: []const u8,
arena: *std.heap.ArenaAllocator,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this arena doing here?

Comment thread src/command.zig
Comment on lines +259 to +263
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);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@solara404 solara404 force-pushed the great_modules_refactor_248 branch from 94b89e2 to b419b93 Compare April 21, 2026 07:40
Copy link
Copy Markdown
Member

@aaumar25 aaumar25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Great modules refactor

2 participants