Skip to content

Speed up reading short length-coded strings - #1296

Open
g4lb wants to merge 2 commits into
PyMySQL:mainfrom
g4lb:perf-length-coded-string
Open

g4lb wants to merge 2 commits into
PyMySQL:mainfrom
g4lb:perf-length-coded-string

Conversation

@g4lb

@g4lb g4lb commented Nov 17, 2026 •

Copy link
Copy Markdown

read_length_coded_string() reads each column value and each field name. When the value is shorter than 251 bytes, it now reads the value with one slice. NULL and longer values do not call read_length_encoded_integer() now. The output and the exceptions do not change.

Benchmark (CPython 3.15) Before After Change
2000 rows, 5 INT columns 2290 µs 1861 µs -18.7%
300 rows, 20 mixed columns 2135 µs 1840 µs -13.8%
100 field descriptors 149 µs 119 µs -20.1%
500 rows, 20 NULL columns 749 µs 664 µs -11.4%

I measured no slowdown on CPython 3.9 and 3.15 for NULL, 251-byte, or 64 KB values. I used 20 interleaved rounds and a Mann-Whitney test (Apple M5). On PyPy 3.11, rows do not change, but a loop of only NULL reads is 5% slower. I did not measure other architectures.

Checks:

  • ruff format --check and ruff check pass.
  • The tests that do not need a server pass. I did not run the server tests.
  • pymysql/tests/test_protocol.py covers each length prefix, NULL, and truncated values.
  • Old and new outputs and exceptions matched on 1,000,000 random inputs. The inputs include result sets and truncated packets.

Summary by CodeRabbit

  • Bug Fixes
    • Improved parsing of length-coded values in database responses. Payloads are now read only when the complete value is available, and null markers continue to be handled correctly. This helps prevent incomplete packet data from being treated as a complete value.
    • Added support for handling empty values and length prefixes of different sizes, with truncated payloads rejected rather than partially read.

@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The length-coded string parser now reads prefixes directly from the packet buffer. Tests cover prefix types, decoded values, cursor positions, and truncated payloads.

Changes: Length-coded string parsing

Layer / File(s) Summary
Parse length-coded string prefixes
pymysql/protocol.py, pymysql/tests/test_protocol.py
read_length_coded_string reads short lengths with bounds-checked slicing, returns None for null and invalid markers, and uses integer readers for extended-length markers. Tests check results, packet positions, and truncated payloads.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: methane

Merge Risk: Minimal · up to 87151

This is a small performance optimization to length-coded string parsing, and the output and exceptions are reported unchanged. Tests cover each prefix type, NULL, and truncated input. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "Speed up reading short length-coded strings" matches the main change. The PR changes read_length_coded_string() so that short values (below 251 bytes) use one bounds-checked slice and avoid…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "Speed up reading short length-coded strings" matches the main change. The PR changes read_length_coded_string() so that short values (below 251 bytes) use one bounds-checked slice and avoid calling read_length_encoded_integer() for NULL and longer values. The objectives report benchmark gains for these reads. The title is specific, concise, and describes the primary change from the developer's perspective.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e2204d2b-b402-4188-9897-9b9d5beb2a1a
Commits

Reviewing files that changed from the base of the PR and between c1d4c45 and 87151c0.

Files selected for processing: 1

  • pymysql/tests/test_protocol.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.22222% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.51%. Comparing base (1e28be8) to head (c1d4c45).
⚠️ Report is 90 commits behind head on main.

Files with missing lines Patch % Lines
pymysql/protocol.py 72.22% 2 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1296      +/-   ##
==========================================
- Coverage   86.24%   84.51%   -1.74%     
==========================================
  Files          17       17              
  Lines        2436     2512      +76     
  Branches      258      262       +4     
==========================================
+ Hits         2101     2123      +22     
- Misses        249      317      +68     
+ Partials       86       72      -14     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant