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