0
Dive into CPython Bytecode Alex Gaynor
I don't need to know that stuff
You don't.  But it's totally helpful.
How does CPython Make Bytecode?
Tokenize the source
Parse the tokens into an AST
Build a symbol table to figure out what's a global
Compile to bytecode
Now I can haz bytecode
Now that I has a bytecode how does I see it?
>>> def f(o): ...  return o >>> import dis >>> dis.dis(f) 2  0 LOAD_FAST  0 (o) 3 RETURN_VALUE
So use the dis module
So now that we have bytecode, what do we do with it?
Well, what are those bytecodes?
A few examples <ul><ul><li>UNARY_NEGATIVE: -TOS </li></ul></ul><ul><ul><li>BINARY_ADD: TOS1 + TOS </li></ul></ul><ul><ul><...
Python VM is stack based
So now we know some bytecodes, what can we do with this knowledge
A tale of 2 functions <ul><li>>>> def f(): </li></ul><ul><li>...  return [x for x in xrange(10)] </li></ul><ul><li>>>> def...
 
 
Less Bytecode == faster!
More specialized bytecodes == faster
@alex_gaynor Thanks
Upcoming SlideShare
Loading in...5
×

Dive into CPython Bytecode

2,663

Published on

My slides from my 2009 DjangoCon lightning talk on CPython bytecode.

Published in: Technology, Sports
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,663
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
72
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Dive into CPython Bytecode"

  1. 1. Dive into CPython Bytecode Alex Gaynor
  2. 2. I don't need to know that stuff
  3. 3. You don't. But it's totally helpful.
  4. 4. How does CPython Make Bytecode?
  5. 5. Tokenize the source
  6. 6. Parse the tokens into an AST
  7. 7. Build a symbol table to figure out what's a global
  8. 8. Compile to bytecode
  9. 9. Now I can haz bytecode
  10. 10. Now that I has a bytecode how does I see it?
  11. 11. >>> def f(o): ... return o >>> import dis >>> dis.dis(f) 2 0 LOAD_FAST 0 (o) 3 RETURN_VALUE
  12. 12. So use the dis module
  13. 13. So now that we have bytecode, what do we do with it?
  14. 14. Well, what are those bytecodes?
  15. 15. A few examples <ul><ul><li>UNARY_NEGATIVE: -TOS </li></ul></ul><ul><ul><li>BINARY_ADD: TOS1 + TOS </li></ul></ul><ul><ul><li>SLICE+3: TOS2[TOS1:TOS] </li></ul></ul><ul><ul><li>UNPACK_SEQUENCE </li></ul></ul><ul><ul><li>LOAD_CONST </li></ul></ul><ul><ul><li>CALL_FUNCTION_VAR_KW </li></ul></ul>
  16. 16. Python VM is stack based
  17. 17. So now we know some bytecodes, what can we do with this knowledge
  18. 18. A tale of 2 functions <ul><li>>>> def f(): </li></ul><ul><li>... return [x for x in xrange(10)] </li></ul><ul><li>>>> def g(): </li></ul><ul><li>... o = [] </li></ul><ul><li>... for x in xrange(10): </li></ul><ul><li>... o.append(x) </li></ul><ul><li>... return o </li></ul>
  19. 21. Less Bytecode == faster!
  20. 22. More specialized bytecodes == faster
  21. 23. @alex_gaynor Thanks
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×