lib.attrsets.mergeAttrsList: small arithmetic performance improvements (#528677)

This commit is contained in:
Johannes Kirschbauer 2026-06-11 15:01:53 +00:00 committed by GitHub
commit 6c0202028d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1612,13 +1612,15 @@ rec {
binaryMerge = binaryMerge =
start: end: start: end:
# assert start < end; # Invariant # assert start < end; # Invariant
if end - start >= 2 then if end - start == 1 then
# If there's at least 2 elements, split the range in two, recurse on each part and merge the result # Base case - there will be exactly 1 element due to the invariant, in
# The invariant is satisfied because each half will have at least 1 element # which case we just return it directly
binaryMerge start (start + (end - start) / 2) // binaryMerge (start + (end - start) / 2) end elemAt list start
else else
# Otherwise there will be exactly 1 element due to the invariant, in which case we just return it directly # If there's at least 2 elements, split the range in two, recurse on each part and merge the result
elemAt list start; # Relies on floor for odd results
# The invariant is satisfied because each half will have at least 1 element
binaryMerge start ((start + end) / 2) // binaryMerge ((start + end) / 2) end;
in in
if list == [ ] then if list == [ ] then
# Calling binaryMerge as below would not satisfy its invariant # Calling binaryMerge as below would not satisfy its invariant