On Mon, Sep 11, 2023 at 09:55:53AM +0200, Marc Espie wrote:
> Not to pkgconf levels, but still way faster than what we had
> 
> Updated patch from what I've shown to people, turns out the second grep
> wasn't quite working.
> 
> This does cache the set_variables_from_env shennanigans, speeding up large
> processing of recursive files by a large factor (since we keep a cache
> of relevant env variables, and don't bother setting the same value twice)
> 
> 
> The optimisation in PkgConfig.pm is sligtly less powerful: we got a marker
> for variable expansions straight up when we parse a pkgconfig file, before
> even splitting into lists, so instead of "raw" lists, 
> tag them as NoExpand/ToExpand classes, so  that we can forego variable
> expansion altogether.
> 
> Please test, this appears to pass regress, and I've just put this into
> a partial bulk.

Oops, as gkoehler noticed, I sent out the old patch.
Here's the actual fixed one:

Index: pkg-config
===================================================================
RCS file: /cvs/src/usr.bin/pkg-config/pkg-config,v
retrieving revision 1.96
diff -u -p -r1.96 pkg-config
--- pkg-config  8 Jun 2023 08:55:27 -0000       1.96
+++ pkg-config  11 Sep 2023 07:50:57 -0000
@@ -279,6 +279,26 @@ if ($mode{cflags} || $mode{libs} || $mod
 exit $rc;
 
 ###########################################################################
+sub set_variables_from_env($file)
+{
+           state (%done, @l);
+
+           if (!defined $done{$file}) {
+                   my $pkg = $file;
+
+                   $pkg =~ s/(^.*\/)?(.*?)\.pc$/$2/g;
+                   $pkg = uc($pkg);
+                   if (!@l) {
+                           @l = grep {/PKG_CONFIG_/} keys %ENV;
+                   }
+                   for my $k (@l) {
+                           next unless $k =~ m/PKG_CONFIG_${pkg}_(\w+)/;
+                           $variables->{lc($1)} = $ENV{$k};
+                   }
+                   $done{$file} = 1;
+           }
+
+}
 
 sub handle_config($p, $op, $v, $list)
 {
@@ -300,22 +320,7 @@ sub handle_config($p, $op, $v, $list)
        }
 
        my $get_props = sub($property) {
-           my $pkg;
-
-           # See if there's anything in the environment that we need to
-           # take into account.
-           ($pkg = $p) =~ s/(^.*\/)?(.*?)\.pc$/$2/g;
-           $pkg = uc($pkg);
-
-           if (grep {/PKG_CONFIG_${pkg}.*/} keys %ENV) {
-                   # Now that we know we have something to look for, do
-                   # the inefficient iteration.
-                   while (my ($k, $v) = each %ENV) {
-                           if ($k =~ /^PKG_CONFIG_${pkg}_(\w+)/) {
-                                   $variables->{lc($1)} = $v;
-                           }
-                   }
-           }
+           set_variables_from_env($p);
 
            my $deps = $cfg->get_property($property, $variables);
            return unless defined $deps;
Index: OpenBSD/PkgConfig.pm
===================================================================
RCS file: /cvs/src/usr.bin/pkg-config/OpenBSD/PkgConfig.pm,v
retrieving revision 1.10
diff -u -p -r1.10 PkgConfig.pm
--- OpenBSD/PkgConfig.pm        8 Jun 2023 08:55:27 -0000       1.10
+++ OpenBSD/PkgConfig.pm        11 Sep 2023 07:50:57 -0000
@@ -16,6 +16,7 @@
 
 use v5.36;
 
+
 # interface to the *.pc file format of pkg-config.
 package OpenBSD::PkgConfig;
 
@@ -72,10 +73,14 @@ sub add_variable($self, $name, $value)
 
 sub parse_value($self, $name, $value)
 {
+       my $class = "OpenBSD::PkgConfig::NoExpand";
+       if ($value =~ m/\$\{.*\}/) {
+               $class = "OpenBSD::PkgConfig::ToExpand";
+       }
        if (defined $parse->{$name}) {
-               return $parse->{$name}($value);
+               return bless $parse->{$name}($value), $class;
        } else {
-               return [split /(?<!\\)\s+/o, $value];
+               return bless [split /(?<!\\)\s+/o, $value], $class;
        }
 }
 
@@ -89,7 +94,7 @@ sub add_property($self, $name, $value)
        if (defined $value) {
                $v = $self->parse_value($name, $value);
        } else {
-               $v = [];
+               $v = bless [], "OpenBSD::PkgConfig::NoExpand";
        }
        $self->{properties}{$name} = $v;
 }
@@ -121,8 +126,9 @@ sub read_fh($class, $fh, $name = '')
                }
        }
        if (defined $cfg->{properties}{Libs}) {
-               $cfg->{properties}{Libs} =
-                   $cfg->compress_list($cfg->{properties}{Libs});
+               $cfg->{properties}{Libs} = bless
+                   $cfg->compress_list($cfg->{properties}{Libs}),
+                   ref($cfg->{properties}{Libs});
        }
        return $cfg;
 }
@@ -220,6 +226,9 @@ sub get_property($self, $k, $extra = {})
        if (!defined $l) {
                return undef;
        }
+       if ($l->noexpand) {
+               return [@$l];
+       }
        my $r = [];
        for my $v (@$l) {
                my $w = $self->expanded($v, $extra);
@@ -263,4 +272,17 @@ sub add_bases($self, $extra)
        }
 }
 
+package OpenBSD::PkgConfig::NoExpand;
+our @ISA = qw(OpenBSD::PkgConfig);
+sub noexpand($)
+{
+       1
+}
+
+package OpenBSD::PkgConfig::ToExpand;
+our @ISA = qw(OpenBSD::PkgConfig);
+sub noexpand($)
+{
+       0
+}
 1;

Reply via email to