[Python-ideas] Make Python code read-only

Victor Stinner victor.stinner at gmail.com
Thu Jun 5 15:05:42 CEST 2014


2014-06-05 14:02 GMT+02:00 Trent Nelson <trent at snakebite.org>:
> On May 20, 2014, at 12:57 PM, Victor Stinner <victor.stinner at gmail.com> wrote:
>> I'm trying to find the best option to make CPython faster. I would
>> like to discuss here a first idea of making the Python code read-only
>> to allow new optimizations.
>
> I did two passes on read-only functionality for PyParallel.  First attempt was similar to yours; I instrumented various core Python objects such that mutations could be detected against read-only objects (and subsequently raised as an exception).  That didn’t pan out the way I wanted it to, especially in the PyParallel multiple-interpreter-threads-running-in-parallel environment.
>
> Second attempt: use memory protection.  CPUs and OSes are really good at enforcing memory protection — leverage that.  Don’t try and do it yourself in userspace.  This worked much better.

My first attempt to "make the code read-only" was a big fail. Lot of
errors and complains :-)

I'm now moving to a different approach: "notify changes of the code".
In PyParellel, you raise an error if something is modified. I don't
need such restriction, I "just" want to disable optimizations if the
code changed.

> On POSIX you’d achieve the same affect via mprotect and a SIGSEV trap.

I don't think that relying on SIGSEGV is reliable :-( Such signal can
be emitted for various reasons and you have to use
sigsetjmp/siglongjmp which is unsafe: you cannot cleanup state when an
error occurs. Or did you implement it differently?

Victor


More information about the Python-ideas mailing list