From 809a8ae8795263036ac2f350a81b6e0e38fd03dd Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Mon, 15 Nov 2021 03:30:25 -0500 Subject: [PATCH] Slightly speed up execution time of common cases (#142) --- tomli/_parser.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tomli/_parser.py b/tomli/_parser.py index 89e81c3..ccd6b92 100644 --- a/tomli/_parser.py +++ b/tomli/_parser.py @@ -584,6 +584,8 @@ def parse_value( # noqa: C901 except IndexError: char = None + # IMPORTANT: order conditions based on speed of checking and likelihood + # Basic strings if char == '"': if src.startswith('"""', pos): @@ -604,6 +606,14 @@ def parse_value( # noqa: C901 if src.startswith("false", pos): return pos + 5, False + # Arrays + if char == "[": + return parse_array(src, pos, parse_float) + + # Inline tables + if char == "{": + return parse_inline_table(src, pos, parse_float) + # Dates and times datetime_match = RE_DATETIME.match(src, pos) if datetime_match: @@ -623,14 +633,6 @@ def parse_value( # noqa: C901 if number_match: return number_match.end(), match_to_number(number_match, parse_float) - # Arrays - if char == "[": - return parse_array(src, pos, parse_float) - - # Inline tables - if char == "{": - return parse_inline_table(src, pos, parse_float) - # Special floats first_three = src[pos : pos + 3] if first_three in {"inf", "nan"}: