Issue 941071: Replace if/elif chain with dispatch pattern in sre_compile (original) (raw)

This issue has been migrated to GitHub: https://github.com/python/cpython/issues/40183

classification

Title: Replace if/elif chain with dispatch pattern in sre_compile
Type: Stage:
Components: None Versions:

process

Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: niemeyer Nosy List: loewis, niemeyer, rhettinger
Priority: normal Keywords: patch

Created on 2004-04-24 00:07 by rhettinger, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
sre.diff rhettinger,2004-04-24 00:07 Diff for Lib/sre_compile.py
Messages (5)
msg45821 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-04-24 00:07
Use a dictionary to implement switch/case sematics instead of a long if/elif chain.
msg45822 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2004-04-28 20:35
Logged In: YES user_id=21627 Gustavo, what do you think?
msg45823 - (view) Author: Gustavo Niemeyer (niemeyer) * (Python committer) Date: 2004-04-28 21:36
Logged In: YES user_id=7887 Raymond, have you done any performance tests showing that the proposed scheme is better? I've created a small test bed for the two patterns here and it looks like the current pattern is faster for small sets. Given the fact that this is a recursive pattern, and that I'm unsure about the kind of patterns which are most used, I'm a little bit uncomfortable in applying this.
msg45824 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-04-28 22:39
Logged In: YES user_id=80475 It needs to be timed on very large patterns -- it will always do worse on very small patterns (due to the time to setup the dispatch table). I'm not satisfied with the patch the way it stands. Ideally, the dispatch table needs to be pre-computed at compile time. The trick is how to view the enclosing variables (passing them as arguments is slow; using globals is not especially clean or fast). If that were resolved, then this approach would just about always be faster (a dictioary lookup and function call versus a long chain of elifs). Any suggestions are welcome.
msg45825 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-05-04 05:42
Logged In: YES user_id=80475 I can't see any way to fix the long if/elif chain without introducing switch/case. The dispatch idiom is too expensive in terms of function call overhead and data sharing costs.
History
Date User Action Args
2022-04-11 14:56:03 admin set github: 40183
2004-04-24 00:07:25 rhettinger create