Mailing List Archive

PEP 659: Specializing Adaptive Interpreter
Hi everyone,

I would like to present PEP 659.

This is an informational PEP about a key part of our plan to improve
CPython performance for 3.11 and beyond.

For those of you aware of the recent releases of Cinder and Pyston,
PEP 659 might look similar.
It is similar, but I believe PEP 659 offers better interpreter
performance and is more suitable to a collaborative, open-source
development model.

As always, comments and suggestions are welcome.

Cheers,
Mark.

Links:

https://www.python.org/dev/peps/pep-0659/
https://github.com/facebookincubator/cinder
https://github.com/pyston/pyston
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/VKBV4X6ZEMRBALW7JNOZYI22KETR4F3R/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On 5/12/2021 1:40 PM, Mark Shannon wrote:

> This is an informational PEP about a key part of our plan to improve
> CPython performance for 3.11 and beyond.

> As always, comments and suggestions are welcome.

The claim that starts the Motivation section, "Python is widely
acknowledged as slow.", has multiple problems. While some people
believe, or at least claim to believe "Python is slow", other know that
as stated, the latter is false. Languages do not have a speed, only
implementations running code for particular applications have a speed,
or a speed relative to equivalent code in another language with a
different runtime.

I reason I am picking on this is that the meme 'Python is slow' is being
morphed into 'Python is destroying the earth' (and should be abandoned,
if not banned). Last fall, a science news journal (Nature News?) quoted
a 'concerned scientist' saying just this. An internet troll repeated it
last week on comp.lang.python (from where it leaked onto python-list).

It is true that Python has characteristics that make it *relatively*
difficult to write interpreters that are *relatively* fast in certain
applications. But the opposite is also true. The language does *not*
mandate that objects, their methods, and modules be written in the language.

Hence, CPython implements builtin objects and function and some stdlib
modules in C and allows 3rd party modules written in C or C++ or
Fortran. I believe the first killer app for Python, in the mid 1990s,
numerical computing with NumericalPython. Rather than being 'slow',
CPython *enabled* people, with a few percent of added time, to access
fast, heavily optimized C and Fortran libraries and do things they could
not do in Fortran and that would have been much more difficult in C. My
daughter's PhD thesis work is a recent example of using Python to access
C libraries.

The concerned scientist mentioned above noted, more or less correctly,
that numerical code, such as neuro-network code, is, say, 80x slower in
pure python than in compiled C. But he did not mention that serious
numerical and scientific work in Python is not done with such code.
I have seen this sort of bogus comparison before.

> https://www.python.org/dev/peps/pep-0659/

--
Terry Jan Reedy

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/M76T6V7UXJRARMVM2CQOMEIOOB4FQZQZ/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On 5/12/2021 1:40 PM, Mark Shannon wrote:

> This is an informational PEP about a key part of our plan to improve
> CPython performance for 3.11 and beyond.

What is the purpose of this PEP? It seems in part to be like a
Standards Track PEP in that it proposes a new (revised) implementation
idea for the CPython bycode interpreter. Do you not intend this to not
constitute approval of even the principle?

One of the issues in the new project gave formulas for the cost versus
benefit calculations underlying specialization. Depending on your
purpose, it might be good to include them. They certainly gave some
clarity to me.

--
Terry Jan Reedy

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/I3UXYIMASCD5MDJ5QU6ITF6CRKIURHQ5/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
Hi Terry,

On 13/05/2021 5:32 am, Terry Reedy wrote:
> On 5/12/2021 1:40 PM, Mark Shannon wrote:
>
>> This is an informational PEP about a key part of our plan to improve
>> CPython performance for 3.11 and beyond.
>
>> As always, comments and suggestions are welcome.
>
> The claim that starts the Motivation section, "Python is widely
> acknowledged as slow.", has multiple problems. While some people
> believe, or at least claim to believe "Python is slow", other know that
> as stated, the latter is false.  Languages do not have a speed, only
> implementations running code for particular applications have a speed,
> or a speed relative to equivalent code in another language with a
> different runtime.

I broadly agree, but CPython is largely synonymous with Python and
CPython is slower than it could be.

The phrase was not meant to upset anyone.
How would you rephrase it, bearing in mind that needs to be short?

>
> I reason I am picking on this is that the meme 'Python is slow' is being
> morphed into 'Python is destroying the earth' (and should be abandoned,
> if not banned).  Last fall, a science news journal (Nature News?) quoted
> a 'concerned scientist' saying just this.  An internet troll repeated it
> last week on comp.lang.python (from where it leaked onto python-list).

It is a legitimate concern that CPython is bad for the environment, and
one that I hope we can address by speeding up CPython.

Since, faster == less energy for the same amount of work, making CPython
faster will reduce the amount of CO2 produced to do that work and
hopefully make it less of a concern.

Of course, compared to the environmental disaster that is BitCoin, it's
not a big deal.

