Hacker News new | past | comments | ask | show | jobs | submit login

Even if you manage to preload everything you need, Python's reference counting mechanism will cause everything to be copied anyway.

Every time you access an object, its reference count is mutated, which will the memory page to be copied.

There are workarounds if you're willing to mess with the Python interpreter: https://instagram-engineering.com/copy-on-write-friendly-pyt...




It still helps with the loaded .so files and whatnot. The code objects and other things that are immutable. (CPython refcounts those too?)


> The code objects and other things that are immutable. (CPython refcounts those too?)

CPython refcounts all objects. Refcounting is not required because of mutability; it's required because the interpreter needs to know when an object's memory can be reclaimed for something else.

I don't know if code objects specifically would have their refcounts mutated a lot, since typically they're only referenced by one object, the function that they're the code for. But function objects will have their refcounts mutated every time the function is called, since that sets up a stack frame that grabs a reference to the function object and then releases it when the function returns.


The code and read-only data of .so files will be shared anyway


> Python's reference counting mechanism will cause everything to be copied anyway.

isn't it CPython specific though, and may not be observed when running on PyPy?


It is. And in practice it is far from everything, only stuff that is actively directly referenced by the service code.


gc.freeze() reached Python 3.7 as mentioned there, I'm not sure how many web frameworks are using it though.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: