diff mbox series

[bug#47251,core-updates] gnu: python: Enable optimizations.

Message ID 20210605174308.57c4a324@tachikoma.lepiller.eu
State Accepted
Headers show
Series [bug#47251,core-updates] gnu: python: Enable optimizations. | expand

Checks

Context Check Description
cbaines/applying patch fail View Laminar job
cbaines/issue success View issue

Commit Message

Julien Lepiller June 5, 2021, 3:43 p.m. UTC
Le Thu, 03 Jun 2021 11:48:40 +0200,
Ludovic Courtès <ludo@gnu.org> a écrit :

> Hi,
> 
> Julien Lepiller <julien@lepiller.eu> skribis:
> 
> > But I noticed another file gained a lot of weight:
> > lib/python3.9/config-3.9-x86_64-linux-gnu/libpython3.9.a weighs
> > 39MB, whereas on master (for 3.8.2) it weighs 6MB. Here is what
> > size has to say:  
> 
> Yeah, the .o files in there likely contain serialized GIMPLE, as a
> result of compiling with ‘-flto’.
> 
> How about removing *.a altogether in a separate patch?  Or moving it
> to a “static” output?  This would be consistent with what’s done in
> other packages.
> 
> Thanks,
> Ludo’.

Right, this is a good idea. I wrote the attached patches to: enable
optimizations, prevent building and installing the static library and
to install idle to a separate output, as it is not required in general.

With the patch from https://issues.guix.gnu.org/47214, python 3.9 on
coreupdates is ~1.7x faster than python 3.8 on master, and is ~25MB
smaller than 3.8 on master (I haven't measured the speedup for python2,
and it is ~10MB smaller with these changes than on master).

Comments

Ludovic Courtès June 6, 2021, 12:34 p.m. UTC | #1
Hi,

Julien Lepiller <julien@lepiller.eu> skribis:

> Right, this is a good idea. I wrote the attached patches to: enable
> optimizations, prevent building and installing the static library and
> to install idle to a separate output, as it is not required in general.
>
> With the patch from https://issues.guix.gnu.org/47214, python 3.9 on
> coreupdates is ~1.7x faster than python 3.8 on master, and is ~25MB
> smaller than 3.8 on master (I haven't measured the speedup for python2,
> and it is ~10MB smaller with these changes than on master).

Yay!

> From 0c45d6e82a5f4d140840aef6b0de321122b6c4df Mon Sep 17 00:00:00 2001
> From: Julien Lepiller <julien@lepiller.eu>
> Date: Thu, 18 Mar 2021 23:41:40 +0100
> Subject: [PATCH 2/4] gnu: python: Enable optimizations.
>
> As a result, python execution time of scripts is decreased by 6 to 40%.
>
> gnu/packages/python (python-2.7)[arguments]: Add configure and make flags to
> optimize the resulting python.

[...]

> +             "CFLAGS=-fno-semantic-interposition"
>               (string-append "LDFLAGS=-Wl,-rpath="
> -                            (assoc-ref %outputs "out") "/lib"))
> +                            (assoc-ref %outputs "out") "/lib"
> +                            " -fno-semantic-interposition"))

Could you add a comment for ‘-fno-semantic-interposition’?  Also, please
make sure that setting CFLAGS does not override useful defaults (for
Autoconf-based build systems, CFLAGS defaults to ‘-O2 -g’ and overriding
it means you lost both -O2 and -g; if we lose ‘-g’, ‘--with-debug-info’
won’t have any effect).

> From 1faeb01becd558f0f4eec16ff32df9dbf1adda7f Mon Sep 17 00:00:00 2001
> From: Julien Lepiller <julien@lepiller.eu>
> Date: Thu, 3 Jun 2021 22:10:09 +0200
> Subject: [PATCH 3/4] gnu: python: Do not install static libraries.
>
> This is something Fedora already does since 2010, so it should be safe,
> and saves a lot of space.
>
> * gnu/packages/patches/python-2.7-no-static-lib.patch: New file.
> * gnu/packages/patches/python-3-no-static-lib.patch: New file.
> * gnu/local.mk (dist_patch_DATA): Add them.
> * gnu/packages/python.scm (python-2.7, python-3): Use them.

LGTM.

