Python Implementation Strategies. Jeremy Hylton Python / Google

Similar documents
Building a Multi-Language Interpreter Engine

Optimization Techniques

Tizen/Artik IoT Lecture Chapter 3. JerryScript Parser & VM

Compiling Techniques

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Operational Semantics. One-Slide Summary. Lecture Outline

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Operational Semantics of Cool

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

Project. there are a couple of 3 person teams. a new drop with new type checking is coming. regroup or see me or forever hold your peace

Delft-Java Link Translation Buffer

High-Level Language VMs

Hardware Emulation and Virtual Machines

6.828: OS/Language Co-design. Adam Belay

Announcements. My office hours are today in Gates 160 from 1PM-3PM. Programming Project 3 checkpoint due tomorrow night at 11:59PM.

Dispatch techniques and closure representations

CSc 453 Interpreters & Interpretation

Assumptions. History

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

Extending Jython. with SIM, SPARQL and SQL

Faster Programs with Guile 3

Dynamic Dispatch and Duck Typing. L25: Modern Compiler Design

SABLEJIT: A Retargetable Just-In-Time Compiler for a Portable Virtual Machine p. 1

Accelerating Ruby with LLVM

Index. object lifetimes, and ownership, use after change by an alias errors, use after drop errors, BTreeMap, 309

CS 360 Programming Languages Interpreters

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Notation. The rules. Evaluation Rules So Far.

Martin Kruliš, v

Field Analysis. Last time Exploit encapsulation to improve memory system performance

Contents in Detail. Who This Book Is For... xx Using Ruby to Test Itself... xx Which Implementation of Ruby?... xxi Overview...

The Java Programming Language

Dynamically-typed Languages. David Miller

CSC 533: Organization of Programming Languages. Spring 2005

About Python. Python Duration. Training Objectives. Training Pre - Requisites & Who Should Learn Python

Java: framework overview and in-the-small features

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Fachhochschule Wedel Technical Report Nr Implementing the Forth Inner Interpreter in High Level Forth

The Procedure Abstraction

PyPy - How to not write Virtual Machines for Dynamic Languages

Lecture Outline. COOL operational semantics. Operational Semantics of Cool. Motivation. Lecture 13. Notation. The rules. Evaluation Rules So Far

Chapter 5:: Target Machine Architecture (cont.)

code://rubinius/technical

Where We Are. Lexical Analysis. Syntax Analysis. IR Generation. IR Optimization. Code Generation. Machine Code. Optimization.

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

Run-time Program Management. Hwansoo Han

An Implementation of Python for Racket. Pedro Palma Ramos António Menezes Leitão

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

CS143 Final Fall 2009

Seminar report Java Submitted in partial fulfillment of the requirement for the award of degree Of CSE

Chapter 3:: Names, Scopes, and Bindings (cont.)

Java Instrumentation for Dynamic Analysis

Chapter 3:: Names, Scopes, and Bindings (cont.)

ShortCut: Architectural Support for Fast Object Access in Scripting Languages

Parsing Scheme (+ (* 2 3) 1) * 1

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Building Robust Embedded Software

Write Barrier Removal by Static Analysis

Procedure and Object- Oriented Abstraction

Short Notes of CS201

Programming Languages Third Edition. Chapter 7 Basic Semantics

CS201 - Introduction to Programming Glossary By

Adaptive Optimization using Hardware Performance Monitors. Master Thesis by Mathias Payer

Some possible directions for the R engine

Introduction to Programming Using Java (98-388)

Processors, Performance, and Profiling

Advanced Object-Oriented Programming Introduction to OOP and Java

Binding and Storage. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

CSCE 314 Programming Languages

Assignment 2. CS 234 Fall 2018 Sandy Graham. Create()

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

CSE 120 Principles of Operating Systems

Kernel Probes for ARM. Quentin Barnes Motorola - Mobile Devices April 17, 2007

Static Analysis of Dynamically Typed Languages made Easy

A new Mono GC. Paolo Molaro October 25, 2006

Object Model Comparisons

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

CPU Structure and Function

