Dive into CPython Bytecode

  • 2,569 views
Uploaded on

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

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

More in: Technology , Sports
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
2,569
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
70
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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