>
> It is true that Python has characteristics that make it *relatively*
> difficult to write interpreters that are *relatively* fast in certain
> applications.  But the opposite is also true.  The language does *not*
> mandate that objects, their methods, and modules be written in the
> language.
>
> Hence, CPython implements builtin objects and function and some stdlib
> modules in C and allows 3rd party modules written in C or C++ or
> Fortran. I believe the first killer app for Python, in the mid 1990s,
> numerical computing with NumericalPython.  Rather than being 'slow',
> CPython *enabled* people, with a few percent of added time, to access
> fast, heavily optimized C and Fortran libraries and do things they could
> not do in Fortran and that would have been much more difficult in C.  My
> daughter's PhD thesis work is a recent example of using Python to access
> C libraries.

Yes, one of the great things about Python is that almost every library
of any size has Python bindings.

But there is a difference between making code that is already written in
C/Fortran available to Python and telling people to write code in
C/Fortran because their Python code is too slow.

We want people to be able to write code in Python and have it perform at
the level they would get from a good Javascript or lua implementation.

>
> The concerned scientist mentioned above noted, more or less correctly,
> that numerical code, such as neuro-network code, is, say, 80x slower in
> pure python than in compiled C.  But he did not mention that serious
> numerical and scientific work in Python is not done with such code.
> I have seen this sort of bogus comparison before.

It is still important to speed up Python though.

If a program does 95% of its work in a C++ library and 5% in Python, it
can easily spend the majority of its time in Python because CPython is a
lot slower than C++ (in general).

Cheers,
Mark.

>
>> https://www.python.org/dev/peps/pep-0659/
>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/QAWW7I733TPNU4VE2I4J2FCPCXXVM7MG/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
Hi Terry,

On 13/05/2021 8:20 am, Terry Reedy wrote:
> On 5/12/2021 1:40 PM, Mark Shannon wrote:
>
>> This is an informational PEP about a key part of our plan to improve
>> CPython performance for 3.11 and beyond.
>
> What is the purpose of this PEP?  It seems in part to be like a
> Standards Track PEP in that it proposes a new (revised) implementation
> idea for the CPython bycode interpreter.  Do you not intend this to not
> constitute approval of even the principle?

I will make it a standards PEP if anyone feels that would be better.
We can implement PEP 659 incrementally, without any large changes to the
implementation or any to the language or API/ABI, so a standards PEP
didn't seem necessary to us.

However, because it is a large change to the implementation, it seemed
worth documenting and doing so in a clearly public fashion. Hence the
informational PEP.

>
> One of the issues in the new project gave formulas for the cost versus
> benefit calculations underlying specialization.  Depending on your
> purpose, it might be good to include them.  They certainly gave some
> clarity to me.
>

Which ones in particular? I can add something like them to the PEP.

Cheers,
Mark.


_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/VY3JB3XO4D2E65ZR5IZUDP7MFQJ3JXIF/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On Thu, 13 May 2021 at 09:23, Mark Shannon <mark@hotpy.org> wrote:
>
> Hi Terry,
>
> On 13/05/2021 5:32 am, Terry Reedy wrote:
> > On 5/12/2021 1:40 PM, Mark Shannon wrote:
> >
> >> This is an informational PEP about a key part of our plan to improve
> >> CPython performance for 3.11 and beyond.
> >
> >> As always, comments and suggestions are welcome.
> >
> > The claim that starts the Motivation section, "Python is widely
> > acknowledged as slow.", has multiple problems. While some people
> > believe, or at least claim to believe "Python is slow", other know that
> > as stated, the latter is false. Languages do not have a speed, only
> > implementations running code for particular applications have a speed,
> > or a speed relative to equivalent code in another language with a
> > different runtime.
>
> I broadly agree, but CPython is largely synonymous with Python and
> CPython is slower than it could be.
>
> The phrase was not meant to upset anyone.
> How would you rephrase it, bearing in mind that needs to be short?

How about simply "The CPython interpreter, while sufficiently fast for
much use, could be faster"? Along with the following sentence, this
seems to me to state the situation fairly but in a way that motivates
this proposal.

Paul
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/UQFXWM64JORKYJVAGIOV6YJ5OWDTTQRA/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On Wed, May 12, 2021 at 10:48 AM Mark Shannon <mark@hotpy.org> wrote:

> ...
> For those of you aware of the recent releases of Cinder and Pyston,
> PEP 659 might look similar.
> It is similar, but I believe PEP 659 offers better interpreter
> performance and is more suitable to a collaborative, open-source
> development model.
>

I was curious what you meant by "is more suitable to a collaborative,
open-source
development model," but I didn't see it elaborated on in the PEP. If this
is indeed a selling point, it might be worth mentioning that and saying why.

--Chris


