Dive into CPython Bytecode
Upcoming SlideShare
Loading in...5
×
 

Dive into CPython Bytecode

on

  • 3,689 views

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

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

Statistics

Views

Total Views
3,689
Views on SlideShare
3,650
Embed Views
39

Actions

Likes
0
Downloads
70
Comments
0

3 Embeds 39

http://www.linkedin.com 22
https://www.linkedin.com 9
http://www.slideshare.net 8

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Dive into CPython Bytecode Dive into CPython Bytecode Presentation Transcript

  • 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
      • UNARY_NEGATIVE: -TOS
      • BINARY_ADD: TOS1 + TOS
      • SLICE+3: TOS2[TOS1:TOS]
      • UNPACK_SEQUENCE
      • LOAD_CONST
      • CALL_FUNCTION_VAR_KW
  • Python VM is stack based
  • So now we know some bytecodes, what can we do with this knowledge
  • 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
  •  
  •  
  • Less Bytecode == faster!
  • More specialized bytecodes == faster
  • @alex_gaynor Thanks