Your SlideShare is downloading. ×
0
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Dive into CPython Bytecode
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Dive into CPython Bytecode

2,658

Published on

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

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,658
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
72
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 <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. 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 <ul><li>&gt;&gt;&gt; def f(): </li></ul><ul><li>... return [x for x in xrange(10)] </li></ul><ul><li>&gt;&gt;&gt; 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. &nbsp;
  • 20. &nbsp;
  • 21. Less Bytecode == faster!
  • 22. More specialized bytecodes == faster
  • 23. @alex_gaynor Thanks

×