feat(device): optimize parse_dtb with a generic hash map#269
Merged
Conversation
f4fdea2 to
7506cba
Compare
parse_dtb() matched every device tree node against the registered drivers with a nested loop: for each of the N nodes it walked the whole list of M drivers looking for a compatible string, once per initcall level -- an O(N * M) scan. Introduce a generic string-keyed hash map (lib/hashmap.c, include/hashmap.h) and use it to index the drivers by their compatible string. The map is built once in O(M); the device tree is then scanned a single time and each node is matched in O(1), bringing the parsing down to O(N + M). The hash map is a reusable container (open addressing, automatic growth, copied keys) that other subsystems can rely on. Its "round up to a power of two" helper is exposed as a generic round_up_pow2() in the new include/math.h. Matched nodes are queued per initcall level during the single scan and only initialized afterwards, core drivers before postcore ones and in device tree order within each level, so the initialization order is unchanged. A single scratch dev_t is reused while probing nodes instead of being allocated and freed for every visited node. Closes #85
7506cba to
cbc6b53
Compare
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.
Closes #85
Problem
parse_dtb()matched every device tree node against the registered drivers with a nested loop: for each of the N nodes it walked the whole list of M drivers looking for a matchingcompatiblestring, once per initcall level — an O(N·M) scan.Change
lib/hashmap.c,include/hashmap.h): open addressing with linear probing, automatic growth, copied/owned keys,void *values. A reusable container other subsystems can rely on.parse_dtb()to index the drivers by theircompatiblestring. The map is built once in O(M); the device tree is then scanned a single time and each node is matched in O(1), bringing the parsing down to O(N + M).dev_tis reused while probing nodes instead of being allocated and freed for every visited node.Behaviour
Initialization order is identical. The only theoretical difference is a
compatibleregistered at both CORE and POSTCORE: the old code initialized such a node twice (a latent double-init), the new code once at core level. This cannot happen with the current drivers — the CORE and POSTCORE compatible sets are disjoint.Verification
virt64_defconfig);device.oandhashmap.ocompile with no new warnings;clang-formatreports no diff on the touched files.lib/hashmap.c: 500 inserts triggering several rehashes, exact-pointer lookups, overwrite semantics, copied-key ownership.