Skip to content

Commit 0724e2a

Browse files
committed
Annotate global constants as Final to speed up compiled code
1 parent 149547d commit 0724e2a

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/tomli/_parser.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@
3232
# lower number than where mypyc binaries crash.
3333
MAX_INLINE_NESTING: Final = sys.getrecursionlimit()
3434

35-
ASCII_CTRL = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))
35+
ASCII_CTRL: Final = frozenset(chr(i) for i in range(32)) | frozenset(chr(127))
3636

3737
# Neither of these sets include quotation mark or backslash. They are
3838
# currently handled as separate cases in the parser functions.
39-
ILLEGAL_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t")
40-
ILLEGAL_MULTILINE_BASIC_STR_CHARS = ASCII_CTRL - frozenset("\t\n")
39+
ILLEGAL_BASIC_STR_CHARS: Final = ASCII_CTRL - frozenset("\t")
40+
ILLEGAL_MULTILINE_BASIC_STR_CHARS: Final = ASCII_CTRL - frozenset("\t\n")
4141

42-
ILLEGAL_LITERAL_STR_CHARS = ILLEGAL_BASIC_STR_CHARS
43-
ILLEGAL_MULTILINE_LITERAL_STR_CHARS = ILLEGAL_MULTILINE_BASIC_STR_CHARS
42+
ILLEGAL_LITERAL_STR_CHARS: Final = ILLEGAL_BASIC_STR_CHARS
43+
ILLEGAL_MULTILINE_LITERAL_STR_CHARS: Final = ILLEGAL_MULTILINE_BASIC_STR_CHARS
4444

45-
ILLEGAL_COMMENT_CHARS = ILLEGAL_BASIC_STR_CHARS
45+
ILLEGAL_COMMENT_CHARS: Final = ILLEGAL_BASIC_STR_CHARS
4646

47-
TOML_WS = frozenset(" \t")
48-
TOML_WS_AND_NEWLINE = TOML_WS | frozenset("\n")
49-
BARE_KEY_CHARS = frozenset(string.ascii_letters + string.digits + "-_")
50-
KEY_INITIAL_CHARS = BARE_KEY_CHARS | frozenset("\"'")
51-
HEXDIGIT_CHARS = frozenset(string.hexdigits)
47+
TOML_WS: Final = frozenset(" \t")
48+
TOML_WS_AND_NEWLINE: Final = TOML_WS | frozenset("\n")
49+
BARE_KEY_CHARS: Final = frozenset(string.ascii_letters + string.digits + "-_")
50+
KEY_INITIAL_CHARS: Final = BARE_KEY_CHARS | frozenset("\"'")
51+
HEXDIGIT_CHARS: Final = frozenset(string.hexdigits)
5252

53-
BASIC_STR_ESCAPE_REPLACEMENTS = MappingProxyType(
53+
BASIC_STR_ESCAPE_REPLACEMENTS: Final = MappingProxyType(
5454
{
5555
"\\b": "\u0008", # backspace
5656
"\\t": "\u0009", # tab

src/tomli/_re.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
from datetime import date, datetime, time, timedelta, timezone, tzinfo
88
from functools import lru_cache
99
import re
10-
from typing import Any
10+
from typing import Any, Final
1111

1212
from ._types import ParseFloat
1313

1414
# E.g.
1515
# - 00:32:00.999999
1616
# - 00:32:00
17-
_TIME_RE_STR = r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?"
17+
_TIME_RE_STR: Final = (
18+
r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?"
19+
)
1820

19-
RE_NUMBER = re.compile(
21+
RE_NUMBER: Final = re.compile(
2022
r"""
2123
0
2224
(?:
@@ -35,8 +37,8 @@
3537
""",
3638
flags=re.VERBOSE,
3739
)
38-
RE_LOCALTIME = re.compile(_TIME_RE_STR)
39-
RE_DATETIME = re.compile(
40+
RE_LOCALTIME: Final = re.compile(_TIME_RE_STR)
41+
RE_DATETIME: Final = re.compile(
4042
rf"""
4143
([0-9]{{4}})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) # date, e.g. 1988-10-27
4244
(?:

0 commit comments

Comments
 (0)