> As always, comments and suggestions are welcome.
>
> Cheers,
> Mark.
>
> Links:
>
> https://www.python.org/dev/peps/pep-0659/
> https://github.com/facebookincubator/cinder
> https://github.com/pyston/pyston
> _______________________________________________
> Python-Dev mailing list -- python-dev@python.org
> To unsubscribe send an email to python-dev-leave@python.org
> https://mail.python.org/mailman3/lists/python-dev.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-dev@python.org/message/VKBV4X6ZEMRBALW7JNOZYI22KETR4F3R/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On Thu, May 13, 2021 at 9:18 AM Mark Shannon <mark@hotpy.org> wrote:

> Hi Terry,
>
> On 13/05/2021 5:32 am, Terry Reedy wrote:
> > On 5/12/2021 1:40 PM, Mark Shannon wrote:
> >
> >> This is an informational PEP about a key part of our plan to improve
> >> CPython performance for 3.11 and beyond.
> >
> >> As always, comments and suggestions are welcome.
> >
> > The claim that starts the Motivation section, "Python is widely
> > acknowledged as slow.", has multiple problems. While some people
> [...]

> different runtime.
>
> I broadly agree, but CPython is largely synonymous with Python and
> CPython is slower than it could be.
>
> The phrase was not meant to upset anyone.
> How would you rephrase it, bearing in mind that needs to be short?
>
> "There is broad interest in improving the performance of pure Python code.
[.optionally: The integration of powerful numerical and other algorithms
already makes Python competitive at intensive computations, but programmers
whose main interest is in solving non-programming problems cannot
generally create such solutions.]"

[...]

hopefully make it less of a concern.
>
> Of course, compared to the environmental disaster that is BitCoin, it's
> not a big deal.


Every little helps. Please switch off the light as you leave the room.

[...]

> It is still important to speed up Python though.
>
> Agreed.


> If a program does 95% of its work in a C++ library and 5% in Python, it
> can easily spend the majority of its time in Python because CPython is a
> lot slower than C++ (in general).
>
> That's a pretty loose statement, but I see no reason to quibbe about the
details. There's room for improvement, and improvement will be welcome.
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On Thu, May 13, 2021 at 09:18:27AM +0100, Mark Shannon wrote:

> Since, faster == less energy for the same amount of work, making CPython
> faster will reduce the amount of CO2 produced to do that work and
> hopefully make it less of a concern.

Work expands to fill the time available: if Python is 10% more
efficient, people will use that extra speed to do 10% more work. There
will be no nett savings in CO2 and if anything a faster Python will lead
to more people using it and hence a nett increase in Python's share of
the CO2 emissions.

Let's make Python faster, but don't fool ourselves that we're doing it
for the environment. Every time people improve the efficiency of some
resource, we respond by using more of that resource.


--
Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/KWKEBEW5U6CTBQCBDEHBD62WHNZYQ33P/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
I suggest we keep it really simple, and name the implementation.

Building on Steve Holden’s suggestion:

There is broad interest in improving the performance of the cPython
runtime. (Interpreter?)

-CHB


--
Christopher Barker, PhD (Chris)

Python Language Consulting
- Teaching
- Scientific Software Development
- Desktop GUI and Web Development
- wxPython, numpy, scipy, Cython
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
Em qua., 12 de mai. de 2021 às 14:45, Mark Shannon <mark@hotpy.org>
escreveu:

> Hi everyone,
>
> I would like to present PEP 659.
>
> This is an informational PEP about a key part of our plan to improve
> CPython performance for 3.11 and beyond.
>
> For those of you aware of the recent releases of Cinder and Pyston,
> PEP 659 might look similar.
> It is similar, but I believe PEP 659 offers better interpreter
> performance and is more suitable to a collaborative, open-source
> development model.
>
> As always, comments and suggestions are welcome.
>
>
Hi Mark,

I think this seems like a nice proposal... I do have some questions related
to the PEP though (from the point of view of implementing a debugger over
it some things are kind of vague to me):

1. When will the specialization happen? (i.e.: is bytecode expected to be
changed while the code is running inside a frame or must it all be done
prior to entering a frame on a subsequent call?)

2. When the adaptive specialization happens, will that be reflected on the
actual bytecode seen externally in the frame or is that all internal? Will
clients be able to make sense of that? -- i.e.: In the debugger right now I
have a need on some occasions to detect the structure of the code from the
bytecode itself (for instance to detect whether some exception would be
handled or unhandled at raise time just given the bytecode).

3. Another example: I'm working right now on a feature to step into a
method. To do that right now my approach is:
- Compute the function call names and bytecode offsets in a given frame.
- When a frame is called (during a frame.f_trace call), check the
parent frame bytecode offset (frame.f_lasti) to detect if the last thing
was the expected call (and if it was, break the execution).

This seems reasonable given the current implementation, where bytecodes are
all fixed and there's a mapping from the frame.f_lasti ... Will that still
work with the specializing adaptive interpreter?

4. Will it still be possible to change the frame.f_code prior to execution
from a callback set in `PyThreadState.interp.eval_frame` (which will change
the code to add a breakpoint to the bytecode and later call
`_PyEval_EvalFrameDefault`)? Note: this is done in the debugger so that
Python can run without any tracing until the breakpoint is hit (tracing is
set afterwards to actually pause the execution as well as doing step
operations).

Best regards,

Fabio
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
Mark Shannon writes:
> On 13/05/2021 5:32 am, Terry Reedy wrote:

> > The claim that starts the Motivation section, "Python is widely
> > acknowledged as slow.", has multiple problems.

> How would you rephrase it, bearing in mind that needs to be short?

We can make CPython run significantly faster, at a reasonable cost
in developer time, without otherwise changing the sematics of the
language.

If you have good justification for saying "as fast as the best JS/Lua
implementations" or whatever, feel free to substitute that for
"significantly faster".

And now this:

> It is a legitimate concern that CPython is bad for the environment,

It is not.

I do this for a living (5 hours in a research hackathon just this
afternoon on a closely related topic[1]), and I assure you that such
"concern" is legitimate only as a matter of purely speculative
metaphysics. We don't have the data to analyze the possibilities, and
we don't even have the models if we did have the data.

The implied model that gets you from your tautology to "concern" is
just plain wrong -- work to be done is not independent of the cost of
doing it[2], not to mention several other relevant variables, and cannot
be made so in a useful model.

> and hopefully make it less of a concern.

It is only a concern in the Tucker Carlson "just asking questions"
mode of "concern". Really -- it's *that* bad.

> We want people to be able to write code in Python and have it
> perform at the level they would get from a good Javascript or lua
> implementation.

So say that. Nothing to be ashamed of there!

The work you propose to do is valuable for a lot of valid reasons, the
most important of which is "because we can and there's no immediate
downside".[3] Stick to those.


Footnotes:
[1] Yoshida, M., Turnbull, S.J. Voluntary provision of environmental
offsets under monopolistic competition. Int Tax Public Finance
(2021). https://doi.org/10.1007/s10797-020-09630-5.
Paywalled, available from the author, rather specialized, though. Two
works-in-progress are much more closely related, but I have a paranoid
coauthor so can't say more at this time. :-)

[2] As Steven d'Aprano points out colorfully, using Parkinson's Law.

[3] Look up Braess's Paradox for a classic and mathematically simple
example of how reducing cost "with no immediate downside" can increase
expense "once everything works itself out."

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/XK2R7ZUPWTXG4ZDTTE2KB52TFYE6KXEH/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On Thu, May 13, 2021 at 08:44:04AM -0700, Christopher Barker wrote:
> I suggest we keep it really simple, and name the implementation.
>
> Building on Steve Holden’s suggestion:
>
> There is broad interest in improving the performance of the cPython
> runtime. (Interpreter?)

+1

As excellent as Mark's work will undoubtfully be, I will be surprised if
it will result in PyPy, MicroPython, Nuitka, etc becoming faster too :-)


--
Steve
(one of the other ones)
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/4HUXRVTXYNTVEC46QAX7ELVWF6Y4Q6MT/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On Thu, May 13, 2021 at 03:20:31AM -0400, Terry Reedy wrote:

> What is the purpose of this PEP? It seems in part to be like a
> Standards Track PEP in that it proposes a new (revised) implementation
> idea for the CPython bycode interpreter. Do you not intend this to not
> constitute approval of even the principle?

Sorry Terry, that last sentence has too many negatives for me to parse
this early in the morning. Could you rephrase please?


--
Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/XL225QYJASK3NXWNIOCXGSY7YNVBC6GZ/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
Hi Fabio,

On 13/05/2021 7:11 pm, Fabio Zadrozny wrote:
>
> Em qua., 12 de mai. de 2021 às 14:45, Mark Shannon <mark@hotpy.org
> <mailto:mark@hotpy.org>> escreveu:
>
> Hi everyone,
>
> I would like to present PEP 659.
>
> This is an informational PEP about a key part of our plan to improve
> CPython performance for 3.11 and beyond.
>
> For those of you aware of the recent releases of Cinder and Pyston,
> PEP 659 might look similar.
> It is similar, but I believe PEP 659 offers better interpreter
> performance and is more suitable to a collaborative, open-source
> development model.
>
> As always, comments and suggestions are welcome.
>
>
> Hi Mark,
>
> I think this seems like a nice proposal... I do have some questions
> related to the PEP though (from the point of view of implementing a
> debugger over it some things are kind of vague to me):
>
> 1. When will the specialization happen? (i.e.: is bytecode expected to
> be changed while the code is running inside a frame or must it all be
> done prior to entering a frame on a subsequent call?)

The specialization is adaptive, so it can happen at anytime during
execution.

