Skip to content
This repository has been archived by the owner on Nov 21, 2022. It is now read-only.

Concerns about undefined behavior #149

Open
gvanrossum opened this issue Aug 26, 2020 · 3 comments
Open

Concerns about undefined behavior #149

gvanrossum opened this issue Aug 26, 2020 · 3 comments
Labels
needs more pep An issue which needs to be documented in the PEP rejected A rejected idea sc-feedback Issues raised in the steering committee feedback

Comments

@gvanrossum
Copy link
Owner

The PEP contains some pretty strong language (added fairly late in the process) that allows the compiler to use various optimizations (see "Performance considerations" and "Exceptions and side effects").

Some SC members don't seem happy with this. They are concerned about things like attributes implemented as properties with side effects, or isinstance operations with side effects. The PEP makes it undefined how many times such side effects occur, for example:

match a:
    case MyClass(x=1, y=2): ...
    case MyClass(x=1, y=3): ...
    case MyClass(x=2, y=y) if ...: ...

Here we don't know how many times MyClass is looked up (some crazy side effect could switch the class object), and more importantly we allow but don't require the interpreter to cache the outcome of isinstance(a, MyClass) as well as the result of a.x and a.y.

I think the SC's concern is that once CPython has settled on a specific strategy here, whatever it does will become part of the de-facto standard that other implementations (e.g. PyPy) will have to implement, regardless of whether the PEP allows them more freedom.

I personally have a performance concern about a match statement with many case SomeClass(...): cases (each with a different class name) -- I wish there was some kind of pre-computation that could be done to turn this into a single (weak) dict lookup (possibly with a PyPy-style guard, and dynamically updating the dict as subclasses are encountered. But that would definitely be optional behavior.

@gvanrossum gvanrossum added the sc-feedback Issues raised in the steering committee feedback label Aug 26, 2020
@Tobias-Kohn
Copy link
Collaborator

I fully understand the reservations and think we have to tread lightly here: we certainly need to leave the door open for optimisations, but having undefined behaviour has a history of quickly turning sour and backfiring.

In order to make pattern matching scale, we certainly want to keep the option to create a decision tree and memoize as much as possible, rather than go through it linearly case by case each time. But stipulating that it has to be so means a huge burden on the first implementation (and subsequently also for other Python implementations), hindering adoption in general. Finally, even with the idea of some optimised tree, we might still end up having an "unpredictable" number of isinstance-checks.

Here is a wild and vague idea (and probably completely unrealistic): if we look at how function and class definitions work, we find that they ingeniously combine static with dynamic elements. A def statement, for instance, creates a new object that uses the already existing and static code object. For classes, we find a similar combination of static and dynamic elements. Could we perhaps adopt a similar scheme here?

What I am proposing is that the match-block could be something like a lazily compiled/evaluated thing that gets compiled the first time the interpreter hits on it. But once it is in place, it will not bother looking up the class names etc. again (this is in itself a fairly commonly used and well understood technique for lazy evaluation). Hence, it would work a bit like a closure.

As I said, I am not sure how realistic it is, but I thought I throw it out there, notwithstanding.

@gvanrossum
Copy link
Owner Author

In the SC-VC we ended up agreeing that such optimized behaviors are desirable but not mandatory and we decided to keep this language.

@gvanrossum gvanrossum added rejected A rejected idea needs more pep An issue which needs to be documented in the PEP labels Sep 16, 2020
@gvanrossum
Copy link
Owner Author

(Needs more PEP because I'm not yet happy with how this is spread across different sections of PEP 634.)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs more pep An issue which needs to be documented in the PEP rejected A rejected idea sc-feedback Issues raised in the steering committee feedback
Projects
None yet
Development

No branches or pull requests

2 participants