Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

perf: optimize Python to QPDF object conversion#711

Open
qooxzuub wants to merge 1 commit intopikepdf:mainfrom
qooxzuub:fix/optimize-dict-creation
Open

perf: optimize Python to QPDF object conversion#711
qooxzuub wants to merge 1 commit intopikepdf:mainfrom
qooxzuub:fix/optimize-dict-creation

Conversation

@qooxzuub
Copy link
Contributor

This change improves the performance of creating PDF objects from Python types, specifically within dictionary and array builders. Benchmarks show a reduction in dictionary creation time from ~4.2s to ~1.7s (~60% speedup).

Changes:

  • Replaced exception-based casting in objecthandle_encode with isinstance-based dispatching to avoid high catch overhead.
  • Implemented heap-allocated static caches for the 'decimal' module classes and functions to eliminate redundant Python imports and attribute lookups.
  • Used a "managed leak" pattern for static Python objects to prevent segmentation faults during interpreter shutdown.
  • Reordered type checks to prioritize integers and booleans.
  • Added vector::reserve to array_builder to minimize reallocations.
  • Switched to map::emplace in dict_builder for more efficient key-value insertion.

@qooxzuub
Copy link
ContributorAuthor

"Benchmark" results:

$ hyperfine 'python bench_dict.py'Benchmark 1: python bench_dict.py   Time (mean ± σ):      1.702 s ±  0.050 s    [User: 1.682 s, System: 0.018 s]  Range (min … max):    1.638 s …  1.811 s    10 runs$ cp ../_core.cpython-310-x86_64-linux-gnu.BASELINE.so src/pikepdf/_core.cpython-310-x86_64-linux-gnu.so $ hyperfine 'python bench_dict.py'Benchmark 1: python bench_dict.py   Time (mean ± σ):      4.330 s ±  0.084 s    [User: 4.307 s, System: 0.019 s]  Range (min … max):    4.197 s …  4.496 s    10 runs$ cat bench_dict.pyimport pikepdfimport timeitimport decimalfrom pikepdf import Dictionary, Nametest_data = {    "/Type": Name.Page,    "/Count": 42,    "/Label": "Chapter 1",    "/Score": decimal.Decimal("99.99"),    "/Version": 1,}def create_dicts():    for _ in range(10000):        d = Dictionary(test_data)if __name__ == "__main__":    t = timeit.timeit(create_dicts, number=5)    print(f"Time for 50,000 dictionary creations: {t:.4f}s")

@qooxzuubqooxzuub marked this pull request as draftFebruary 4, 2026 15:31
Refactor objecthandle_encode to use type dispatch via Py_TYPEpointer comparisons, replacing py::isinstance templates.- Implement fast-path string encoding via PyUnicode_AsUTF8AndSize.- Add support for decimal.Decimal with configurable precision via  get_decimal_precision().- Optimize array_builder with pre-allocation via result.reserve().- Use leaky static singletons for decimal module attributes to avoid  repeated imports and interpreter shutdown races.- Restore consistent ValueError strings for non-finite floats.
@qooxzuubqooxzuubforce-pushed thefix/optimize-dict-creation branch froma622eb7 to43f750dCompareFebruary 4, 2026 17:15
@qooxzuubqooxzuub marked this pull request as ready for reviewFebruary 4, 2026 17:16
@codecov
Copy link

codecovbot commentedFeb 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.64%. Comparing base (dbcee73) to head (43f750d).

Additional details and impacted files
@@            Coverage Diff             @@##             main     #711      +/-   ##==========================================- Coverage   93.64%   93.64%   -0.01%==========================================  Files          60       60                Lines        6938     6936       -2       Branches      391      393       +2     ==========================================- Hits         6497     6495       -2  Misses        441      441

☔ View full report in Codecov by Sentry.
📢 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.

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

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@qooxzuub@web-flow

Comments


[8]ページ先頭

©2009-2026 Movatter.jp