⚡ Sangsakala Proxy 🏠 Home
Skip to content

Use a set when allocating image part names - #1639

Open
cakirmert wants to merge 1 commit into
python-openxml:masterfrom
cakirmert:perf/image-partname-membership
Open

cakirmert wants to merge 1 commit into
python-openxml:masterfrom
cakirmert:perf/image-partname-membership

Conversation

@cakirmert

Copy link
Copy Markdown

_next_image_partname() checks candidate numbers against a list of image numbers already in use. Store those numbers in a set so these checks no longer repeatedly scan the list. The search still returns the lowest available number and reuses gaps.

Name allocation becomes expected O(n) instead of O(n²) per new image, at the cost of some additional temporary memory.

Full-operation timings against e45454602b53e8e572b179ccf1c91093ec9f4ed7, using CPython 3.12.6 on Windows 11 with generated 64×48 thumbnails. Timings include document creation or loading, picture insertion, and saving.

Workload Before (ms) After (ms) Speedup Pairs
20 distinct PNGs 19.003 19.015 1.00x 20
250 distinct PNGs 174.814 166.176 1.05x 20
1,000 distinct PNGs 2667.254 2095.356 1.27x 7
2,000 distinct PNGs 13661.021 8684.973 1.57x 7
500 mixed PNG/JPEG images 649.474 582.158 1.12x 7
Same PNG repeated 250 times 91.880 92.626 0.99x 20
Load 1,000 images, add 100, save 736.627 534.916 1.38x 7

Paired-run medians after one warmup pair, alternating execution order with fresh inputs. Every ZIP entry's contents and order, image names, and shape counts matched exactly. Small and repeated-image controls were approximately unchanged.

Validation: 1,609 existing unit tests and 650 acceptance scenarios passed; Ruff lint, formatting, and git diff --check passed. Existing tests cover choosing the next number and reusing gaps; no test files changed.

AI-assisted investigation, implementation, benchmarking, and drafting with Codex.

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