Reduce memory footprint of module metadata Obj instances#21230
Open
bcoles wants to merge 2 commits intorapid7:masterfrom
Open
Reduce memory footprint of module metadata Obj instances#21230bcoles wants to merge 2 commits intorapid7:masterfrom
bcoles wants to merge 2 commits intorapid7:masterfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR reduces memory overhead when instantiating Msf::Modules::Metadata::Obj entries from the on-disk JSON metadata cache, which is loaded early during framework startup and multiplied across thousands of modules.
Changes:
- Interns highly-duplicated strings (e.g., type/platform/arch/author entries) to reduce duplicate heap allocations.
- Reuses shared frozen empty array/hash constants for empty metadata fields.
- Adds a class-level cache to reuse parsed
Msf::Module::PlatformListobjects for repeated platform strings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Reduces per-instance memory overhead in
Msf::Modules::Metadata::Objwhen loading the ~6,600 modules from the JSON metadata cache. The metadata cache is the first thing loaded on startup, so every allocation here is multiplied across every module entry.All changes are internal to
init_from_hashand the class-level helpers. No public API changes.Three techniques are applied:
1. String deduplication via
String#-@(fstring interning)Fields like
type,platform,arch, andauthorare highly duplicated across modules (e.g., only 7 uniquetypevalues across 6,600+ entries).String#-@returns a frozen, deduplicated copy from Ruby's internal string table, so identical values share a single object.2. Shared frozen empty containers
Many modules have empty
aliases,references,notes, ortargets. Previously each got its own[]or{}allocation. These now shareEMPTY_ARRAYandEMPTY_HASHfrozen constants.3. PlatformList caching
PlatformList.transformwas called once per module (~6,600 times) despite only ~100 unique platform strings existing. A class-level cache (cached_platform_list) returns the samePlatformListobject for identical platform strings, moving the privateparse_platform_listinstance method to a shared class method.