From 746642aec3f9380f44bff3c5e8b79acfb105af6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Thu, 15 Jun 2023 14:24:43 +0200 Subject: [PATCH 1/3] eclass/tests: Add a benchmark for python-utils-r1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Initial timings on my system: ``` * Timing _python_set_impls real 1463 it/s user 1886 it/s ``` Signed-off-by: Michał Górny --- eclass/tests/python-utils-bench.sh | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100755 eclass/tests/python-utils-bench.sh diff --git a/eclass/tests/python-utils-bench.sh b/eclass/tests/python-utils-bench.sh new file mode 100755 index 0000000000000..7f27adef55090 --- /dev/null +++ b/eclass/tests/python-utils-bench.sh @@ -0,0 +1,53 @@ +#!/bin/bash +# Copyright 2023 Gentoo Authors +# Distributed under the terms of the GNU General Public License v2 + +EAPI=8 +source tests-common.sh || exit + +export LC_ALL=C + +ITERATIONS=10000 +RUNS=3 + +doit() { + local i + for (( i = 0; i < ITERATIONS; i++ )); do + "${@}" + done +} + +timeit() { + local real=() + local user=() + local x vr avg + + einfo "Timing ${*}" + for (( x = 0; x < RUNS; x++ )); do + while read tt tv; do + case ${tt} in + real) real+=( ${tv} );; + user) user+=( ${tv} );; + esac + done < <( ( time -p doit "${@}" ) 2>&1 ) + done + + [[ ${#real[@]} == ${RUNS} ]] || die "Did not get ${RUNS} real times" + [[ ${#user[@]} == ${RUNS} ]] || die "Did not get ${RUNS} user times" + + local xr avg + for x in real user; do + xr="${x}[*]" + avg=$(dc -S 3 -e "${ITERATIONS} ${RUNS} * ${!xr} + + / p") + + printf '%s %4.0f it/s\n' "${x}" "${avg}" + done +} + +PYTHON_COMPAT=( python3_{10..12} pypy3 ) + +inherit python-utils-r1 + +timeit _python_set_impls + +texit From 4d7804af0ce9077cbc53cda7318a3e388db8dbb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Thu, 15 Jun 2023 14:29:55 +0200 Subject: [PATCH 2/3] python-utils-r1.eclass: Use @a instead of declare check for array MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use bash 5's @a variable substitution instead of `declare -p` check to determine if `PYTHON_COMPAT` is an array. This avoids a subshell, and effectively makes the function roughly 3.5x faster: ``` * Timing _python_set_impls real 4854 it/s user 4862 it/s ``` In practice, this roughly reduces total sourcing time for `dev-python/*` from 57-58 s to 54 s. Signed-off-by: Michał Górny Closes: https://github.com/gentoo/gentoo/pull/31454 Signed-off-by: Michał Górny --- eclass/python-utils-r1.eclass | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/eclass/python-utils-r1.eclass b/eclass/python-utils-r1.eclass index 52e9e061d6bd3..7a1be381f5966 100644 --- a/eclass/python-utils-r1.eclass +++ b/eclass/python-utils-r1.eclass @@ -114,11 +114,18 @@ _python_verify_patterns() { _python_set_impls() { local i - if ! declare -p PYTHON_COMPAT &>/dev/null; then - die 'PYTHON_COMPAT not declared.' + # TODO: drop BASH_VERSINFO check when we require EAPI 8 + if [[ ${BASH_VERSINFO[0]} -ge 5 ]]; then + [[ ${PYTHON_COMPAT@a} == *a* ]] + else + [[ $(declare -p PYTHON_COMPAT) == "declare -a"* ]] fi - if [[ $(declare -p PYTHON_COMPAT) != "declare -a"* ]]; then - die 'PYTHON_COMPAT must be an array.' + if [[ ${?} -ne 0 ]]; then + if ! declare -p PYTHON_COMPAT &>/dev/null; then + die 'PYTHON_COMPAT not declared.' + else + die 'PYTHON_COMPAT must be an array.' + fi fi local obsolete=() From c8ac88e4cbea0e59e71422ef18d28909108827b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Thu, 15 Jun 2023 21:03:53 +0200 Subject: [PATCH 3/3] pypi.eclass: Do extglob reset unconditionally MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change _pypi_normalize_name() to reset extglob unconditionally. This function is called only in two contexts: - inside a subshell, therefore making it unnecessary to restore the original extglob value, - in global scope, via _pypi_set_globals, where we know that extglob is not supposed to be set. This makes the code simpler. Signed-off-by: Michał Górny Closes: https://github.com/gentoo/gentoo/pull/31465 Signed-off-by: Michał Górny --- eclass/pypi.eclass | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/eclass/pypi.eclass b/eclass/pypi.eclass index 8a842c450ebcd..594216a7fd966 100644 --- a/eclass/pypi.eclass +++ b/eclass/pypi.eclass @@ -70,14 +70,12 @@ _PYPI_ECLASS=1 # Internal normalization function, returns the result # via _PYPI_NORMALIZED_NAME variable. _pypi_normalize_name() { - local name=${1} - if shopt -p -q extglob; then - name=${name//+([._-])/_} - else - shopt -s extglob - name=${name//+([._-])/_} - shopt -u extglob - fi + # NB: it's fine to alter it unconditionally since this function is + # always called from a subshell or in global scope + # (via _pypi_set_globals) + shopt -s extglob + local name=${1//+([._-])/_} + shopt -u extglob _PYPI_NORMALIZED_NAME="${name,,}" }