• [gentoo-dev] [PATCH 7/7] pypi.eclass: Avoid subshell for extglob settin

    From =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?@21:1/5 to All on Tue Jun 13 08:50:01 2023
    Suggested by Eli Schwartz. This gives roughly 5260 ops / s, over 550%
    speedup.

    The complete patch series therefore increases the speed from roughly
    326 ops / s to 5260 ops / s, making the common case 16 times faster.

    Closes: https://bugs.gentoo.org/908411
    Closes: https://github.com/gentoo/gentoo/pull/31404
    Signed-off-by: Michał Górny <mgorny@gentoo.org>
    ---
    eclass/pypi.eclass | 9 ++++++---
    1 file changed, 6 insertions(+), 3 deletions(-)

    diff --git a/eclass/pypi.eclass b/eclass/pypi.eclass
    index eade3d955ab5..618fde5e3de8 100644
    --- a/eclass/pypi.eclass
    +++ b/eclass/pypi.eclass
    @@ -71,10 +71,13 @@ _PYPI_ECLASS=1
    # via _PYPI_NORMALIZED_NAME variable.
    _pypi_normalize_name() {
    local name=${1}
    - local shopt_save=$(shopt -p extglob)
    - shopt -s extglob
    + local prev_extglob=-s
    + if ! shopt -p extglob >/dev/null; then
    + prev_extglob=-u
    + shopt -s extglob
    + fi
    name=${name//+([._-])/_}
    - ${shopt_save}
    + shopt "${prev_extglob}" extglob
    _PYPI_NORMALIZED_NAME="${name,,}"
    }

    --
    2.41.0

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ulrich Mueller@21:1/5 to All on Tue Jun 13 11:10:01 2023
    On Tue, 13 Jun 2023, Michał Górny wrote:

    _pypi_normalize_name() {
    local name=${1}
    - local shopt_save=$(shopt -p extglob)
    - shopt -s extglob
    + local prev_extglob=-s
    + if ! shopt -p extglob >/dev/null; then
    + prev_extglob=-u
    + shopt -s extglob
    + fi
    name=${name//+([._-])/_}
    - ${shopt_save}
    + shopt "${prev_extglob}" extglob
    _PYPI_NORMALIZED_NAME="${name,,}"
    }

    In principle you could also do something like this:

    if shopt -pq extglob; then
    name=${name//+([._-])/_}
    else
    shopt -s extglob
    name=${name//+([._-])/_}
    shopt -u extglob
    fi

    It duplicates one line of code, but saves a variable and IMHO the code
    would be easier to understand.

    Also, with the -q option no output redirection is needed.

    Ulrich

    --=-=-Content-Type: application/pgp-signature; name="signature.asc"

    -----BEGIN PGP SIGNATURE-----

    iQFDBAEBCAAtFiEEtDnZ1O9xIP68rzDbUYgzUIhBXi4FAmSIMe0PHHVsbUBnZW50 b28ub3JnAAoJEFGIM1CIQV4uPbQIALwHoHoDWeWgDj2nlYXT1Ytvcyo5toaVe9Wg 7R9sTq3UTMBIMdKXUQwwePIRI8zJE/xrUcDNUq0x8u5jNXg/WfH+dGoxo1TGNMiM lWicXPhvdTK+KM7cyVvtzkBVaJZDrW5HzAktCHGj3t7zDGFh7lN/Aih9CeJWD62s FdGnXuik8T19spcfJ5lxhCfHDCG/W6S9ip9T4LhaEC6VYybw0aNMFnR6akJcTI+h H/IyL824imc84wetCiw7euZFeduy2adeLF4g+ibJ87vRUVw8w6+Ct+bXtZXfKJJC ia6f5v18vXoImULnSZjl1H+pmkJ8iWKEu8Cdsz7YaRi7bHLZbP8=y9l0
    -----END PGP SIGNATURE-----

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Micha=C5=82_G=C3=B3rny?=@21:1/5 to Ulrich Mueller on Tue Jun 13 15:40:01 2023
    On Tue, 2023-06-13 at 11:07 +0200, Ulrich Mueller wrote:
    On Tue, 13 Jun 2023, Michał Górny wrote:

     _pypi_normalize_name() {
      local name=${1}
    - local shopt_save=$(shopt -p extglob)
    - shopt -s extglob
    + local prev_extglob=-s
    + if ! shopt -p extglob >/dev/null; then
    + prev_extglob=-u
    + shopt -s extglob
    + fi
      name=${name//+([._-])/_}
    - ${shopt_save}
    + shopt "${prev_extglob}" extglob
      _PYPI_NORMALIZED_NAME="${name,,}"
     }

    In principle you could also do something like this:

    if shopt -pq extglob; then
    name=${name//+([._-])/_}
    else
    shopt -s extglob
    name=${name//+([._-])/_}
    shopt -u extglob
    fi

    It duplicates one line of code, but saves a variable and IMHO the code
    would be easier to understand.


    I was thinking about this but I really dislike repeating the logic
    twice. In my opinion, having such block would be confusing: why are
    there two logics for extglob on and off? Why are both the same? Is
    this some mistake?

    Even though this is unlikely, someone could actually end up creating
    a missync there, and things would go downhill from there.

    -q is a good idea though.

    Ideally, we'd avoid extglob at all but I can't think of another pure
    bash way of doing this.

    --
    Best regards,
    Michał Górny

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)