Commit a59b863
committed
gh-148762: speed up SRE_AT_BEGINNING_LINE regexes
`SRE(search)` has an early exit for `SRE_AT_BEGINNING` and
`SRE_AT_BEGINNING_STRING`, but lacks fast-forward for
`SRE_AT_BEGINNING_LINE`. This means that a regex of the following form
is slow:
```
re.compile("^foo", re.MULTILINE)
```
The current implementation does a character-by-character loop that calls
`SRE(match)` each time. This is rather expensive function call.
This commit
* ensures `SRE(match)` is only called right after a newline
* optimizes fast-forwarding to the next newline by calling `memchr` in
the UCS1 case
This can lead to 10x or even 100x speedups in the no-match case with
long lines, while not causing overhead in the case of short lines.
Signed-off-by: Harmen Stoppels <harmenstoppels@gmail.com>1 parent 7ce737e commit a59b863
File tree
2 files changed
+37
-6
lines changed- Misc/NEWS.d/next/Library
- Modules/_sre
2 files changed
+37
-6
lines changedLines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1854 | 1854 | | |
1855 | 1855 | | |
1856 | 1856 | | |
1857 | | - | |
1858 | | - | |
1859 | | - | |
1860 | | - | |
1861 | | - | |
1862 | | - | |
| 1857 | + | |
| 1858 | + | |
| 1859 | + | |
| 1860 | + | |
| 1861 | + | |
| 1862 | + | |
| 1863 | + | |
| 1864 | + | |
| 1865 | + | |
| 1866 | + | |
| 1867 | + | |
| 1868 | + | |
| 1869 | + | |
| 1870 | + | |
| 1871 | + | |
| 1872 | + | |
| 1873 | + | |
| 1874 | + | |
| 1875 | + | |
| 1876 | + | |
| 1877 | + | |
| 1878 | + | |
| 1879 | + | |
| 1880 | + | |
| 1881 | + | |
| 1882 | + | |
| 1883 | + | |
| 1884 | + | |
| 1885 | + | |
| 1886 | + | |
| 1887 | + | |
| 1888 | + | |
| 1889 | + | |
| 1890 | + | |
| 1891 | + | |
1863 | 1892 | | |
1864 | 1893 | | |
1865 | 1894 | | |
| |||
0 commit comments