[Python-ideas] Make Python code read-only

Trent Nelson trent at snakebite.org
Thu Jun 5 14:02:14 CEST 2014


On May 20, 2014, at 12:57 PM, Victor Stinner <victor.stinner at gmail.com> wrote:

> Hi,
> 
> 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.

That work is described starting here:
	https://speakerdeck.com/trent/pyparallel-how-we-removed-the-gil-and-exploited-all-cores?slide=138

Relevant bits of implementation:
	obmalloc.c:	
		http://hg.python.org/sandbox/trent/rev/0e70a0caa1c0#l6.299

	ceval.c:
		http://hg.python.org/sandbox/trent/rev/0e70a0caa1c0#l9.30

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

Just FYI.

Regards,

	Trent.


More information about the Python-ideas mailing list