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.

classification
Title: Optimize mapping patterns of structural pattern matching
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brandtbucher, corona10, kj, tim.peters
Priority: normal Keywords: patch

Created on 2021-08-29 15:56 by corona10, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
bench_pattern.py corona10, 2021-08-29 15:56
Pull Requests
URL Status Linked Edit
PR 28043 merged corona10, 2021-08-29 15:58
Messages (7)
msg400549 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-08-29 15:56
There are optimizable points that can be achieved by removing unnecessary tuple transformation and using vector calling convention.

+---------------+--------+----------------------+
| Benchmark     | base   | opt                  |
+===============+========+======================+
| bench pattern | 482 ns | 417 ns: 1.15x faster |
+---------------+--------+----------------------+
msg400567 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-08-30 00:11
With Ken Jin's suggestion

+---------------+--------+----------------------+
| Benchmark     | base   | PR 28043           |
+===============+========+======================+
| bench pattern | 482 ns | 373 ns: 1.29x faster |
+---------------+--------+----------------------+
msg400585 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-08-30 10:02
New changeset e6497fe698f6e87344501a68ffdea106eafcb257 by Dong-hee Na in branch 'main':
bpo-45045: Optimize mapping patterns of structural pattern matching (GH-28043)
https://github.com/python/cpython/commit/e6497fe698f6e87344501a68ffdea106eafcb257
msg400586 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-08-30 10:05
For the record,

Final benchmark with optimization build + thin LTO option.

+---------------+---------------+----------------------+
| Benchmark     | thin_lto_base | thin_lto_opt         |
+===============+===============+======================+
| bench pattern | 357 ns        | 287 ns: 1.24x faster |
+---------------+---------------+----------------------+
msg400625 - (view) Author: Brandt Bucher (brandtbucher) * (Python committer) Date: 2021-08-30 15:38
Thanks, this is awesome.

FYI, there's probably more low-hanging fruit like this scattered about the pattern matching implementation. It's all very new code, mostly written by one person ;).

(I wouldn't worry too much about the pattern compiler, though... I'm currently finishing up a total rewrite of that!)
msg400626 - (view) Author: Brandt Bucher (brandtbucher) * (Python committer) Date: 2021-08-30 15:42
I'm also in the process of creating some pattern matching benchmarks. You might find them useful for benching optimizations like this in the future:

https://github.com/brandtbucher/patmaperformance

In particular, I'm curious to see the impact of this change on "patma_holdem". I bet it's quite good.
msg400630 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-08-30 16:09
> https://github.com/brandtbucher/patmaperformance

Nice benchmark suite, I will take a look :)
History
Date User Action Args
2022-04-11 14:59:49adminsetgithub: 89208
2021-08-30 16:09:02corona10setmessages: + msg400630
2021-08-30 15:42:06brandtbuchersetmessages: + msg400626
2021-08-30 15:38:54brandtbuchersetmessages: + msg400625
2021-08-30 10:05:18corona10setstatus: open -> closed
stage: patch review -> resolved
2021-08-30 10:05:02corona10setmessages: + msg400586
2021-08-30 10:02:42corona10setmessages: + msg400585
2021-08-30 00:34:05tim.peterssetfiles: - runstack.py
2021-08-30 00:33:43tim.peterssetmessages: - msg400568
2021-08-30 00:33:16tim.peterssetfiles: + runstack.py
nosy: + tim.peters
messages: + msg400568

2021-08-30 00:11:14corona10setnosy: + kj
messages: + msg400567
2021-08-29 16:08:08corona10setnosy: + brandtbucher
2021-08-29 15:58:53corona10setkeywords: + patch
stage: patch review
pull_requests: + pull_request26488
2021-08-29 15:56:08corona10create