Skip to content

perf(paths): skip unused string replacements -- 17.8% less time - #1068

Merged
vgrem merged 1 commit into
vgrem:masterfrom
dexhunter:perf/odata-string-encoding
Nov 6, 2026
Merged

vgrem merged 1 commit into
vgrem:masterfrom
dexhunter:perf/odata-string-encoding

Conversation

@dexhunter

@dexhunter dexhunter commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR

  • Speed up _encode_string when building OData file paths.
  • Skip absent characters and select the replacement method once.
  • Mixed path benchmark: 140.94 ms -> 115.88 ms, 17.8% less time.
  • uv run pytest -q tests/unit/test_runtime_paths.py: 11 passed.

What & Why

The old loop called replace for all seven escape characters, even when a path contained none of them.
It also read every replacement value from the mapping.

The shared loop now reads replacement values only after finding a match in the original plain string.
String subclasses still receive every method call in the original order.
Percent escaping stays first, and apostrophes are doubled once.

Validation

Measured on Linux with CPython 3.12.13. The table gives medians of three runs.
The mixed batch encodes 6,000 file paths and service-operation cases, repeated 24 times per sample.
File paths include 60% ordinary names and 40% names requiring escaping.

Situation Before After Change
Mixed OData path batch 140.94 ms 115.88 ms 17.8% less time
Synchronous loopback requests, per request 18.75 us 18.85 us +0.5%, no material change
Async loopback requests, per request 92.36 us 93.94 us +1.7%, no material change
Async requests with inline setup, per request 18.64 us 18.70 us +0.3%, no material change

The request cases do not encode string arguments, so they show no benefit from this change.
Their small timing shifts do not show a material slowdown.
Each request run uses the best of five repetitions; baseline and changed versions alternate order.
Reproduce these control measurements on each revision with:

PYTHONPATH=. uv run python scripts/bench_async_overhead.py --requests 2000 --repeat 5

The full offline suite also passed:

uv run pytest -q
# 526 passed, 983 skipped, 133 subtests passed

Ruff 0.16.10 lint and formatting checks passed across the repository.
Pyright reported 11 errors and one warning, identical to baseline; none are in the changed helper.
A separate comparison matched 5,689 outputs, exception results, and string-subclass call sequences.

Why this is safe and easy to maintain

The change keeps one short replacement loop and the existing character mapping.
It preserves escaped values, result types, and custom string behavior without adding dependencies or caches.
Existing path tests cover URL construction and OData callers, while additional comparisons cover punctuation and overridden string methods.

Summary

Scope is limited to ODataPathBuilder._encode_string in office365/runtime/paths/builder.py.

Type of change

  • Bug fix
  • New feature
  • Documentation / examples
  • Refactor / internal

Checklist

  • uv run ruff check . and uv run ruff format --check . pass (run with uvx --from ruff==0.16.10).
  • uv run pyright is clean.
  • uv run pytest -q passes (offline by default).
  • uv run mkdocs build --strict passes (if docs/examples changed).
  • Added/updated tests under tests/unit where practical.
  • Linked the related issue.

AI assistance

This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.

Weco share link: https://dashboard.weco.ai/share/qP3vOqPobI6vbPDbhPvNtLo-EfUoKuNn

Skip absent escape characters and select the replacement operation once.
Keep one shared loop, percent-first escaping, and string-subclass calls.

Native path tests: 11 passed. Full offline suite: 526 passed, 983 skipped,
and 133 subtests passed. Mixed path time fell from 140.94 ms to 115.88 ms.

Co-Authored-By: Aiden <[email protected]>
@vgrem
vgrem merged commit 4ad616a into vgrem:master Nov 6, 2026
4 of 8 checks passed
vgrem added a commit that referenced this pull request Nov 7, 2026
…author

The Unreleased section ran 1,463 lines for 175 changes. Each bullet carried the
mechanism — which module was folded into which, which helper replaced which — so
a reader looking for "what changes for me" had to reconstruct the answer from the
commit that made it.

Nothing was dropped that a caller can observe. What went is the narration of how
the change was reached: the second design, the internal seam that reappeared, the
count of lines deleted. What stayed is the fact, the one clause of why it matters,
and the names a caller types. 175 bullets became 149, and the four permissions
sections read as what they are: one declaration surface, counted once.

Four merged PRs had no line at all — the serialization, path-building, timestamp
and progress fast paths (#1069, #1068, #1067, #1066), each now stated as the
number it buys. The page payload fix (0117ecc) is stated too, because it is the
answer to a reported 2019-farm refusal: an emptied nested value is no longer sent.
The single-quote escape (#1053) is not here — it shipped in 3.3.0, which already
records it.
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.

2 participants