
On Fri, Sep 18, 2020 at 9:39 AM Paolo Lammens <lammenspaolo@gmail.com> wrote:
I'm not so sure about dispatch because it is solely based on type and isn't very flexible; if in the future any change is made to the definition of path-like, or another addition is made to `json.load`/`json.dump`, or whatever, the type-based dispatch option is more likely to need more changes.
Ideally, a match statement should have good runtime performance compared to an equivalent chain of if-statements. Although the history of
Though I doubt the __fspath__ interface will change, Unfortunately, the @singledispatch decorator accepts types but not functions (or methods); so there's no way to dispatch according to the presence or value of parameter attributes like file_.__fspath__; all @singledispatch will do is check whether isinstance(file_, os.PathLike). IDK if that's for (C) performance reasons? There are a few references to singledispatch in the stdlib: https://github.com/python/cpython/search?q=singledispatch. Given the presented use cases, I don't think the extensibility of @singledispatch is not worth the performance cost for json.load. https://www.python.org/dev/peps/pep-0622/#performance-considerations : programming languages is rife with examples of new features which increased engineer productivity at the expense of additional CPU cycles, it would be unfortunate if the benefits of match were counter-balanced by a significant overall decrease in runtime performance.
Although this PEP does not specify any particular implementation
strategy, a few words about the prototype implementation and how it attempts to maximize performance are in order.
Basically, the prototype implementation transforms all of the match
statement syntax into equivalent if/else blocks - or more accurately, into Python byte codes that have the same effect. In other words, all of the logic for testing instance types, sequence lengths, mapping keys and so on are inlined in place of the match.
This is not the only possible strategy, nor is it necessarily the best.
For example, the instance checks could be memoized, especially if there are multiple instances of the same class type but with different arguments in a single match statement. It is also theoretically possible for a future implementation to process case clauses or sub-patterns in parallel using a decision tree rather than testing them one by one.