Skip to content

Optimize modules dict iteration with early return when empty#4002

Open
enjoy-binbin wants to merge 1 commit into
valkey-io:unstablefrom
enjoy-binbin:minor_change
Open

Optimize modules dict iteration with early return when empty#4002
enjoy-binbin wants to merge 1 commit into
valkey-io:unstablefrom
enjoy-binbin:minor_change

Conversation

@enjoy-binbin

@enjoy-binbin enjoy-binbin commented Jun 18, 2026

Copy link
Copy Markdown
Member

Avoid dictIterator allocation overhead by checking dictSize(modules) == 0
in all functions that iterate over the modules dictionary, returning early
with the appropriate default value.

@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR adds early-exit checks across the modules subsystem: when the modules dictionary is empty, 12 functions now return immediately with appropriate empty results (NULL, 0, C_OK, or input value), avoiding iterator creation and per-module loops in config loading, type lookup, capability verification, persistence, info collection, and lifecycle operations.

Changes

Early-exit optimization across modules subsystem for empty module dictionary

Layer / File(s) Summary
Config module-loading early-exit
src/config.c
rewriteConfigLoadmoduleOption() checks if modules is empty, marks loadmodule option processed, and returns immediately, skipping iterator creation.
Module type lookup early-exits
src/module.c
moduleTypeLookupModuleByNameInternal() and moduleTypeLookupModuleByID() return NULL immediately when modules is empty, avoiding cache traversal and iterator setup.
Module capability verification early-exits
src/module.c
moduleAllDatatypesHandleErrors(), moduleAllModulesHandleReplAsyncLoad(), and moduleVerifyAllAllowAtomicSlotMigrationOrReply() return their appropriate empty-case values immediately when modules is empty.
Module persistence and info collection early-exits
src/module.c
rdbSaveModulesAux(), modulesCollectInfo(), and genModulesInfoString() return immediately with appropriate empty results when modules is empty, skipping iteration loops.
Module lifecycle and reply early-exits
src/module.c
moduleUnloadAllModules() returns immediately, addReplyLoadedModules() sends empty array and returns, and moduleDefragGlobals() returns immediately when modules is empty.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 76.92% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description is directly related to the changeset, explaining the optimization approach of checking dictSize(modules) == 0 and returning early across modules iteration.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title accurately describes the main optimization implemented throughout the pull request: adding early-return checks when the modules dictionary is empty to avoid iterator creation and loops.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 68.75000% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.73%. Comparing base (436dcae) to head (b9230f9).
⚠️ Report is 11 commits behind head on unstable.

Files with missing lines Patch % Lines
src/module.c 76.92% 3 Missing ⚠️
src/config.c 33.33% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #4002      +/-   ##
============================================
- Coverage     76.75%   76.73%   -0.02%     
============================================
  Files           162      162              
  Lines         81017    81035      +18     
============================================
- Hits          62187    62185       -2     
- Misses        18830    18850      +20     
Files with missing lines Coverage Δ
src/config.c 78.84% <33.33%> (-0.09%) ⬇️
src/module.c 25.44% <76.92%> (+0.12%) ⬆️

... and 22 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Avoid dictIterator allocation overhead by checking dictSize(modules) == 0
in all functions that iterate over the modules dictionary, returning early
with the appropriate default value.

Signed-off-by: Binbin <binloveplay1314@qq.com>
@enjoy-binbin enjoy-binbin changed the title Optimize addReplyLoadedModules to early return when modules is empty Optimize modules dict iteration with early return when empty Jun 23, 2026
@enjoy-binbin enjoy-binbin requested a review from zuiderkwast June 23, 2026 05:38
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.

1 participant