>
> 2. When the adaptive specialization happens, will that be reflected on
> the actual bytecode seen externally in the frame or is that all
> internal? Will clients be able to make sense of that? -- i.e.: In the
> debugger right now I have a need on some occasions to detect the
> structure of the code from the bytecode itself (for instance to detect
> whether some exception would be handled or unhandled at raise time just
> given the bytecode).

The bytecode, as externally visible, will be unchanged. All
specializations will be internal and should be invisible to all Python
tools.

>
> 3. Another example: I'm working right now on a feature to step into a
> method. To do that right now my approach is:
>     - Compute the function call names and bytecode offsets in a given
> frame.
>     - When a frame is called (during a frame.f_trace call), check the
> parent frame bytecode offset (frame.f_lasti) to detect if the last thing
> was the expected call (and if it was, break the execution).
>
> This seems reasonable given the current implementation, where bytecodes
> are all fixed and there's a mapping from the frame.f_lasti ... Will that
> still work with the specializing adaptive interpreter?

If you are implementing this in Python, then everything should work as
it does now.

OOI, would inserting a breakpoint at offset 0 in the callee function
work?

>
> 4. Will it still be possible to change the frame.f_code prior to
> execution from a callback set in `PyThreadState.interp.eval_frame`
> (which will change the code to add a breakpoint to the bytecode and
> later call `_PyEval_EvalFrameDefault`)? Note: this is done in the
> debugger so that Python can run without any tracing until the breakpoint
> is hit (tracing is set afterwards to actually pause the execution as
> well as doing step operations).

Since frame.f_code is read-only in Python, I assume you mean in C.

I can make no guarantees about the layout or meaning of fields in the C
frame struct, I'm afraid.
But I'm sure we can get something to work for you.

Cheers,
Mark.

>
> Best regards,
>
> Fabio
>
>
>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/JLD3PDRU6YXPIGXVUDE3JP4EEBI2PWJ7/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
>
> > 3. Another example: I'm working right now on a feature to step into a
> > method. To do that right now my approach is:
> > - Compute the function call names and bytecode offsets in a given
> > frame.
> > - When a frame is called (during a frame.f_trace call), check the
> > parent frame bytecode offset (frame.f_lasti) to detect if the last thing
> > was the expected call (and if it was, break the execution).
> >
> > This seems reasonable given the current implementation, where bytecodes
> > are all fixed and there's a mapping from the frame.f_lasti ... Will that
> > still work with the specializing adaptive interpreter?
>
> If you are implementing this in Python, then everything should work as
> it does now.
>

Ok... this part is all done in Python, so, if frame.f_lasti is still
updated properly according to the original bytecode while executing the
super instructions, then all seems to work properly on my side ;).

>
> OOI, would inserting a breakpoint at offset 0 in the callee function
> work?
>

Yes... if you're curious, for the breakpoint to actually work, what is done
is generate bytecode which calls a function to set the tracing and later
generates a spurious line event (so that the tracing function is then able
to make the pause. The related code that generates this bytecode would be:
https://github.com/fabioz/PyDev.Debugger/blob/pydev_debugger_2_4_1/_pydevd_frame_eval/pydevd_modify_bytecode.py#L74
).


>
> >
> > 4. Will it still be possible to change the frame.f_code prior to
> > execution from a callback set in `PyThreadState.interp.eval_frame`
> > (which will change the code to add a breakpoint to the bytecode and
> > later call `_PyEval_EvalFrameDefault`)? Note: this is done in the
> > debugger so that Python can run without any tracing until the breakpoint
> > is hit (tracing is set afterwards to actually pause the execution as
> > well as doing step operations).
>
> Since frame.f_code is read-only in Python, I assume you mean in C.
>
> I can make no guarantees about the layout or meaning of fields in the C
> frame struct, I'm afraid.
> But I'm sure we can get something to work for you.
>

Yes, it's indeed done in C (cython in this case... the related code for
reference is:
https://github.com/fabioz/PyDev.Debugger/blob/pydev_debugger_2_4_1/_pydevd_frame_eval/pydevd_frame_evaluator.template.pyx#L591
).

I'm fine in going through some other way or using some other API (this is
quite specific to a debugger after all), I just wanted to let you know of
the use case so that something along those lines can still be supported
(currently on CPython, this is as low-overhead for debugging as I can think
of, but since there'll be an adaptive bytecode specializer, using any other
custom API would also be reasonable).

Cheers,

Fabio
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On Thu, May 13, 2021 at 11:07 PM Steven D'Aprano <steve@pearwood.info>
wrote:

> Steve
> (one of the other ones)
>

We are all other Steves!
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
The related code that generates this bytecode

https://www.credosystemz.com/training-in-chennai/best-python-training-in-chennai/
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/U4B3JG2LNHNHPZRGFC4ZHL4S2OYFVJ3V/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
Steve Holden writes:
> On Thu, May 13, 2021 at 11:07 PM Steven D'Aprano <steve@pearwood.info>
> wrote:
>
> > Steve
> > (one of the other ones)
> >
>
> We are all other Steves!

