Skip to content

fix(serialization/msgspec_hooks): avoid dict allocation in default_serializer when no custom type_encoders#4710

Open
Peopl3s wants to merge 1 commit into
litestar-org:mainfrom
Peopl3s:fix-4701
Open

fix(serialization/msgspec_hooks): avoid dict allocation in default_serializer when no custom type_encoders#4710
Peopl3s wants to merge 1 commit into
litestar-org:mainfrom
Peopl3s:fix-4701

Conversation

@Peopl3s

@Peopl3s Peopl3s commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Description

Avoid dict allocation in default_serializer when no custom type_encoders

Closes #4701


📚 Documentation preview 📚: https://litestar-org.github.io/litestar-docs-preview/4710

@codecov

codecov Bot commented Apr 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 67.38%. Comparing base (eb86bda) to head (f2bae4d).
⚠️ Report is 58 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4710      +/-   ##
==========================================
- Coverage   67.38%   67.38%   -0.01%     
==========================================
  Files         292      292              
  Lines       14995    14992       -3     
  Branches     1684     1685       +1     
==========================================
- Hits        10105    10102       -3     
  Misses       4755     4755              
  Partials      135      135              

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a hot path indeed, do you have a benchmark to prove your optimization? :)

@Peopl3s

Peopl3s commented Apr 20, 2026

Copy link
Copy Markdown
Contributor Author

@sobolevn Yeah, sure (I provided an example benchmark in the issue)

from __future__ import annotations

import timeit
from datetime import datetime
from typing import Any

from litestar.serialization.msgspec_hooks import DEFAULT_TYPE_ENCODERS

def default_serializer_before(value: Any, type_encoders=None) -> Any:
    type_encoders = {**DEFAULT_TYPE_ENCODERS, **(type_encoders or {})}
    for base in value.__class__.__mro__[:-1]:
        try:
            encoder = type_encoders[base]
        except KeyError:
            continue
        else:
            return encoder(value)
    raise TypeError(f"Unsupported type: {type(value)!r}")


def default_serializer_after(value: Any, type_encoders=None) -> Any:
    encoders = DEFAULT_TYPE_ENCODERS if not type_encoders else {**DEFAULT_TYPE_ENCODERS, **type_encoders}
    for base in value.__class__.__mro__[:-1]:
        if (encoder := encoders.get(base)) is not None:
            return encoder(value)
    raise TypeError(f"Unsupported type: {type(value)!r}")


from dataclasses import dataclass

@dataclass
class Record:
    id: int
    created_at: datetime
    name: str

records = [Record(id=i, created_at=datetime.now(), name=f"item-{i}") for i in range(10_000)]
payload = [{"id": r.id, "created_at": r.created_at, "name": r.name} for r in records]

ITERATIONS = 100

def run_before():
    for row in payload:
        default_serializer_before(row["created_at"])

def run_after():
    for row in payload:
        default_serializer_after(row["created_at"])

RUNS = 5

results_before = [timeit.timeit(run_before, number=ITERATIONS) for _ in range(RUNS)]
results_after  = [timeit.timeit(run_after,  number=ITERATIONS) for _ in range(RUNS)]

def stats(label, times):
    mn, mx, med = min(times), max(times), sorted(times)[len(times) // 2]
    ms = lambda t: f"{t / ITERATIONS * 1000:.2f} ms/iter"
    print(f"{label}")
    print(f"  min={mn:.3f}s ({ms(mn)})  max={mx:.3f}s ({ms(mx)})  median={med:.3f}s ({ms(med)})")

stats("BEFORE", results_before)
stats("AFTER ", results_after)
med_b = sorted(results_before)[RUNS // 2]
med_a = sorted(results_after)[RUNS // 2]
print(f"speedup (median): {med_b / med_a:.2f}x")
  ┌─────────┬─────────┬─────────┬─────────┐                                                                                                                                                                 
  │         │   min   │ median  │   max   │
  ├─────────┼─────────┼─────────┼─────────┤                                                                                                                                                                 
  │ BEFORE  │ 5.40 ms │ 5.46 ms │ 5.49 ms │                                                                                                                                                               
  ├─────────┼─────────┼─────────┼─────────┤                                                                                                                                                                 
  │ AFTER   │ 4.34 ms │ 4.39 ms │ 4.48 ms │                                                                                                                                                               
  ├─────────┼─────────┼─────────┼─────────┤                                                                                                                                                                 
  │ speedup │ 1.24x   │ 1.25x   │ 1.22x   │
  └─────────┴─────────┴─────────┴─────────┘   

@sobolevn sobolevn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

@Peopl3s

Peopl3s commented Apr 20, 2026

Copy link
Copy Markdown
Contributor Author

@sobolevn ty for review :)

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: default_serializer creates a full copy of DEFAULT_TYPE_ENCODERS on every call

2 participants