> From c650ac48be47b373c6c8c0b3495f30d9fe21e68b Mon Sep 17 00:00:00 2001
> From: Julien Lepiller <julien@lepiller.eu>
> Date: Thu, 3 Jun 2021 22:14:14 +0200
> Subject: [PATCH 4/4] gnu: python: Move idle to a separate output.
>
> idle is a programming environment that is not useful for building
> packages, but it is still useful for end users. This saves 5MB in the
> default output.
>
> * gnu/packages/python.scm (python-2.7)[outputs]: Add `idle'.
> [arguments]: Move idle files in the new output.
> (wrap-python): Do not wrap idle since it is in a different output.

Good idea, LGTM.

Thank you!

Ludo’.
Julien Lepiller June 11, 2021, 1:03 a.m. UTC | #2
Le Sun, 06 Jun 2021 14:34:31 +0200,
Ludovic Courtès <ludo@gnu.org> a écrit :

> 
> > +             "CFLAGS=-fno-semantic-interposition"
> >               (string-append "LDFLAGS=-Wl,-rpath="
> > -                            (assoc-ref %outputs "out") "/lib"))
> > +                            (assoc-ref %outputs "out") "/lib"
> > +                            " -fno-semantic-interposition"))  
> 
> Could you add a comment for ‘-fno-semantic-interposition’?  Also,
> please make sure that setting CFLAGS does not override useful
> defaults (for Autoconf-based build systems, CFLAGS defaults to ‘-O2
> -g’ and overriding it means you lost both -O2 and -g; if we lose
> ‘-g’, ‘--with-debug-info’ won’t have any effect).
> 

With the patch, python still builds with -g -O3.

Pushed as 99d450b085b7bf44d669093f6c9964391ce1b451 to
e4aa346d57f61956626032d0d1dfca6b3c96e06a to core-updates, thanks!
diff mbox series

Patch

From c650ac48be47b373c6c8c0b3495f30d9fe21e68b Mon Sep 17 00:00:00 2001
From: Julien Lepiller <julien@lepiller.eu>
Date: Thu, 3 Jun 2021 22:14:14 +0200
Subject: [PATCH 4/4] gnu: python: Move idle to a separate output.

idle is a programming environment that is not useful for building
packages, but it is still useful for end users. This saves 5MB in the
default output.

* gnu/packages/python.scm (python-2.7)[outputs]: Add `idle'.
[arguments]: Move idle files in the new output.
(wrap-python): Do not wrap idle since it is in a different output.
---
 gnu/packages/python.scm | 28 +++++++++++++++++++++++++---
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/gnu/packages/python.scm b/gnu/packages/python.scm
index 4ac4b7c23c..72e05552d8 100644
--- a/gnu/packages/python.scm
+++ b/gnu/packages/python.scm
@@ -176,7 +176,8 @@ 
                        "Lib/test/test_subprocess.py"))
            #t))))
     (outputs '("out"
-               "tk"))                   ;tkinter; adds 50 MiB to the closure
+               "tk"                     ;tkinter; adds 50 MiB to the closure
+               "idle"))                 ;programming environment; weighs 5MB
     (build-system gnu-build-system)
     (arguments
      `(#:test-target "test"
@@ -357,6 +358,27 @@ 
                       (install-file tkinter.so target)
                       (delete-file tkinter.so)))))
                #t)))
+         (add-after 'install 'move-idle
+           (lambda* (#:key outputs #:allow-other-keys)
+             ;; when idle is built, move it to a separate output to save some
+             ;; space (5MB)
+             (let ((out (assoc-ref outputs "out"))
+                   (idle (assoc-ref outputs "idle")))
+               (when idle
+                 (for-each
+                  (lambda (file)
+                    (let ((target (string-append idle "/bin/" (basename file))))
+                      (install-file file (dirname target))
+                      (delete-file file)))
+                  (find-files (string-append out "/bin") "^idle"))
+                 (match (find-files out "^idlelib$" #:directories? #t)
+                   ((idlelib)
+                    (let* ((len (string-length out))
+                           (target (string-append idle "/"
+                                                  (string-drop idlelib len)
+                                                  "/site-packages")))
+                      (mkdir-p (dirname target))
+                      (rename-file idlelib target))))))))
          (add-after 'install 'install-sitecustomize.py
            ,(customize-site version)))))
     (inputs
@@ -620,8 +642,8 @@  for more information.")))
                   (lambda (old new)
                     (symlink (string-append python old)
                              (string-append bin "/" new)))
-                  `("python3" ,"pydoc3" ,"idle3" ,"pip3")
-                  `("python"  ,"pydoc"  ,"idle"  ,"pip"))
+                  `("python3" ,"pydoc3" ,"pip3")
+                  `("python"  ,"pydoc"  ,"pip"))
                 ;; python-config outputs search paths based upon its location,
                 ;; use a bash wrapper to avoid changing its outputs.
                 (let ((bash (string-append (assoc-ref %build-inputs "bash")
-- 
2.31.1