mirror of
https://github.com/NixOS/nixpkgs.git
synced 2026-07-06 17:13:24 -05:00
lib.attrsets.mergeAttrsList: small arithmetic performance improvements (#528677)
This commit is contained in:
commit
6c0202028d
1 changed files with 7 additions and 5 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue