Skip to content

perf(serialize_value): return built-in scalars early -- 62% less time - #1069

Merged
vgrem merged 1 commit into
vgrem:masterfrom
dexhunter:perf/scalar-serialization-early-return
Nov 7, 2026
Merged

vgrem merged 1 commit into
vgrem:masterfrom
dexhunter:perf/scalar-serialization-early-return

Conversation

@dexhunter

@dexhunter dexhunter commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR

  • Speed up serialize_value for payloads with many plain scalar values.
  • Return None and exact str, int, bool and float values before the conversion checks.
  • Serialization benchmark: 389.3 ms → 146.1 ms, 62% less time.
  • All 9 serialization tests pass; the full offline suite also passes.

What & Why

Before this change, every plain scalar went through all conversion checks before it came back unchanged.
These checks include the enum, UUID, bytes, date and to_json checks.
Most list item and entity payloads hold plain scalars, so this extra work adds up.

The function now returns None and exact built-in scalars at the top.
The check uses type(value) is ..., so subclasses still go through the existing checks.
Enums such as IntEnum and scalar subclasses with a to_json hook keep their output.
The rest of the function does not change.

Validation

Measured with CPython 3.12.13 on Linux x86_64, on a dedicated CPU core.
Each row is the median of five alternating runs of both revisions.
Each run takes the best of nine timings.

Situation Before After Change
2,000 list rows, 8 plain scalar fields each 5.83 ms 1.97 ms 66% less time
200 nested items: dicts with lists of small dicts 5.05 ms 2.66 ms 47% less time
2,000 enums, datetimes and UUIDs plus 200 EmailAddress models 1.66 ms 1.63 ms no clear change
Mixed scalar cells, models, enum formats and error cases 389.3 ms 146.1 ms 62% less time

The enum and model row gets almost no gain, because those values still need their conversion.
The 1.6% difference there is inside the spread between runs: before 1.63–1.68 ms, after 1.63–1.64 ms.
No row is slower.

The last row is a larger mixed benchmark, with the median of three runs.
Run this quick check on both revisions to compare the scalar case:

uv run python -m timeit -s 'from office365.runtime.converters.value import serialize_value; rows = [{"Title": "Task", "Id": i, "Done": True, "Owner": None, "Score": 0.5} for i in range(2000)]' 'serialize_value(rows)'

Serialization tests (9 passed):

uv run pytest -q tests/unit/test_converters.py::TestSerializeValue tests/unit/test_converters.py::TestClientValueToJson tests/unit/test_converters.py::TestClientObjectToJson tests/unit/test_serialization_contract.py

uv run pytest -q reports 526 passed, 983 skipped and 133 subtests passed.
uv run ruff check . and uv run ruff format --check . pass with Ruff 0.16.10.

Why this is safe and easy to maintain

The change adds five lines at the top of one function and touches nothing else.
Exact type checks keep subclass handling and the order of conversion hooks.
The serialization tests check models, enum formats, collections and round trips, and the output is the same.

Summary

Only office365/runtime/converters/value.py changes.

Type of change

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

Checklist

  • uv run ruff check . and uv run ruff format --check . pass
  • 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/FjHsRkiqK50WpX0uKtlM9OTLzpBSUtAb

Return None and exact str, int, bool and float values at the top of
serialize_value, before the enum, UUID, bytes, date and to_json checks.
Subclasses still use the existing checks, so output does not change.
The rest of the function is unchanged.

Co-Authored-By: Aiden <[email protected]>
@vgrem
vgrem merged commit e09ed6d into vgrem:master Nov 7, 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