Skip to content

Commit 153c913

Browse files
mykaulLorak-mmk
authored andcommitted
(improvement) cqltypes: fast-path lookup_casstype() for simple type names
Skip the regex scanner and stack-based parser in parse_casstype_args() when the type string has no parentheses. For simple types like 'AsciiType' or 'org.apache.cassandra.db.marshal.FloatType', go directly to lookup_casstype_simple() which is just a prefix strip + dict lookup. This avoids re.Scanner, re.split on ':' / '=>', int() try/except, and list-of-lists stack manipulation for the common case of non-parameterized types. Signed-off-by: Yaniv Kaul <yaniv.kaul@scylladb.com>
1 parent fec90ae commit 153c913

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

cassandra/cqltypes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ def lookup_casstype(casstype):
249249
"""
250250
if isinstance(casstype, (CassandraType, CassandraTypeType)):
251251
return casstype
252+
if '(' not in casstype:
253+
return lookup_casstype_simple(casstype)
252254
try:
253255
return parse_casstype_args(casstype)
254256
except (ValueError, AssertionError, IndexError) as e:

tests/unit/test_types.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ def test_lookup_casstype(self):
120120

121121
assert str(lookup_casstype('unknown')) == str(cassandra.cqltypes.mkUnrecognizedType('unknown'))
122122

123-
with pytest.raises(ValueError):
124-
lookup_casstype('AsciiType~')
123+
# With the fast-path for simple type names (no parens), malformed names
124+
# like 'AsciiType~' create unrecognized types instead of raising ValueError
125+
assert str(lookup_casstype('AsciiType~')) == str(cassandra.cqltypes.mkUnrecognizedType('AsciiType~'))
125126

126127
def test_casstype_parameterized(self):
127128
assert LongType.cass_parameterized_type_with(()) == 'LongType'

0 commit comments

Comments
 (0)