[Python-Dev] Why is Bytecode the way it is?

Raymond Hettinger python at rcn.com
Wed Jul 14 13:56:50 CEST 2004


>   case LOAD_CONST:
>       x = GETITEM(consts, oparg);
>       Py_INCREF(x);
> +     if (*next_instr == RETURN_VAL) {
> +         retval = x;
> +         why = WHY_RETURN;
> +         goto fast_block_end;
> +     }
>       PUSH(x);
>       goto fast_next_opcode;
> 
> This would skip the stack and a trip through the loop without changing
> the parser or the bytecode,  and with a minimal amount of added code
or
> overhead. This could (of course) be applied to other opcodes, too.

This approach is a total disaster.  It adds overhead (an unpredictable)
branch to one of the most common opcodes.  It burdens the common case to
save only a tiny bit in a special case.  LOAD_CONST and RETURN_VAL are
already among the fastest bytecodes.

The returning of values is not where Python code is spending all its
time.  Better to focus on attribute lookup and function calls.


Raymond



More information about the Python-Dev mailing list