+1

There were five Steves (and one Stephanie) in my 6th grade class (of
27). "Steve, move that <heavy object>" became an idiom ....

-- Other Steve since 1994

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/FS6XIKMOD2JIUQINJ6UB7ZRJFCMMR4DN/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On Tue, May 18, 2021 at 8:51 PM Stephen J. Turnbull
<turnbull.stephen.fw@u.tsukuba.ac.jp> wrote:
>
> Steve Holden writes:
> > On Thu, May 13, 2021 at 11:07 PM Steven D'Aprano <steve@pearwood.info>
> > wrote:
> >
> > > Steve
> > > (one of the other ones)
> > >
> >
> > We are all other Steves!
>
> +1
>
> There were five Steves (and one Stephanie) in my 6th grade class (of
> 27). "Steve, move that <heavy object>" became an idiom ....
>
> -- Other Steve since 1994
>

I feel the need to redress the balance of names here. This thread has
had a mere two Chrises so far, and I am improving that statistic by
50%.

ChrisA
(used to style his name as "chrisa" but people complained that I
looked like a girl)
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/UMMFCI5NTHCF3PZVNJYVRH4ZKQQDWE5L/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On 5/13/2021 4:18 AM, Mark Shannon wrote:
> Hi Terry,
>
> On 13/05/2021 5:32 am, Terry Reedy wrote:
>> On 5/12/2021 1:40 PM, Mark Shannon wrote:
>>
>>> This is an informational PEP about a key part of our plan to improve
>>> CPython performance for 3.11 and beyond.
>>
>>> As always, comments and suggestions are welcome.
>>
>> The claim that starts the Motivation section, "Python is widely
>> acknowledged as slow.", has multiple problems. While some people
>> believe, or at least claim to believe "Python is slow", other know
>> that as stated, the latter is false.  Languages do not have a speed,
>> only implementations running code for particular applications have a
>> speed, or a speed relative to equivalent code in another language with
>> a different runtime.
>
> I broadly agree, but CPython is largely synonymous with Python and
> CPython is slower than it could be.
>
> The phrase was not meant to upset anyone.
> How would you rephrase it, bearing in mind that needs to be short?

Others have given some fine suggestions. Take your pick.

[ship]
> We want people to be able to write code in Python and have it perform at
> the level they would get from a good Javascript or lua implementation.

I agree with others that this is a good way to state the goal. It also
seems on the face of it reasonable, though not trivial. I get the
impression that you are proposing to use python-adapted variations of
techniques already used for such implementations.


> It is still important to speed up Python though.

I completely agree. Some application areas are amenable to speedup be
resorting to C libraries, often already available. Others are not. The
latter involve lots of idiosyncratic business logic, individual numbers
rather than arrays of numbers, and strings.

Numpy based applications gain firstly from using unboxed arrays of
machine ints and floats instead of lists (and lists) of boxed ints and
floats and secondly from C or assembly-coded routines

Python strings are already arrays of machine ints (codepoints). Basic
operations on strings, such as 'substring in string' are already coded
in C working on machine values. So the low-hanging fruit has already
been picked.

> If a program does 95% of its work in a C++ library and 5% in Python, it
> can easily spend the majority of its time in Python because CPython is a
> lot slower than C++ (in general).

I believe the ratio for the sort of numerical computing getting bogus
complaints is sometimes more like 95% of *time* in compiled C and only,
say, 5% of *time* in the Python interpreter. So even if the interpreter
ran instantly, it would make also no difference -- for such applications.

--
Terry Jan Reedy


_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/GFUISO3ZDYS7E3FV457AVRW2Q7B5BAVW/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On Thu, 20 May 2021 at 04:58, Terry Reedy <tjreedy@udel.edu> wrote:
>
> On 5/13/2021 4:18 AM, Mark Shannon wrote:
>
> > If a program does 95% of its work in a C++ library and 5% in Python, it
> > can easily spend the majority of its time in Python because CPython is a
> > lot slower than C++ (in general).
>
> I believe the ratio for the sort of numerical computing getting bogus
> complaints is sometimes more like 95% of *time* in compiled C and only,
> say, 5% of *time* in the Python interpreter. So even if the interpreter
> ran instantly, it would make also no difference -- for such applications.

Not necessarily because if the interpreter is faster then it opens up
new options that perhaps don't involve the same C routines. The
situation right now is that it is often faster to do more
"computation" than needed using efficient C routines rather than do
precisely what is needed in bare Python. If the bare Python part
becomes faster then maybe you don't need the C routine at all.

