Skip to content

Conversation

@sydney-runkle
Copy link
Contributor

Alternative to #10260 re memory usage.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Aug 29, 2024

Deploying pydantic-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 8ceda20
Status: ✅  Deploy successful!
Preview URL: https://89f2ee49.pydantic-docs.pages.dev
Branch Preview URL: https://remove-defaults-filter.pydantic-docs.pages.dev

View logs

@sydney-runkle
Copy link
Contributor Author

With this change:

(pydantic_env) (base) programming@sydneys-mbp pydantic % memray stats memray_output3.bin                       
📏 Total allocations:
        2981099

📦 Total memory allocated:
        5.032GB

📊 Histogram of allocation size:
        min: 1.000B
        ---------------------------------------------
        < 4.000B   : 307851 ▇▇▇▇▇▇▇▇▇▇▇▇▇
        < 16.000B  : 615800 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇
        < 70.000B  : 457813 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇
        < 288.000B : 525727 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇
        < 1.163KB  : 634589 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇
        < 4.795KB  : 375658 ▇▇▇▇▇▇▇▇▇▇▇▇▇▇▇
        < 19.771KB :  43154 ▇▇
        < 81.511KB :   6267 ▇
        < 336.054KB:  13853 ▇
        <=1.353MB  :    387 ▇
        ---------------------------------------------
        max: 1.353MB

📂 Allocator type distribution:
         MALLOC: 2815062
         REALLOC: 133925
         CALLOC: 31730
         MMAP: 382

🥇 Top 5 largest allocating locations (by size):
        - add_module_globals:/Users/programming/pydantic_work/pydantic/pydantic/_internal/_typing_extra.py:213 -> 2.028GB
        - push:/Users/programming/pydantic_work/pydantic/pydantic/_internal/_generate_schema.py:339 -> 1.014GB
        - validate_core_schema:/Users/programming/pydantic_work/pydantic/pydantic/_internal/_core_utils.py:570 -> 900.423MB
        - __init__:/Users/programming/anaconda3/envs/pydantic_env/lib/python3.10/typing.py:668 -> 348.622MB
        - create_schema_validator:/Users/programming/pydantic_work/pydantic/pydantic/plugin/_schema_validator.py:50 -> 165.853MB

🥇 Top 5 largest allocating locations (by number of allocations):
        - create_schema_validator:/Users/programming/pydantic_work/pydantic/pydantic/plugin/_schema_validator.py:50 -> 1119543
        - validate_core_schema:/Users/programming/pydantic_work/pydantic/pydantic/_internal/_core_utils.py:570 -> 923648
        - complete_model_class:/Users/programming/pydantic_work/pydantic/pydantic/_internal/_model_construction.py:595 -> 478022
        - __init__:/Users/programming/anaconda3/envs/pydantic_env/lib/python3.10/typing.py:668 -> 91976
        - get_cls_type_hints_lenient:/Users/programming/pydantic_work/pydantic/pydantic/_internal/_typing_extra.py:238 -> 51607

Effectively, reverting #10136, which increased memory usage + didn't simplify debugging all that much.

@sydney-runkle
Copy link
Contributor Author

One question - do we need to copy the f_locals here? If we were being ultra safe, I think the answer is yes. But I don't know if that's actually necessary based on how we handle the namespace.

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 29, 2024

CodSpeed Performance Report

Merging #10261 will not alter performance

Comparing remove-defaults-filter (8ceda20) with main (27411c4)

Summary

✅ 34 untouched benchmarks

@github-actions
Copy link
Contributor

github-actions bot commented Aug 29, 2024

Coverage report

Image Image

This PR does not seem to contain any modification to coverable code.

@Viicos
Copy link
Member

Viicos commented Aug 29, 2024

Seems like this also improves performance.

One question - do we need to copy the f_locals here? If we were being ultra safe, I think the answer is yes. But I don't know if that's actually necessary based on how we handle the namespace.

We don't do any mutations on the types namespace so I don't think we should introduce any extra overhead.

Copy link
Member

@samuelcolvin samuelcolvin left a comment

Choose a reason for hiding this comment

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

I think this looks good.

@sydney-runkle can you add a comment explaining why we're not copying or f_locals and that it must not be mutated.

We could return Mapping[str, Any] then have implement our own FrozenMapping type that holds frame.f_locals internally but stops it being accidentally modified, but maybe that's over kill.

@davidhewitt maybe you should look at this too.

@sydney-runkle sydney-runkle enabled auto-merge (squash) August 29, 2024 12:40
@sydney-runkle sydney-runkle merged commit 3809bd5 into main Aug 29, 2024
@sydney-runkle sydney-runkle deleted the remove-defaults-filter branch August 29, 2024 12:41
AdolfoVillalobos pushed a commit to AdolfoVillalobos/pydantic that referenced this pull request Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

relnotes-fix Used for bugfixes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants