diff mbox series

Option to change default optimization level of Python

Message ID 14bccfd5-f8fb-4120-a779-7219fa18983e@hqv.ch
State New
Headers show
Series Option to change default optimization level of Python | expand

Commit Message

Lukas Woodtli April 10, 2025, 6:23 a.m. UTC
From 1614b1bb21b164d11aa88ff428c6bae9439b82d6 Mon Sep 17 00:00:00 2001
From: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
Date: Mon, 7 Apr 2025 13:18:41 +0200
Subject: [PATCH] python3: Allow to set the default optimization level

It's possible to compile the Python interpreter for the target, that it
uses a given optimization level by default. This is as providing the
`-O` option to the interpreter (possibly one or two times) or setting
the environment variable `PYTHONOPTIMIZE`.

Signed-off-by: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
---
  ...zation-level-1-as-default-for-Python.patch | 25 ++++++++++++++++++
  ...zation-level-2-as-default-for-Python.patch | 26 +++++++++++++++++++
  .../recipes-devtools/python/python3_3.13.2.bb |  9 +++++++
  3 files changed, 60 insertions(+)
  create mode 100644 
meta/recipes-devtools/python/python3/0001-Use-optimization-level-1-as-default-for-Python.patch
  create mode 100644 
meta/recipes-devtools/python/python3/0001-Use-optimization-level-2-as-default-for-Python.patch

+++ 
b/meta/recipes-devtools/python/python3/0001-Use-optimization-level-1-as-default-for-Python.patch
@@ -0,0 +1,25 @@
+From ef51db9528916493d23a7e7a63cfc3b12b4ce3d9 Mon Sep 17 00:00:00 2001
+From: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
+Date: Tue, 8 Apr 2025 08:26:35 +0200
+Subject: [PATCH] Use optimization level 1 as default for Python
+
+This is as if the command line option `-O` is set implicitly.
+
+Upstream-Status: Inappropriate [this is an optional enhancement just 
useful for a custom build for an embedded system]
+---
+ Python/initconfig.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/Python/initconfig.c b/Python/initconfig.c
+index 192089b..c451b15 100644
+--- a/Python/initconfig.c
++++ b/Python/initconfig.c
+@@ -788,7 +788,7 @@ config_init_defaults(PyConfig *config)
+     config->bytes_warning = 0;
+     config->inspect = 0;
+     config->interactive = 0;
+-    config->optimization_level = 0;
++    config->optimization_level = 1;
+     config->parser_debug= 0;
+     config->write_bytecode = 1;
+     config->verbose = 0;
+++ 
b/meta/recipes-devtools/python/python3/0001-Use-optimization-level-2-as-default-for-Python.patch
@@ -0,0 +1,26 @@
+From 31d904e770437cf47b7b56161687a0d90b1bc982 Mon Sep 17 00:00:00 2001
+From: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
+Date: Mon, 7 Apr 2025 13:11:02 +0200
+Subject: [PATCH] Use optimization level 2 as default for Python
+
+This is as if the command line option `-OO` is set implicitly.
+
+Upstream-Status: Inappropriate [this is an optional enhancement just 
useful for a custom build for an embedded system]
+
+---
+ Python/initconfig.c | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+diff --git a/Python/initconfig.c b/Python/initconfig.c
+index 192089b..3f91fac 100644
+--- a/Python/initconfig.c
++++ b/Python/initconfig.c
+@@ -788,7 +788,7 @@ config_init_defaults(PyConfig *config)
+     config->bytes_warning = 0;
+     config->inspect = 0;
+     config->interactive = 0;
+-    config->optimization_level = 0;
++    config->optimization_level = 2;
+     config->parser_debug= 0;
+     config->write_bytecode = 1;
+     config->verbose = 0;
  SRC_URI[sha256sum] = 
"d984bcc57cd67caab26f7def42e523b1c015bbc5dc07836cf4f0b63fa159eb56"

  # exclude pre-releases for both python 2.x and 3.x

Comments

Alexander Kanavin April 10, 2025, 7:57 a.m. UTC | #1
This needs to be proposed to python upstream first. And definitely
first reworked as an option to configure script, rather than
conditional patches that hardcode the level.

You can also install a runtime wrapper for python executable that adds
the command line option, or sets the environment variable.

As it is, it's unsuitable.

Alex

On Thu, 10 Apr 2025 at 08:23, Lukas Woodtli via lists.openembedded.org
<lw=hqv.ch@lists.openembedded.org> wrote:
>
>  From 1614b1bb21b164d11aa88ff428c6bae9439b82d6 Mon Sep 17 00:00:00 2001
> From: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
> Date: Mon, 7 Apr 2025 13:18:41 +0200
> Subject: [PATCH] python3: Allow to set the default optimization level
>
> It's possible to compile the Python interpreter for the target, that it
> uses a given optimization level by default. This is as providing the
> `-O` option to the interpreter (possibly one or two times) or setting
> the environment variable `PYTHONOPTIMIZE`.
>
> Signed-off-by: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
> ---
>   ...zation-level-1-as-default-for-Python.patch | 25 ++++++++++++++++++
>   ...zation-level-2-as-default-for-Python.patch | 26 +++++++++++++++++++
>   .../recipes-devtools/python/python3_3.13.2.bb |  9 +++++++
>   3 files changed, 60 insertions(+)
>   create mode 100644
> meta/recipes-devtools/python/python3/0001-Use-optimization-level-1-as-default-for-Python.patch
>   create mode 100644
> meta/recipes-devtools/python/python3/0001-Use-optimization-level-2-as-default-for-Python.patch
>
> diff --git
> a/meta/recipes-devtools/python/python3/0001-Use-optimization-level-1-as-default-for-Python.patch
> b/meta/recipes-devtools/python/python3/0001-Use-optimization-level-1-as-default-for-Python.patch
> new file mode 100644
> index 0000000000..98e4355b1d
> --- /dev/null
> +++
> b/meta/recipes-devtools/python/python3/0001-Use-optimization-level-1-as-default-for-Python.patch
> @@ -0,0 +1,25 @@
> +From ef51db9528916493d23a7e7a63cfc3b12b4ce3d9 Mon Sep 17 00:00:00 2001
> +From: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
> +Date: Tue, 8 Apr 2025 08:26:35 +0200
> +Subject: [PATCH] Use optimization level 1 as default for Python
> +
> +This is as if the command line option `-O` is set implicitly.
> +
> +Upstream-Status: Inappropriate [this is an optional enhancement just
> useful for a custom build for an embedded system]
> +---
> + Python/initconfig.c | 2 +-
> + 1 file changed, 1 insertion(+), 1 deletion(-)
> +
> +diff --git a/Python/initconfig.c b/Python/initconfig.c
> +index 192089b..c451b15 100644
> +--- a/Python/initconfig.c
> ++++ b/Python/initconfig.c
> +@@ -788,7 +788,7 @@ config_init_defaults(PyConfig *config)
> +     config->bytes_warning = 0;
> +     config->inspect = 0;
> +     config->interactive = 0;
> +-    config->optimization_level = 0;
> ++    config->optimization_level = 1;
> +     config->parser_debug= 0;
> +     config->write_bytecode = 1;
> +     config->verbose = 0;
> diff --git
> a/meta/recipes-devtools/python/python3/0001-Use-optimization-level-2-as-default-for-Python.patch
> b/meta/recipes-devtools/python/python3/0001-Use-optimization-level-2-as-default-for-Python.patch
> new file mode 100644
> index 0000000000..ca196970a6
> --- /dev/null
> +++
> b/meta/recipes-devtools/python/python3/0001-Use-optimization-level-2-as-default-for-Python.patch
> @@ -0,0 +1,26 @@
> +From 31d904e770437cf47b7b56161687a0d90b1bc982 Mon Sep 17 00:00:00 2001
> +From: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
> +Date: Mon, 7 Apr 2025 13:11:02 +0200
> +Subject: [PATCH] Use optimization level 2 as default for Python
> +
> +This is as if the command line option `-OO` is set implicitly.
> +
> +Upstream-Status: Inappropriate [this is an optional enhancement just
> useful for a custom build for an embedded system]
> +
> +---
> + Python/initconfig.c | 2 +-
> + 1 file changed, 1 insertion(+), 1 deletion(-)
> +
> +diff --git a/Python/initconfig.c b/Python/initconfig.c
> +index 192089b..3f91fac 100644
> +--- a/Python/initconfig.c
> ++++ b/Python/initconfig.c
> +@@ -788,7 +788,7 @@ config_init_defaults(PyConfig *config)
> +     config->bytes_warning = 0;
> +     config->inspect = 0;
> +     config->interactive = 0;
> +-    config->optimization_level = 0;
> ++    config->optimization_level = 2;
> +     config->parser_debug= 0;
> +     config->write_bytecode = 1;
> +     config->verbose = 0;
> diff --git a/meta/recipes-devtools/python/python3_3.13.2.bb
> b/meta/recipes-devtools/python/python3_3.13.2.bb
> index 7c36fd92ed..f86c150534 100644
> --- a/meta/recipes-devtools/python/python3_3.13.2.bb
> +++ b/meta/recipes-devtools/python/python3_3.13.2.bb
> @@ -36,6 +36,15 @@ SRC_URI:append:class-native = " \
> file://0001-Lib-sysconfig.py-use-prefix-value-from-build-configu.patch \
>              "
>
> +
> +# Default optimization level when running the Python interpreter (`-O`
> flag)
> +PYTHON_DEFAULT_OPT_LEVEL ?= "0"
> +
> +SRC_URI:append:class-target = " \
> + ${@'file://0001-Use-optimization-level-1-as-default-for-Python.patch'
> if d.getVar('PYTHON_DEFAULT_OPT_LEVEL') == '1' else ''} \
> + ${@'file://0001-Use-optimization-level-2-as-default-for-Python.patch'
> if d.getVar('PYTHON_DEFAULT_OPT_LEVEL') == '2' else ''} \
> +"
> +
>   SRC_URI[sha256sum] =
> "d984bcc57cd67caab26f7def42e523b1c015bbc5dc07836cf4f0b63fa159eb56"
>
>   # exclude pre-releases for both python 2.x and 3.x
> --
> 2.43.0
>
>
>
> -=-=-=-=-=-=-=-=-=-=-=-
> Links: You receive all messages sent to this group.
> View/Reply Online (#214619): https://lists.openembedded.org/g/openembedded-core/message/214619
> Mute This Topic: https://lists.openembedded.org/mt/112186757/1686489
> Group Owner: openembedded-core+owner@lists.openembedded.org
> Unsubscribe: https://lists.openembedded.org/g/openembedded-core/unsub [alex.kanavin@gmail.com]
> -=-=-=-=-=-=-=-=-=-=-=-
>
Mathieu Dubois-Briand April 10, 2025, 8:03 a.m. UTC | #2
On Thu Apr 10, 2025 at 8:23 AM CEST, Lukas Woodtli wrote:
>  From 1614b1bb21b164d11aa88ff428c6bae9439b82d6 Mon Sep 17 00:00:00 2001
> From: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
> Date: Mon, 7 Apr 2025 13:18:41 +0200
> Subject: [PATCH] python3: Allow to set the default optimization level
>
> It's possible to compile the Python interpreter for the target, that it
> uses a given optimization level by default. This is as providing the
> `-O` option to the interpreter (possibly one or two times) or setting
> the environment variable `PYTHONOPTIMIZE`.
>
> Signed-off-by: Lukas Woodtli <lukas.woodtli@husqvarnagroup.com>
> ---

Hi Lukas,

Thanks for your patch. However, it seems it was corrupted somehow in the
sending process. This comes either from your mailer or the behaviour of
your mail server. Are you using 'git send-email' or 'b4 prep' to send
it?

First, it looks like long lines are wrapped, so git cannot understand
them. I tried a bit to reconstruct a correct patch, but failed. So maybe
something else was also modified in the process.

Can you please try to fix this? It looks like there is the same issue
for your other patch.
Lukas Woodtli April 10, 2025, 12:53 p.m. UTC | #3
On 10.04.25 13:52, Alexander Kanavin wrote:
> On Thu, 10 Apr 2025 at 13:47, Lukas Woodtli via Lists.Openembedded.Org
> <lw=hqv.ch@lists.openembedded.org> wrote:
>
>> Thank you for your feedback.
>>
>> I think that setting a default optimization level for the Python interpreter is a valid use case for an embedded system (and probably only for an embedded system).
>>
>> But as the way doing it here (with the patch) is not suitable, I'll see if I can achieve something upstream.
> Please do not reply in private. The conversation needs to stay on the
> mailing list.
>
> Alex

Sorry, this was a private message by accident.
diff mbox series

Patch

diff --git 
a/meta/recipes-devtools/python/python3/0001-Use-optimization-level-1-as-default-for-Python.patch 
b/meta/recipes-devtools/python/python3/0001-Use-optimization-level-1-as-default-for-Python.patch
new file mode 100644
index 0000000000..98e4355b1d
--- /dev/null
diff --git 
a/meta/recipes-devtools/python/python3/0001-Use-optimization-level-2-as-default-for-Python.patch 
b/meta/recipes-devtools/python/python3/0001-Use-optimization-level-2-as-default-for-Python.patch
new file mode 100644
index 0000000000..ca196970a6
--- /dev/null
diff --git a/meta/recipes-devtools/python/python3_3.13.2.bb 
b/meta/recipes-devtools/python/python3_3.13.2.bb
index 7c36fd92ed..f86c150534 100644
--- a/meta/recipes-devtools/python/python3_3.13.2.bb
+++ b/meta/recipes-devtools/python/python3_3.13.2.bb
@@ -36,6 +36,15 @@  SRC_URI:append:class-native = " \
file://0001-Lib-sysconfig.py-use-prefix-value-from-build-configu.patch \
             "

+
+# Default optimization level when running the Python interpreter (`-O` 
flag)
+PYTHON_DEFAULT_OPT_LEVEL ?= "0"
+
+SRC_URI:append:class-target = " \
+ ${@'file://0001-Use-optimization-level-1-as-default-for-Python.patch' 
if d.getVar('PYTHON_DEFAULT_OPT_LEVEL') == '1' else ''} \
+ ${@'file://0001-Use-optimization-level-2-as-default-for-Python.patch' 
if d.getVar('PYTHON_DEFAULT_OPT_LEVEL') == '2' else ''} \
+"
+