Merge pull request #269637 from adisbladis/lib-meta-avoid-alloc-platformmatch
lib.meta: Avoid attrset allocation in platformMatch
This commit is contained in:
commit
627af21e87
32
lib/meta.nix
32
lib/meta.nix
@ -3,6 +3,11 @@
|
|||||||
|
|
||||||
{ lib }:
|
{ lib }:
|
||||||
|
|
||||||
|
let
|
||||||
|
inherit (lib) matchAttrs any all;
|
||||||
|
inherit (builtins) isString;
|
||||||
|
|
||||||
|
in
|
||||||
rec {
|
rec {
|
||||||
|
|
||||||
|
|
||||||
@ -83,14 +88,21 @@ rec {
|
|||||||
We can inject these into a pattern for the whole of a structured platform,
|
We can inject these into a pattern for the whole of a structured platform,
|
||||||
and then match that.
|
and then match that.
|
||||||
*/
|
*/
|
||||||
platformMatch = platform: elem: let
|
platformMatch = platform: elem: (
|
||||||
pattern =
|
# Check with simple string comparison if elem was a string.
|
||||||
if builtins.isString elem
|
#
|
||||||
then { system = elem; }
|
# The majority of comparisons done with this function will be against meta.platforms
|
||||||
else if elem?parsed
|
# which contains a simple platform string.
|
||||||
then elem
|
#
|
||||||
else { parsed = elem; };
|
# Avoiding an attrset allocation results in significant performance gains (~2-30) across the board in OfBorg
|
||||||
in lib.matchAttrs pattern platform;
|
# because this is a hot path for nixpkgs.
|
||||||
|
if isString elem then platform ? system && elem == platform.system
|
||||||
|
else matchAttrs (
|
||||||
|
# Normalize platform attrset.
|
||||||
|
if elem ? parsed then elem
|
||||||
|
else { parsed = elem; }
|
||||||
|
) platform
|
||||||
|
);
|
||||||
|
|
||||||
/* Check if a package is available on a given platform.
|
/* Check if a package is available on a given platform.
|
||||||
|
|
||||||
@ -102,8 +114,8 @@ rec {
|
|||||||
2. None of `meta.badPlatforms` pattern matches the given platform.
|
2. None of `meta.badPlatforms` pattern matches the given platform.
|
||||||
*/
|
*/
|
||||||
availableOn = platform: pkg:
|
availableOn = platform: pkg:
|
||||||
((!pkg?meta.platforms) || lib.any (platformMatch platform) pkg.meta.platforms) &&
|
((!pkg?meta.platforms) || any (platformMatch platform) pkg.meta.platforms) &&
|
||||||
lib.all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []);
|
all (elem: !platformMatch platform elem) (pkg.meta.badPlatforms or []);
|
||||||
|
|
||||||
/* Get the corresponding attribute in lib.licenses
|
/* Get the corresponding attribute in lib.licenses
|
||||||
from the SPDX ID.
|
from the SPDX ID.
|
||||||
|
|||||||
@ -1948,4 +1948,24 @@ runTests {
|
|||||||
testGetExe'FailureSecondArg = testingThrow (
|
testGetExe'FailureSecondArg = testingThrow (
|
||||||
getExe' { type = "derivation"; } "dir/executable"
|
getExe' { type = "derivation"; } "dir/executable"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
testPlatformMatch = {
|
||||||
|
expr = meta.platformMatch { system = "x86_64-linux"; } "x86_64-linux";
|
||||||
|
expected = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
testPlatformMatchAttrs = {
|
||||||
|
expr = meta.platformMatch (systems.elaborate "x86_64-linux") (systems.elaborate "x86_64-linux").parsed;
|
||||||
|
expected = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
testPlatformMatchNoMatch = {
|
||||||
|
expr = meta.platformMatch { system = "x86_64-darwin"; } "x86_64-linux";
|
||||||
|
expected = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
testPlatformMatchMissingSystem = {
|
||||||
|
expr = meta.platformMatch { } "x86_64-linux";
|
||||||
|
expected = false;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user