short fetch for NEXTARG macro (was: one byte byte code arguments)

I just did it: my first python source code hack. I replaced the NEXTARG and PEEKARG macros in ceval.c using a cast to short pointer, and lo and behold, a crude measurement indicates one to two percent speed increase. That isn't much, but it is virtually for free! Here are the macro's I used: #define NEXTARG() (next_instr +=2, *(short*)&next_instr[-2]) #define PEEKARG() (*(short*)&next_instr[1]) - Jurjen

On Mon, 31 Jan 2011 13:28:39 +0100 "Jurjen N.E. Bos" <Jurjen.Bos@hetnet.nl> wrote:
I just did it: my first python source code hack. I replaced the NEXTARG and PEEKARG macros in ceval.c using a cast to short pointer, and lo and behold, a crude measurement indicates one to two percent speed increase. That isn't much, but it is virtually for free!
Here are the macro's I used: #define NEXTARG() (next_instr +=2, *(short*)&next_instr[-2]) #define PEEKARG() (*(short*)&next_instr[1])
Some architectures forbid unaligned access, so this can't be used as-is. Regards Antoine.

2011/1/31 Antoine Pitrou <solipsis@pitrou.net>
On Mon, 31 Jan 2011 13:28:39 +0100 "Jurjen N.E. Bos" <Jurjen.Bos@hetnet.nl> wrote:
I just did it: my first python source code hack. I replaced the NEXTARG and PEEKARG macros in ceval.c using a cast to short pointer, and lo and behold, a crude measurement indicates one to two percent speed increase. That isn't much, but it is virtually for free!
Here are the macro's I used: #define NEXTARG() (next_instr +=2, *(short*)&next_instr[-2]) #define PEEKARG() (*(short*)&next_instr[1])
Some architectures forbid unaligned access, so this can't be used as-is.
Regards
Antoine.
WPython already addressed it ( http://code.google.com/p/wpython2/source/browse/Python/ceval.c?repo=wpython1...): #ifdef WORDS_BIGENDIAN #define NEXTOPCODE() oparg = *next_instr++; \ opcode = oparg >> 8; oparg &= 0xff #else #define NEXTOPCODE() oparg = *next_instr++; \ opcode = oparg & 0xff; oparg >>= 8 #endif Shorts alignament is also guaranted due to wordcodes ( http://wpython2.googlecode.com/files/Beyond%20Bytecode%20-%20A%20Wordcode-ba...). Cesare

On Jan 31, 2011, at 7:45 AM, Antoine Pitrou wrote:
On Mon, 31 Jan 2011 13:28:39 +0100 "Jurjen N.E. Bos" <Jurjen.Bos@hetnet.nl> wrote:
I just did it: my first python source code hack. I replaced the NEXTARG and PEEKARG macros in ceval.c using a cast to short pointer, and lo and behold, a crude measurement indicates one to two percent speed increase. That isn't much, but it is virtually for free!
Here are the macro's I used: #define NEXTARG() (next_instr +=2, *(short*)&next_instr[-2]) #define PEEKARG() (*(short*)&next_instr[1])
Some architectures forbid unaligned access, so this can't be used as-is.
It could perhaps be #ifdef'd in on x86/x86-64, though, which is by far the most common architecture to run python on. James
participants (4)
-
Antoine Pitrou
-
Cesare Di Mauro
-
James Y Knight
-
Jurjen N.E. Bos