CS527 Software Security

Mechanized Operational Semantics

Scope, Functions, and Storage Management

Running class Timing on Java HotSpot VM, 1

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Java Bytecode (binary file)

WELCOME TO PERL = Tuesday, June 4, 13

Context Threading: A flexible and efficient dispatch technique for virtual machine interpreters

Playing with bird guts. Jonathan Worthington YAPC::EU::2007

Sista: Improving Cog s JIT performance. Clément Béra

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

The Java Language Implementation

Typical Runtime Layout. Tiger Runtime Environments. Example: Nested Functions. Activation Trees. code. Memory Layout

Compiler construction 2009

UNIT 3

Slightly Advanced Python:

A Trace-based Java JIT Compiler Retrofitted from a Method-based Compiler

Operational Semantics of Cool

Virtual Machine Design

Transcription:

Python Implementation Strategies Jeremy Hylton Python / Google

Python language basics High-level language Untyped but safe First-class functions, classes, objects, &c. Garbage collected Simple module system Lexically scoped Std primitive objects: str, num, dict, list, tuple Object-orientation Multiple inheritance, single dispatch Metaclass for customization

Introspection in Python Nearly every object supports introspection Classes and instances represented as dictionaries Access to local variables via dictionary Modify top-level name bindings in other modules Load bytecode from string Originally envisioned for debugging But many projects find ways to abuse Language does not provide encapsulation Makes optimization more challenging

Python implementation Standard Python interpreter Classic bytecode interpreter implemented in C Small, straightforward implementation Roughly 230k LOC, a dozen active developers Large, platform-specific subsystems: Mac, Win32 Jython Python running on the JVM; development stalled IronPython Experimental implementation for CLR Easy to extend / embed in host environment

Memory management All objects are heap allocated Everything is boxed Very high allocation rate Reference counting + cyclic trash collection Objects consume a lot of memory Doubly-linked list of containers for cycle detection Instances typically used dict to store variables Optimizations? Custom small object allocator Type-specific free lists for int, frame

Multithreading Uses platform threads Only one thread can execute at a time Serialized by interpreter mainloop Pseudo-preemptive scheduling Switch threads every N bytecodes Release locks around I/O, &c. Much code depends on this feature Reference counting

Bytecode interpreter Stack-based interpreter, switch stmt dispatch Heavyweight opcodes Often correspond to Python source constructs Invoke expensive Python runtime operations Common opcodes take hundreds of cycles Examples: LOAD_ATTR fetch attribute by name LOAD_GLOBAL load global variable BINARY_ADD add two objects (untyped) Stack operations push, pop, rot

Interpreter details At entry point periodic checks for interrupts, thread switches check for debugging/profiling hooks Decode opcode & arg small set of opcodes branch directly to decode don't call out of mainloop code, on success A few opcodes inline decode compare dispatches directly to JUMP on equality saves several unpredictable branches

Example: BINARY_ADD pop args off stack check for two exact integer args inline addition check for overflow check for exact strings call concatenation routine fallback to generic PyNumber_Add routine does either arg have a numeric add slot that accomodates both types, can return NotImplemented does either arg have a sequence concat

Function calls Python frames are heap allocated But make recursive call to eval for each frame Note: Stackless Python Rich function calling conventions Variable args, keyword args, default values Function call code is complex and slow

Calling from C to Python Python runtime often calls back to Python Operator overloading, finalizers, metaclasses Delicate to maintain state, ref counts Heavy use of C stack Occasionally hard to propagate errors Hard to predict effects GC finalizer can resurrect the object Modify list that is being sorted

IronPython comparisons Generally faster than C implementation 40-80% faster on Dhrystone-style benchmark Function calls much faster Can use native CLR frames, gives up some introspection Non-local name lookup much faster Generates static definition of module Has hooks to support introspection Memory management is costly Creates and destroys builtin types much more slowly Microbenchmarks at best 70% of C interpreter

Python futures Interpreter issues raised by users Performance Security How these are realized by developers It s not much fun to implement Python in C Need better runtime representation for optimization Thread limits a growing liability