Lightweight Language Support for Type-Based, Concurrent Event Processing Philipp Haller EPFL Scala Workshop 2010.

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

Objects and Classes David Walker CS 320. Advanced Languages advanced programming features –ML data types, exceptions, modules, objects, concurrency,...
ML Exceptions.1 Standard ML Exceptions. ML Exceptions.2 Exceptions – The Need  An extensive part of the code is error handling  A function can return.
Greta YorshEran YahavMartin Vechev IBM Research. { ……………… …… …………………. ……………………. ………………………… } T1() Challenge: Correct and Efficient Synchronization { ……………………………
2 nd Microsoft Rotor Workshop, Pisa, April 23-25, SCOOPLI for.NET: a library for concurrent object-oriented programming Volkan Arslan, Piotr Nienaltowski.
1 Introducing Collaboration to Single User Applications A Survey and Analysis of Recent Work by Brian Cornell For Collaborative Systems Fall 2006.
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
CS 299 – Web Programming and Design Overview of JavaScript and DOM Instructor: Dr. Fang (Daisy) Tang.
Generative Programming. Generic vs Generative Generic Programming focuses on representing families of domain concepts Generic Programming focuses on representing.
Time, Clocks, and the Ordering of Events in a Distributed System Leslie Lamport (1978) Presented by: Yoav Kantor.
Programming Paradigms for Concurrency Lecture 9 Part III – Message Passing Concurrency.
Scala Actors -Terrance Dsilva.  Thankfully, Scala offers a reasonable, flexible approach to concurrency  Actors aren’t a concept unique to Scala.
Maria-Cristina Marinescu Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology A Synthesis Algorithm for Modular Design of.
Introduction to the Enterprise Library. Sounds familiar? Writing a component to encapsulate data access Building a component that allows you to log errors.
The Scala Programming Language presented by Donna Malayeri.
Generative Programming. Automated Assembly Lines.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Concurrency Patterns Emery Berger and Mark Corner University.
CS 346 – Chapter 4 Threads –How they differ from processes –Definition, purpose Threads of the same process share: code, data, open files –Types –Support.
Design Analysis builds a logical model that delivers the functionality. Design fully specifies how this functionality will be delivered. Design looks from.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
CSE 451: Operating Systems Winter 2015 Module 22 Remote Procedure Call (RPC) Mark Zbikowski Allen Center 476 © 2013 Gribble, Lazowska,
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
10/02/2012CS4230 CS4230 Parallel Programming Lecture 11: Breaking Dependences and Task Parallel Algorithms Mary Hall October 2,
SPL/2010 Synchronization 1. SPL/2010 Overview ● synchronization mechanisms in modern RTEs ● concurrency issues ● places where synchronization is needed.
D u k e S y s t e m s Asynchronous Replicated State Machines (Causal Multicast and All That) Jeff Chase Duke University.
On Implementing High Level Concurrency in Java G Stewart von Itzstein Mark Jasiunas University of South Australia.
CS533 - Concepts of Operating Systems 1 Threads, Events, and Reactive Objects - Alan West.
Dynamic Programming & Memoization. When to use? Problem has a recursive formulation Solutions are “ordered” –Earlier vs. later recursions.
Source Level Debugging of Parallel Programs Roland Wismüller LRR-TUM, TU München Germany.
Symbolic Model Checking of Software Nishant Sinha with Edmund Clarke, Flavio Lerda, Michael Theobald Carnegie Mellon University.
An Overview of Scala Philipp Haller, EPFL (Lots of things taken from Martin Odersky's Scala talks)
Tom Van Cutsem, Vrije Universiteit Brussel
Jim Fawcett CSE 691 – Software Modeling and Analysis Fall 2000
Scala days 2010 exmachina.
Threads vs. Events SEDA – An Event Model 5204 – Operating Systems.
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
Chapter 13: The Graph Abstract Data Type
Parallel and Distributed Simulation
Semaphores Synchronization tool (provided by the OS) that does not require busy waiting. Logically, a semaphore S is an integer variable that, apart from.
Data Transport for Online & Offline Processing
Behavioral Design Patterns
CSC 4250 Computer Architectures
Parallel Programming By J. H. Wang May 2, 2017.
Lecture 5: GPU Compute Architecture
Amir Kamil and Katherine Yelick
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Setac: A Phased Deterministic Testing Framework for Scala Actors
CSE 451: Operating Systems Winter 2006 Module 20 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Lecture 5: GPU Compute Architecture for the last time
Setac: A Phased Deterministic Testing Framework for Scala Actors
CSE 451: Operating Systems Autumn 2003 Lecture 16 RPC
CSE341: Programming Languages Lecture 12 Equivalence
CSE 451: Operating Systems Winter 2007 Module 20 Remote Procedure Call (RPC) Ed Lazowska Allen Center
A List Implementation That Links Data
CSE341: Programming Languages Lecture 12 Equivalence
Background and Motivation
CSE 451: Operating Systems Winter 2004 Module 19 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Spring 2012 Module 22 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Autumn 2009 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
Computer Science 312 Concurrent Programming I Processes and Messages 1.
Amir Kamil and Katherine Yelick
More advanced aspects of search
CSE341: Programming Languages Lecture 12 Equivalence
TensorFlow: A System for Large-Scale Machine Learning
CSE 451: Operating Systems Autumn 2010 Module 21 Remote Procedure Call (RPC) Ed Lazowska Allen Center
CSE 451: Operating Systems Winter 2003 Lecture 16 RPC
CSE341: Programming Languages Lecture 12 Equivalence
CSE 451: Operating Systems Messaging and Remote Procedure Call (RPC)
A List Implementation That Links Data
Presentation transcript:

Lightweight Language Support for Type-Based, Concurrent Event Processing Philipp Haller EPFL Scala Workshop 2010

April 15, 2010Philipp Haller2/21 Motivation ● Concurrent and distributed programming with asynchronous events is indispensible ● Many applications: event processing, web applications, algorithmic trading,... ● In Scala: actors via embedded DSL ● Problem: bad performance of innocent code patterns ● Idea: – Selectively enrich run-time type information to avoid performance hazards

April 15, 2010Philipp Haller3/21 Actors in Scala ● Actors: processes that exchange messages ● Send is asynchronous: messages are buffered in actor's mailbox ● react waits for next message that matches any of the patterns msgpat i actor ! message // message send react { // message receive case msgpat 1 => action 1... case msgpat n => action n }

April 15, 2010Philipp Haller4/21 Example loop { react { case Put(x) => react { case Get(from) => from ! x } Simple buffer actor: Loops indefinitely Remove message from current actor's mailbox Scenario: ● Lots of Put messages in mailbox ● Get messages arrive slowly ➔ Outer react finishes quickly ➔ Inner react searches entire mailbox in most cases ➔ Worst case: for every Get message: go through all Put messages in mailbox!

April 15, 2010Philipp Haller5/21 Optimization by Hand val putQ = new Queue[Int] val getQ = new Queue[Actor] loop { react { case Get(from) => if (putQ.isEmpty) getQ enqueue from else from ! Put(putQ.dequeue) case Put(x) => if (getQ.isEmpty) putQ enqueue x else getQ.dequeue ! Put(x) } Explicit queues replace actor's mailbox

April 15, 2010Philipp Haller6/21 Partial Functions in Scala { case msgpat 1 => action 1... case msgpat n => action n } trait PartialFunction[-A,+B] extends Function1[A,B] { def isDefinedAt(x: A): Boolean } trait Function1[-A,+B] { def apply(x: A): B } is compiled to (anonymous) class that extends

April 15, 2010Philipp Haller7/21 Implementing react... react { case Get(from) => // handle msg } def react(handler: PartialFunction[Msg, Unit]) = { mailbox.extractFirst(handler.isDefinedAt) match { case None => waitingFor = handler; suspendActor() case Some(msg) => handler(msg) } “Blindly” tests each message in mailbox simplified...

April 15, 2010Philipp Haller8/21 Idea: Reify Matched Types ● Add method to PartialFunction[A, B] : ● Split mailbox into subqueues: ● extractFirst(handler) skips uninteresting subqueues – Only search queue mailbox(clazz) if handler.definedFor contains clazz def definedFor : Array[Class[_]] val mailbox : Map[Class[_], Queue[Msg]] 1 2 3

April 15, 2010Philipp Haller9/21 Translucent Functions ● Each class in definedFor represents a case class that is the type of a pattern, e.g. ● tf has type TranslucentFunction[Msg, Int] and tf.definedFor == Array(classOf[Put]) trait TranslucentFunction[-A, +B] extends PartialFunction[A, B] { def definedFor: Array[Class[_]] } abstract class Msg case class Put(x: Int) extends Msg case class Get(from: Actor) extends Msg val tf = { case Put(y) => y }

April 15, 2010Philipp Haller10/21 Applying Translucency to Actors ● Let fun: TranslucentFunction[A, B] such that – fun.definedFor == Array(C 1,..., C n ) – typeof(msg) == Msg for all msg in subqueue – Msg <: C i for all i = 1..n ● We want to conclude that fun isDefinedAt msg == false for all msg in subqueue

April 15, 2010Philipp Haller11/21 Getting definedFor Right: Subtyping ● Problem: will not find Put(42) if only mailbox(classOf[Msg]) is searched! ● Solution: definedFor is empty if a pattern type is not subtype of a case class ● All subqueues searched if definedFor empty abstract class Msg case class Put(x: Int) extends Msg mailbox = Map(classOf[Msg] -> Queue(), classOf[Put] -> Queue(Put(42))) react { case any: Msg =>... }

April 15, 2010Philipp Haller12/21 Getting definedFor Right: Modularity ● Consider usage of translucent function ● Adding separately compiled ● Problem: find PutTwice messages without recompiling translucent function! ● Adding a separately compiled class should not affect definedFor ● Solution: definedFor contains only case classes class PutTwice(x: Int) extends Put(x) react { case Put(x) =>... } Assume no case class inheritance

April 15, 2010Philipp Haller13/21 Translucent Functions, Precisely

April 15, 2010Philipp Haller14/21 Optimizing Actors ● Foundation: ● Split mailbox: using case classes C 1,..., C n – if typeof(msg) <: C 1 then typeof(msg) <: C i (i = 1) ● Insert msg such that typeof(msg) <: C i into subqueue for C i ; into global queue otherwise ● search only subqueues for classes in definedFor

April 15, 2010Philipp Haller15/21 Optimizing Join Patterns join { case Exclusive(from) => join { case Sharing(0) => from ! OK } case ReleaseExclusive(from: Reactor) => self ! Sharing(0); from ! OK case Shared(from: Reactor) => join { case Sharing(n) => self ! Sharing(n+1); from ! OK } case ReleaseShared(from) => join { case Sharing(n) if n > 0 => self ! Sharing(n-1); from ! OK } ● Extending partial match requires matching messages received so far against next pattern ● Skip subqueues with messages that cannot match

April 15, 2010Philipp Haller16/21 Implementation ● Small extension to Scala 2.8 compiler ● Partial function literals are translucent: ● Add def definedFor: Array[Class[_]], populated with classOf[C] for each case class C such that the type of a pattern is a subtype of C ● definedFor is empty iff one of the pattern types is not subtype of a case class def react(fun: TranslucentFunction[Msg, Unit]) =... react { case Put(x) =>... }

April 15, 2010Philipp Haller17/21 Implementation (2) ● Drop-in replacement for actors message queue ● Incremental queue splitting – Unknown class in definedFor ? Create new subqueue and populate with conforming messages from global queue (operate on queue nodes) ● Queue nodes contain time stamps to maintain ordering across subqueues ● Cache mappings between concrete message classes and target subqueues (for superclass)

April 15, 2010Philipp Haller18/21 Experiments ● Worst-case overhead in chameneos-redux – No nested/sequenced receives: translucent functions only incur overhead! ● Baseline 18% slower than ActorFoundry ● 23% overhead compared to baseline

April 15, 2010Philipp Haller19/21 Experiments (2) ● Producer/consumer scenario ● Default does not scale (quadratic factor!) – Affects all other actor implementations! ● Overhead compared to manual optimization shrinks from 58% (20,000) to 14% (200,000)

April 15, 2010Philipp Haller20/21 Code Size ● No execution overhead when used in place of partial functions; class files get bigger, though ➔ Can we make all partial functions translucent? ● Only very small increase in code size – Generated class files for compiler and standard library: increase by 0.26% or 140 KB ● Actor-based benchmark code: – chameneos-redux: 3.7% increase – producer-consumer: 8.9% increase

April 15, 2010Philipp Haller21/21 Conclusion ● Minimal compiler extension (not syntax) ● Refinement of Scala's partial functions ● Potential for significant performance improvements of concurrency abstractions ● Future work: apply lessons learned in compiler plug-in for optimizing join patterns ● Questions?