Skip to content

Optimize int encode with direct PyLong digit read#107

Merged
MarshalX merged 1 commit into
mainfrom
optimize-pylong-int
May 26, 2026
Merged

Optimize int encode with direct PyLong digit read#107
MarshalX merged 1 commit into
mainfrom
optimize-pylong-int

Conversation

@MarshalX

@MarshalX MarshalX commented May 26, 2026

Copy link
Copy Markdown
Owner

Adds a CPython 3.12+ fast path in encode_int that reads PyLongObject.lv_tag + ob_digit[0..2] directly to encode 0/1/2-digit ints (≤ 2**60), skipping obj.extract::<i128>() which internally calls _PyLong_AsByteArray with a 16-byte buffer + sign check on every int

Ints requiring 3+ digits, pre-3.12, and non-CPython fall through to the existing i128 path, which still enforces the +-2**64 DAG-CBOR range

@codspeed-hq

codspeed-hq Bot commented May 26, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 12.33%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 191 untouched benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation test_dag_cbor_encode_real_data[citm_catalog.json] 8.1 ms 7.2 ms +12.33%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing optimize-pylong-int (87d86d2) with main (d2eaf4d)

Open in CodSpeed

@MarshalX

MarshalX commented May 26, 2026

Copy link
Copy Markdown
Owner Author

citm +12% aligns with my local tests; also there are github +4% and twitter +4.6%

@MarshalX MarshalX merged commit 05883cf into main May 26, 2026
5 checks passed
@MarshalX MarshalX deleted the optimize-pylong-int branch May 26, 2026 16:04
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