To give a concrete example, in SymPy I have written a pure Python
implementation of typed sparse matrices (this is much faster than the
public Matrix class so don't compare with that). I would like to use
the flint library to speed up some of these matrix calculations and
the flint library has a highly optimised C/assembly implementation of
dense matrices of arbitrary precision integers. Which of these
implementations is faster for e.g. matrix multiplication depends on
how sparse the matrix actually is. If I have a large matrix say 1000 x
1000 and only 1% of the elements are nonzero then the pure Python
sparse implementation is faster (it can be much faster as the density
reduces since it does not have the same big-O characteristics). On the
other hand for fully dense matrices where all elements are nonzero the
flint implementation is consistently around 100x faster.

The break even point where both implementations take equal time is
around about 5% density. What that means is that for a 1000 x 1000
matrix with 10% of elements nonzero it is faster to ask flint to
construct an enormous dense matrix and perform a huge number of
arithmetic operations (mostly involving zeros) than it is to use a
pure Python implementation that has more favourable asymptotic
complexity and theoretically computes the result with 100x fewer
arithmetic "operations". In this situation there is a sliding scale
where the faster the Python interpreter gets the less often you
benefit from calling the C routine in the first place.

Although this is a very specific example it illustrates something that
I see very often which is that while the efficient C routines can make
things "run at the speed of C" you can often end up optimising things
to use an approach that would seem inefficient if you were working in
C directly. This happens because it works out faster from the
perspective of pure Python code that is encumbered by interpreter
overhead and has a limited range of C routines to choose from. If the
interpreter overhead is less then the options to choose from are
improved.

Also for many applications it is much easier for the programmer to
write an algorithm directly in loops rather than coming up with a
vectorised version based on e.g. numpy arrays. Vectorisation as a way
of optimising code is actually work for the programmer. There is
another tradeoff here which is not about C speed vs Python speed but
about programmer time vs CPU time. If a straight-forward Python
implementation is already "fast enough" then you don't need to spend
time thinking about how to translate that into something that would
possibly run faster (potentially at the expense of code readability).
In the case of SymPy/flint if the maximum speed gain of flint was only
10x then I might not bother using it at all to avoid the complexity of
having multiple implementations to choose from and external
dependencies etc.

--
Oscar
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/BBNEBYO42RAXUM526ZUA65SAQTKCS3QD/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
On 5/20/2021 10:49 AM, Oscar Benjamin wrote:
> On Thu, 20 May 2021 at 04:58, Terry Reedy <tjreedy@udel.edu> wrote:

>> I believe the ratio for the sort of numerical computing getting bogus
>> complaints is sometimes more like 95% of *time* in compiled C and only,
>> say, 5% of *time* in the Python interpreter. So even if the interpreter
>> ran instantly, it would make also no difference -- for such applications.

'also' was meant to be 'almost'

> Not necessarily

In the context I carefully defined, where Python is falsely accused of
endangering the earth, by people who set up strawpeople images of how
Python is actually used and who care nothing about programmer time and
joy, yes, necessarily. However, in the related context you define,
faster Python could help save the earth by reducing the need for
brute-force C routines when they are grossly inefficient. How ironic
that would be.

> because if the interpreter is faster then it opens up
> new options that perhaps don't involve the same C routines. The
> situation right now is that it is often faster to do more
> "computation" than needed using efficient C routines rather than do
> precisely what is needed in bare Python. If the bare Python part
> becomes faster then maybe you don't need the C routine at all.
>
> To give a concrete example, in SymPy I have written a pure Python
> implementation of typed sparse matrices (this is much faster than the
> public Matrix class so don't compare with that). I would like to use
> the flint library to speed up some of these matrix calculations and
> the flint library has a highly optimised C/assembly implementation of
> dense matrices of arbitrary precision integers. Which of these
> implementations is faster for e.g. matrix multiplication depends on
> how sparse the matrix actually is. If I have a large matrix say 1000 x
> 1000 and only 1% of the elements are nonzero then the pure Python
> sparse implementation is faster (it can be much faster as the density
> reduces since it does not have the same big-O characteristics). On the
> other hand for fully dense matrices where all elements are nonzero the
> flint implementation is consistently around 100x faster.
>
> The break even point where both implementations take equal time is
> around about 5% density. What that means is that for a 1000 x 1000
> matrix with 10% of elements nonzero it is faster to ask flint to
> construct an enormous dense matrix and perform a huge number of
> arithmetic operations (mostly involving zeros) than it is to use a
> pure Python implementation that has more favourable asymptotic
> complexity and theoretically computes the result with 100x fewer
> arithmetic "operations". In this situation there is a sliding scale
> where the faster the Python interpreter gets the less often you
> benefit from calling the C routine in the first place.
>
> Although this is a very specific example it illustrates something that
> I see very often which is that while the efficient C routines can make
> things "run at the speed of C" you can often end up optimising things
> to use an approach that would seem inefficient if you were working in
> C directly. This happens because it works out faster from the
> perspective of pure Python code that is encumbered by interpreter
> overhead and has a limited range of C routines to choose from. If the
> interpreter overhead is less then the options to choose from are
> improved.
>
> Also for many applications it is much easier for the programmer to
> write an algorithm directly in loops rather than coming up with a
> vectorised version based on e.g. numpy arrays. Vectorisation as a way
> of optimising code is actually work for the programmer. There is
> another tradeoff here which is not about C speed vs Python speed but
> about programmer time vs CPU time. If a straight-forward Python
> implementation is already "fast enough" then you don't need to spend
> time thinking about how to translate that into something that would
> possibly run faster (potentially at the expense of code readability).
> In the case of SymPy/flint if the maximum speed gain of flint was only
> 10x then I might not bother using it at all to avoid the complexity of
> having multiple implementations to choose from and external
> dependencies etc.
>
> --
> Oscar
>


--
Terry Jan Reedy

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/PLPOOWB6KRBSIBZLM5NOP2O5AJGAN2AB/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
Oscar Benjamin writes:

> The break even point where both implementations take equal time is
> around about 5% density. What that means is that for a 1000 x 1000
> matrix with 10% of elements nonzero it is faster to ask flint to
> construct an enormous dense matrix and perform a huge number of
> arithmetic operations (mostly involving zeros) than it is to use a
> pure Python implementation that has more favourable asymptotic
> complexity and theoretically computes the result with 100x fewer
> arithmetic "operations". In this situation there is a sliding scale
> where the faster the Python interpreter gets the less often you
> benefit from calling the C routine in the first place.

Sure, but what's also happening here is that you're optimizing
programmer cost by not writing the sparse algorithm in C, C++, or
Rust. So I haven't done the math, but I guess to double the
percentage of nonzero matrix elements that constitutes the breakeven
point you need to double the speed of the Python runtime, and I don't
think that's going to happen any time soon. As far as I can see, any
reasonably anticipatable speedup is quite marginal for you (a 10%
speedup in arithmetic is, I hope, a dream, but that would get you from
5% to 5.5% -- is that really a big deal?)

> This happens because it works out faster from the perspective of
> pure Python code that is encumbered by interpreter overhead and has
> a limited range of C routines to choose from. If the interpreter
> overhead is less then the options to choose from are improved.

Absolutely. But the real problem you face is that nobody is writing
routines for sparse matrices in languages that compile to machine code
(or worse, not wrapping already available C libraries).

> In the case of SymPy/flint if the maximum speed gain of flint was only
> 10x then I might not bother using it at all to avoid the complexity of
> having multiple implementations to choose from and external
> dependencies etc.

Sure, but my guesstimate is that that would require a 90% speedup in
Python arithmetic. Really, is that going to happen?

I feel your pain (even though for me it's quite theoretical, my own
data is dense, even impenetrable). But I just don't see even
potential 10% or 20% speedups in Python overcoming the generic need
for programmers to either (1) accept the practical limits to the size
of data they can work with in Python or (2) bite the bullet and write
C (or ctypes) that can do the calculations 100x as fast as a
well-tuned Python program.

I'm all for Mark's work, and I'm glad somebody's willing to pay him
some approximation to what he's worth, even though I probably won't
benefit myself (nor my students). But I really don't see the
economics of individual programmers changing very much -- 90% of us
will just use the tried-and-true packages (some of which are
accelerated like NumPy and Pandas), 9% will think for ten minutes and
choose (1) or (2) above, and 1% will do the careful breakeven analysis
you do, and write (and deal with the annoyances of) hybrid code.

Steve
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-leave@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/UWLGL6HGTM6LIUOS2HFZX23GFJDXQPG7/
Code of Conduct: http://python.org/psf/codeofconduct/
Re: PEP 659: Specializing Adaptive Interpreter [ In reply to ]
I find this whole conversation confusing -- does anyone really think a
substantial performance boost to cPython is not a "good thing"? Worth the
work? Maybe not, but it seems that Mark, Guido, and MS think it is -- more
power to them!

Anyway:

potential 10% or 20% speedups in Python


I believe the folks involved think they may get a factor of two speedup --
but in any case, Oscar has a point -- there is a trade-off of effort vs
performance, and increasing the performance of cPython moves that trade-off
point, even if just a little.

I like Oscar's example, because it's got hard numbers attached to it, but
the principle is the same for any time you are considering writing, or even
using, a non-python library.


> (2) bite the bullet and write
> C (or ctypes) that can do the calculations 100x as fast as a
> well-tuned Python program.
>

Oddly missing from this conversation is PyPy -- which can buy you a lot of
performance for some types of code in pure Python, and things like Cython
or numba, which can buy you a lot with slightly modified Python.

All those options are why Python is very useful today -- but none of them
make the case that making cPython run faster isn't a worthy goal.

-CHB

--
Christopher Barker, PhD (Chris)

Python Language Consulting
- Teaching
- Scientific Software Development
- Desktop GUI and Web Development
- wxPython, numpy, scipy, Cython

1 2  View All