Hi Timothy, On Wed, May 23, 2012 at 2:44 PM, Timothy Baldridge <tbaldri...@gmail.com> wrote: > So my question is, how easy/hard would it be to somehow get a > @purefunction decorator in Python instead of just RPython? For that > matter, having __immutable_fields__ would also be nice, but that may > be asking for too much.
It's the opposite. We could more or less easily have an "__immutable_fields__" declaration that lets you write to an attribute of a class only once. Maybe call it "__immutable_slots__" and have it work like "__slots__". But providing a @purefunction or @elidable decorator at the level of Python raises more questions. For example, I'm sure that the intended meaning is that if the function is called as f(10) with a constant 10, then we can constant-fold it. But does this mean the value 10, or the object 10 (which might be different even though the value 10 is the same)? Or do you want to go all the way and say that as soon as Python objects compare equal, then the function call can be constant-folded? Do you want to require the arguments to be hashable objects, so that we can use a dictionary to know the result? And anyway, what does "compare equal as Python objects" mean at the level of the JIT? Nothing... So your original suggestion is hard to define correctly... Note that already nowadays you can hack (maybe we should just make such hacks more explicit): if you use not a general-purpose dictionary but instead a namespace like a module or a class, then reads are constant-folded with an out-of-line guard. This means you could rewrite your code for example like this: class ProtocolFn(object): def __init__(self): self.m = new.module('protocol') def extend(self, tp, fn): setattr(self.m, tp, fn) def getFn(self, tp): return getattr(self.m, tp) Or even "self.d = new.module('protocol').__dict__", with no changes in the rest of the code. I'm not 100% sure if it works, but if you're successful at using it, then we could provide it more officially, e.g. with "import __pypy__; d = __pypy__.rarely_changing_dict()" :-) A bientôt, Armin. _______________________________________________ pypy-dev mailing list pypy-dev@python.org http://mail.python.org/mailman/listinfo/pypy-dev