This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Mark.Shannon
Recipients AndersMunch, Dennis Sweeney, Kshitiz17, Mark.Shannon, brandtbucher, corona10, kj, rhettinger, serhiy.storchaka
Date 2021-06-14.13:45:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1623678333.99.0.244961042917.issue44283@roundup.psfhosted.org>
In-reply-to
Content
This is going in the wrong direction.

Rather than add more complex instructions for use only by pattern matching, we need to simplify the individual operations and re-use existing instructions.
That way pattern matching can benefit from the general performance improvements that we are making.


If you are serious about improving the performance of pattern matching, you need to do the following in the compiler:
1. Generate a decision tree.
2. Improve that decision tree so that fewer decisions are required.
3. Generate bytecode from that tree that uses lower level operations.

E.g.

match x:
    case [1]:
        A
    case [2]:
        B

The initial decision tree looks like:

if is_sequence(x):
    if len(x) == 1:
        if x[0] == 1:
            A
if is_sequence(x):
    if len(x) == 1:
        if x[0] == 2:
            B

which can be improved to:

if is_sequence(x):
    if len(x) == 1:
        if x[0] == 1:
            A
        elif x[0] == 2:
            B

For a sequence of integer constants, introducing the test `type(x) == int` at the start would allow you to convert the linear sequence of tests into a tree. Reducing `n` tests to `ln(n) + 1` tests.
History
Date User Action Args
2021-06-14 13:45:34Mark.Shannonsetrecipients: + Mark.Shannon, rhettinger, serhiy.storchaka, corona10, brandtbucher, Dennis Sweeney, AndersMunch, kj, Kshitiz17
2021-06-14 13:45:33Mark.Shannonsetmessageid: <1623678333.99.0.244961042917.issue44283@roundup.psfhosted.org>
2021-06-14 13:45:33Mark.Shannonlinkissue44283 messages
2021-06-14 13:45:33Mark.Shannoncreate