Skip to content

Commit a2f97d9

Browse files
committed
refactor: optimize row parsing with pre-compiled structs and reduced attribute lookups
1 parent 15c3fb4 commit a2f97d9

File tree

3 files changed

+286
-252
lines changed

3 files changed

+286
-252
lines changed

mariadb/impl/client/async_client.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -475,14 +475,17 @@ async def execute_many(self, messages: List[ClientMessage], config: 'Configurati
475475

476476
async def _read_result(self, is_binary: bool, config: 'Configuration' = None, buffered: bool = True, prepare_stmt_packet: Optional[PrepareStmtPacket] = None) -> List[Completion]:
477477
results = []
478-
478+
context = self.context
479+
ok_packet = self.OK_PACKET
480+
error_packet = self.ERROR_PACKET
479481

480482
while True:
481483
packet = await self.read_payload()
482-
if packet[0] == self.OK_PACKET:
483-
results.append(OkPacket.decode(packet, self.context))
484-
elif packet[0] == self.ERROR_PACKET:
485-
raise ErrorPacket.decode(packet, self.context).toError(self.exception_factory)
484+
packet_type = packet[0]
485+
if packet_type == ok_packet:
486+
results.append(OkPacket.decode(packet, context))
487+
elif packet_type == error_packet:
488+
raise ErrorPacket.decode(packet, context).toError(self.exception_factory)
486489
else:
487490
"""Parse result set with column definitions and row data"""
488491
# Parse column count from first packet
@@ -491,17 +494,17 @@ async def _read_result(self, is_binary: bool, config: 'Configuration' = None, bu
491494

492495
# Read column definitions
493496
columns: List[ColumnDefinitionPacket] = [None] * column_count
494-
if self.context.has_capability(constants.CAPABILITY.CACHE_METDATA) and parser.read_byte() == 0:
497+
if context.has_capability(constants.CAPABILITY.CACHE_METDATA) and parser.read_byte() == 0:
495498
# skip metadata
496499
columns = prepare_stmt_packet.columns
497500
else:
498501
for i in range(column_count):
499502
col_packet = await self.read_payload()
500-
columns[i] = ColumnDefinitionPacket.decode(col_packet, self.context)
501-
if (prepare_stmt_packet is not None):
502-
prepare_stmt_packet.columns = columns
503+
columns[i] = ColumnDefinitionPacket.decode(col_packet, context)
504+
if prepare_stmt_packet is not None:
505+
prepare_stmt_packet.columns = columns
503506
# Read EOF packet after column definitions (if not deprecated)
504-
if not self.context.isEofDeprecated():
507+
if not context.isEofDeprecated():
505508
await self.read_payload() # Skip EOF packet
506509

507510
# Select appropriate row parser based on protocol
@@ -511,7 +514,7 @@ async def _read_result(self, is_binary: bool, config: 'Configuration' = None, bu
511514
if not buffered:
512515
from ..result import AsyncStreamingResult
513516
streaming_result = AsyncStreamingResult(self.read_payload,
514-
self.context,
517+
context,
515518
columns,
516519
column_count,
517520
config,
@@ -526,18 +529,20 @@ async def _read_result(self, is_binary: bool, config: 'Configuration' = None, bu
526529
else:
527530
# Read rows
528531
rows: List[tuple] = []
532+
eof_deprecated = context.isEofDeprecated()
533+
529534
while True:
530535
row_packet = await self.read_payload()
531536
# Check for EOF/OK packet based on DEPRECATE_EOF capability and packet length
532537
# EOF/OK packets start with 0xFE and have specific length constraints
533538
if (row_packet[0] == 0xFE and
534-
((self.context.isEofDeprecated() and len(row_packet) < 16777215) or
535-
(not self.context.isEofDeprecated() and len(row_packet) < 8))):
539+
((eof_deprecated and len(row_packet) < 16777215) or
540+
(not eof_deprecated and len(row_packet) < 8))):
536541

537-
if not self.context.isEofDeprecated():
538-
completion = EofPacket.decode(row_packet, self.context)
542+
if not eof_deprecated:
543+
completion = EofPacket.decode(row_packet, context)
539544
else:
540-
completion = OkPacket.decode(row_packet, self.context)
545+
completion = OkPacket.decode(row_packet, context)
541546

542547
# Apply converters to all rows at once
543548
rows = self._apply_converters_to_rows(rows, columns, config)
@@ -552,8 +557,8 @@ async def _read_result(self, is_binary: bool, config: 'Configuration' = None, bu
552557
completion.result_set = complete_result
553558
results.append(completion)
554559
break
555-
elif row_packet[0] == self.ERROR_PACKET:
556-
raise ErrorPacket.decode(row_packet, self.context).toError(self.exception_factory)
560+
elif row_packet[0] == error_packet:
561+
raise ErrorPacket.decode(row_packet, context).toError(self.exception_factory)
557562
else:
558563
rows.append(row_parser(row_packet, columns, config))
559564

0 commit comments

Comments
 (0)