From d14c79eab5ce7d375039083aa45bceda4ceccdb3 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Sun, 16 May 2021 17:23:29 +0800 Subject: [PATCH 1/6] Fix some minor nits in whatsnew and document CALL_METHOD_KW --- Doc/library/dis.rst | 12 ++++++++++++ Doc/whatsnew/3.11.rst | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst index 193e40014d1ce4e..98bd04659f9034e 100644 --- a/Doc/library/dis.rst +++ b/Doc/library/dis.rst @@ -1176,6 +1176,18 @@ All of the following opcodes use their arguments. .. versionadded:: 3.7 +.. opcode:: CALL_METHOD_KW (argc) + + Similar to :opcode:`CALL_METHOD` but also supports keyword arguments. + Calls a method. *argc* is the number of positional and keyword arguments. + This opcode is designed to be used with :opcode:`LOAD_METHOD`. TOS is a + tuple of keyword argument names. Argument values are below that. + Below them, the two items described in :opcode:`LOAD_METHOD` are on the + stack (either ``self`` and an unbound method object or ``NULL`` and an + arbitrary callable). All of them are popped and the return value is pushed. + + .. versionadded:: 3.11 + .. opcode:: MAKE_FUNCTION (flags) Pushes a new function object on the stack. From bottom to top, the consumed diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 503688294072a3b..bb6e6fdc198a232 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -90,7 +90,7 @@ Improved Modules Optimizations ============= -* Compiler optimizes now simple C-style formatting with literal format +* Compiler now optimizes simple C-style formatting with literal format containing only format codes ``%s``, ``%r`` and ``%a`` and makes it as fast as corresponding f-string expression. (Contributed by Serhiy Storchaka in :issue:`28307`.) @@ -99,6 +99,17 @@ Optimizations almost eliminated when no exception is raised. (Contributed by Mark Shannon in :issue:`40222`.) +* Method calls with keywords are now up to 20% faster due to bytecode + changes which avoid creating bound method instances. Previously, this + optimization was applied only to method calls with purely positional + arguments. + (Contributed by Ken Jin and Mark Shannon in :issue:`26110`, based on ideas + implemented in PyPy.) + +CPython bytecode changes +======================== + +* Added a new :opcode:`CALL_METHOD_KW` opcode. Build Changes ============= From 56d502795c8af1bf3a86af1155262c676c11a50e Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Mon, 17 May 2021 22:44:17 +0800 Subject: [PATCH 2/6] Apply suggestions from code review (By Pablo) Co-authored-by: Pablo Galindo --- Doc/library/dis.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/dis.rst b/Doc/library/dis.rst index 98bd04659f9034e..a4746bcabc76bc3 100644 --- a/Doc/library/dis.rst +++ b/Doc/library/dis.rst @@ -1178,13 +1178,13 @@ All of the following opcodes use their arguments. .. opcode:: CALL_METHOD_KW (argc) - Similar to :opcode:`CALL_METHOD` but also supports keyword arguments. - Calls a method. *argc* is the number of positional and keyword arguments. + Calls a method in a similar fashion as :opcode:`CALL_METHOD`, but also supports keyword arguments. + *argc* is the number of positional and keyword arguments. This opcode is designed to be used with :opcode:`LOAD_METHOD`. TOS is a tuple of keyword argument names. Argument values are below that. Below them, the two items described in :opcode:`LOAD_METHOD` are on the stack (either ``self`` and an unbound method object or ``NULL`` and an - arbitrary callable). All of them are popped and the return value is pushed. + arbitrary callable). All of them are popped from the stack and the return value is pushed. .. versionadded:: 3.11 From 160d1a7d6a09238503b60004b740d9ca3ce6bb7b Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Mon, 17 May 2021 23:37:40 +0800 Subject: [PATCH 3/6] Add a short summary, remove 20% claim --- Doc/whatsnew/3.11.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index bb6e6fdc198a232..8d17dfece5aaa0b 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -99,7 +99,7 @@ Optimizations almost eliminated when no exception is raised. (Contributed by Mark Shannon in :issue:`40222`.) -* Method calls with keywords are now up to 20% faster due to bytecode +* Method calls with keywords are now faster due to bytecode changes which avoid creating bound method instances. Previously, this optimization was applied only to method calls with purely positional arguments. @@ -109,7 +109,8 @@ Optimizations CPython bytecode changes ======================== -* Added a new :opcode:`CALL_METHOD_KW` opcode. +* Added a new :opcode:`CALL_METHOD_KW` opcode. Calls a method in a similar +fashion as :opcode:`CALL_METHOD`, but also supports keyword arguments. Build Changes ============= From dd7668c1c09fc6c480b793181e452fe0d61f4759 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Mon, 17 May 2021 23:38:30 +0800 Subject: [PATCH 4/6] improve the summary a little --- Doc/whatsnew/3.11.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 8d17dfece5aaa0b..72b434a04249548 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -110,7 +110,8 @@ CPython bytecode changes ======================== * Added a new :opcode:`CALL_METHOD_KW` opcode. Calls a method in a similar -fashion as :opcode:`CALL_METHOD`, but also supports keyword arguments. +fashion as :opcode:`CALL_METHOD`, but also supports keyword arguments. Works +in tandem with :opcode:`LOAD_METHOD`. Build Changes ============= From cec3e74ebe4d595a1071b954a0cda403a7012e46 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Mon, 17 May 2021 23:45:30 +0800 Subject: [PATCH 5/6] fix build error (add blank line) --- Doc/whatsnew/3.11.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 72b434a04249548..11d22f8cf9c9585 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -113,6 +113,7 @@ CPython bytecode changes fashion as :opcode:`CALL_METHOD`, but also supports keyword arguments. Works in tandem with :opcode:`LOAD_METHOD`. + Build Changes ============= From a46d42d2fe4504db9b6abb7aa1a117ac2f33c703 Mon Sep 17 00:00:00 2001 From: Fidget-Spinner <28750310+Fidget-Spinner@users.noreply.github.com> Date: Mon, 17 May 2021 23:56:08 +0800 Subject: [PATCH 6/6] fix doc errors for real this time (maybe) --- Doc/whatsnew/3.11.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/whatsnew/3.11.rst b/Doc/whatsnew/3.11.rst index 11d22f8cf9c9585..9058b261560873a 100644 --- a/Doc/whatsnew/3.11.rst +++ b/Doc/whatsnew/3.11.rst @@ -110,8 +110,8 @@ CPython bytecode changes ======================== * Added a new :opcode:`CALL_METHOD_KW` opcode. Calls a method in a similar -fashion as :opcode:`CALL_METHOD`, but also supports keyword arguments. Works -in tandem with :opcode:`LOAD_METHOD`. + fashion as :opcode:`CALL_METHOD`, but also supports keyword arguments. Works + in tandem with :opcode:`LOAD_METHOD`. Build Changes