abuild: add default_pyc helper

Python by default pre-compiles cache files in __pycache__ directories,
which we currently happily install along in python packages.

Theses .pyc files are rather big and the time/space tradeoff could be
left to users if we just split these out to a -pyc subpackage.

With this default_pyc helper, one can add $pkgname-pyc to their
package's subpackages and it will automatically split off the pyc files
in a package that will be automatically installed if the virtual 'pyc'
package is installed.
Note that this does not work so easily if there already were python
subpackages, the function could be adjusted to strip off the last dash
if required but that seems rather rare.

Random data, sizes:
 - python3: currently 47MiB, split into 23M (main package) / 24M (pyc)
 - py3-markdown: currently 700KiB, 368K (main) / 288K (pyc)

Random benchmark, with python3-pyc:
hyperfine --warmup 5 -m 100 \
    "python3 -c 'import time; print(time.strftime(\"%T\"))'"
  Time (mean ± σ):      24.5 ms ±   2.5 ms    [User: 18.4 ms, System: 6.0 ms]
  Range (min … max):    19.4 ms …  28.9 ms    148 runs

without python3-pyc (same as user without root permissions, root would
generate files on first root, for reference this command generates 184KB
of pyc files):
hyperfine --warmup 5 -m 100 \
    -p 'rm -rf /usr/lib/python3.10/__pycache__ /usr/lib/python3.10/encodings/__pycache__' \
    "python3 -c 'import time; print(time.strftime(\"%T\"))'"
  Time (mean ± σ):      53.7 ms ±   4.3 ms    [User: 39.3 ms, System: 14.3 ms]
  Range (min … max):    47.0 ms …  65.6 ms    100 runs

Link: https://gitlab.alpinelinux.org/alpine/aports/-/issues/11906
Suggested-by: Alex Xu (Hello71) <alex_y_xu@yahoo.ca>
This commit is contained in:
Dominique Martinet 2023-04-18 10:43:20 +00:00 committed by Natanael Copa
parent bb8b1f8dd0
commit dd07911cbf
1 changed files with 13 additions and 0 deletions

View File

@ -2141,6 +2141,19 @@ fishcomp() {
default_fishcomp
}
default_pyc() {
pkgdesc="Precompiled Python bytecode for ${subpkgname%-pyc}"
install_if="${subpkgname%-pyc}=$pkgver-r$pkgrel pyc"
cd "$pkgdir" || return 0
local IFS=$'\n'
amove $(find usr/lib/python* -type d -name __pycache__)
}
pyc() {
default_pyc
}
is_function() {
type "$1" 2>&1 | head -n 1 | grep -E -q "is a (shell )?function"
}