Skip to content

CP-314128: Reduce unnecessary allocations and complexity across xapi - #7240

Open
GabrielBuica wants to merge 8 commits into
xapi-project:masterfrom
GabrielBuica:private/dbuica/CP-314128
Open

CP-314128: Reduce unnecessary allocations and complexity across xapi#7240
GabrielBuica wants to merge 8 commits into
xapi-project:masterfrom
GabrielBuica:private/dbuica/CP-314128

Conversation

@GabrielBuica

Copy link
Copy Markdown
Contributor
  • Replace O(n^2) list operations with Set/Hashtbl lookups in session auth and repository helpers
  • Move regex compilation out of per-line loops in network_utils and repository_helpers
  • Use array indexing instead of List.nth in rrdp_iostat
  • Avoid List.rev allocation on the event poll path in xapi_message
  • Remove redundant DB round-trips in xapi_vm_lifecycle
  • Fetch only needed fields instead of full VM record in xapi_vm_helpers

Each commit is independent and targets a different module.

In check_operation_error, metrics was fetched via Db.VM.get_metrics
when vmr.Db_actions.vM_metrics already held the same value. Similarly,
Db.VM.get_by_uuid looked up the VM ref we already have as a parameter.

Eliminating these two extra DB calls avoids unnecessary overhead on a
function that is invoked for every VM allowed-operations recomputation.

Signed-off-by: Gabriel Buica <danutgabriel.buica@citrix.com>
@GabrielBuica
GabrielBuica force-pushed the private/dbuica/CP-314128 branch from 5010842 to 404af73 Compare August 26, 2026 13:23
Comment thread ocaml/xapi/xapi_message.ml Outdated
Comment thread ocaml/xapi/repository_helpers.ml Outdated
Gabriel Buica added 7 commits August 27, 2026 15:59
get_since_for_events reversed the entire 512-entry in_memory_cache list
just to peek at the oldest (last) element. Replace with
Listext.List.last which traverses without allocating a reversed copy.

Signed-off-by: Gabriel Buica <danutgabriel.buica@citrix.com>
The 'replacing' pattern was recompiled via Re.Str.regexp on every line
of yum upgrade output. Move it to module level so it is compiled once.

Regex compilation is more expensive than matching, so recompiling on
every line is unnecessary repeated work.

Signed-off-by: Gabriel Buica <danutgabriel.buica@citrix.com>
host_of_vm called Db.VM.get_record to fetch the entire VM record but
only used scheduled_to_be_resident_on, resident_on, and power_state.
Replace with three individual field getters.

get_record deserialises every field of the VM object. Using targeted
getters avoids that overhead, which matters when iterating over all
VMs in a group for anti-affinity placement.

Signed-off-by: Gabriel Buica <danutgabriel.buica@citrix.com>
stats_get and stats_diff_get used List.nth for O(n) indexed access on
every field of every VDI during each sampling cycle. Convert the stats
lists to arrays once, then index with O(1) array access.

This function runs on every RRD sampling tick for every VDI, so
replacing O(n) list traversals with O(1) array lookups reduces
per-tick overhead.

Signed-off-by: Gabriel Buica <danutgabriel.buica@citrix.com>
parse_single_line in config_sriov compiled two regexes on every line of
the modprobe config: a driver-specific pattern and a whitespace splitter.
Move the driver pattern before the loop and the whitespace splitter to
module level so both are compiled once.

Re.Perl.compile_pat is more expensive than matching. Even though modprobe
config files are short, recompiling on every line is unnecessary.

Signed-off-by: Gabriel Buica <danutgabriel.buica@citrix.com>
get_updates_from_updateinfo and get_updates_from_repoquery both filtered
available packages against known updates using List.mem, giving O(A*U)
per-element membership tests. Build a Set from the updates list first,
reducing each lookup from O(U) to O(log U).

Signed-off-by: Gabriel Buica <danutgabriel.buica@citrix.com>
get_intersection used Listext.List.intersect (O(n*m) via nested
List.mem). Build a Hashtbl from the DB subject IDs and filter against
it for amortised O(n+m).

get_permissions used Listext.List.setify (O(n^2) via List.mem) to
deduplicate roles and permissions. Replace with List.sort_uniq which
is O(n log n) and also avoids the intermediate helper function.

Signed-off-by: Gabriel Buica <danutgabriel.buica@citrix.com>
@GabrielBuica
GabrielBuica force-pushed the private/dbuica/CP-314128 branch from 404af73 to 9196cc2 Compare August 27, 2026 15:00
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.

3 participants