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 =
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue