Skip to content

Optimize inventory.Diff with map lookup - #1720

Merged
stefanprodan merged 1 commit into
fluxcd:mainfrom
manoj-1407:perf/inventory-diff-map-lookup
Aug 1, 2026
Merged

Optimize inventory.Diff with map lookup#1720
stefanprodan merged 1 commit into
fluxcd:mainfrom
manoj-1407:perf/inventory-diff-map-lookup

Conversation

@manoj-1407

Copy link
Copy Markdown
Contributor

What does this change do?

inventory.Diff used a versionOf closure that performed a linear
scan through all inventory entries on every call — once per item in
the diff list. For an inventory with n entries and a diff list of
m items, this is O(n*m).

This change builds a version map once upfront (O(n)), then uses O(1)
map lookups per diff item, reducing overall complexity to O(n+m).

On clusters with large inventories this avoids redundant linear scans
on every reconciliation cycle where pruning is needed.

Testing

Existing Test_Inventory/diff_objects_in_inventory passes without
modification — behavior is identical, only the lookup strategy changed.

Replace the O(n) versionOf closure with a map built once upfront,
reducing complexity from O(n*m) to O(n+m). On large inventories
this avoids redundant linear scans on every prune cycle.

Signed-off-by: manoj-1407 <manojkumar148700@gmail.com>
Assisted-by: Claude/claude-sonnet-4-6

@stefanprodan stefanprodan left a comment

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.

LGTM

Thanks @manoj-1407

PS. Please open a PR with this optimization in flux-operator.

@stefanprodan
stefanprodan merged commit 186fb3b into fluxcd:main Aug 1, 2026
7 checks passed
@manoj-1407
manoj-1407 deleted the perf/inventory-diff-map-lookup branch August 1, 2026 11:42
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.

2 participants