[Python-Dev] Release of astoptimizer 0.3

MRAB python at mrabarnett.plus.com
Tue Sep 11 19:06:31 CEST 2012


On 11/09/2012 13:06, Victor Stinner wrote:
>>> * Call builtin functions if arguments are constants. Examples:
>>>
>>>   - len("abc") => 3
>>>   - ord("A") => 65
>>
>> Does it preserve python semantics? What if you change the len builtin?
>
> This optimization is disabled by default (in the version 0.3), because
> builtin functions may be shadowed. Examples: "len=ord;
> print(len('A'))" or exec(compile("print(len('A'))", "test", "exec"),
> {'len': ord}).
>
> If you know that one specific builtin function is shadowed (ex:
> print), you can modify the configuration to enable optimizations on
> builtin functions except the specified function.
>
> Do you projects where builtin functions are shadowed?
>
> The idea is that the author of the application knows its application
> (... and all modules used by applications) and is able to explicitly
> specify what is that "constant". You can for example declare some
> variables as constant using Config.add_constant("module.name", value).
> In the same manner, you can declare "pure" functions (without border
> effect).
>
Do you know what the cost would be of, say, replacing:

     len("abc")

with:

     3 if len is __builtins__.len else len("abc")

if possible where the lookup __builtins__.len is been done early, such
as at compile time?


More information about the Python-Dev mailing list