Performance Engineering of the Starlogo Nova
Total Page:16
File Type:pdf, Size:1020Kb
Performance Engineering of the StarLogo Nova Execution Engine by Jin Pan Submitted to the Department of Electrical Engineering and Computer Science in partial fulfillment of the requirements for the degree of Master of Engineering in Computer Science and Engineering at the MASSACHUSETTS INSTITUTE OF TECHNOLOGY September 2016 ○c Massachusetts Institute of Technology 2016. All rights reserved. Author................................................................ Department of Electrical Engineering and Computer Science August 16, 2016 Certified by. Eric Klopfer Director of MIT Scheller Teacher Education Program Thesis Supervisor Accepted by . Christopher J. Terman Chairman, Masters of Engineering Thesis Committee 2 Performance Engineering of the StarLogo Nova Execution Engine by Jin Pan Submitted to the Department of Electrical Engineering and Computer Science on August 16, 2016, in partial fulfillment of the requirements for the degree of Master of Engineering in Computer Science and Engineering Abstract StarLogo Nova is an online blocks-based programming environment designed for pre- college students to explore the collective behavior of decentralized agents. Users can drag and drop blocks to construct graphical scripts that control how individual agents respond to stimuli. These scripts are run by the Execution Engine and rendered in real-time. By investigating hypotheses about how small tweaks to individual behav- ior impact the entire system, students learn to think beyond the centralized mindset where all actions are dictated by a singular leader. This thesis migrates the Execution Engine from the aging Adobe ActionScript 3 language to TypeScript, a weakly typed language that transpiles to JavaScript. To promote code health, this thesis introduces code formatters, linters, test cases, and a build process. Finally, this thesis optimizes the StarLogo Nova Execution Engine for performance, consistently beating the previous engine and bringing the execution time per cycle for key benchmarks under 10 milliseconds. Thesis Supervisor: Eric Klopfer Title: Director of MIT Scheller Teacher Education Program 3 4 Acknowledgments To my parents, for all the difficult sacrifices they have made for my education. Ihope that future StarLogo Nova users are able to indirectly benefit from your hard work. I would like to thank all the teachers, mentors, and friends that I have met and learned immensely from, both inside the classroom and outside. From that list of teachers, mentors, and friends, I would like to specifically Daniel Wendel and Eric Klopfer for our rich discussions this year: they not only helped me understand the internal StarLogo codebase, but also its role in the educational technologies world and students’ lives. Last but not least, I would like to thank my girlfriend Linda Wang for always being there and reassuring me in seemingly hopeless situations that there always will be a way. Thank you for all your inspiration and believing in me. 5 6 Contents 1 StarLogo Nova Background 15 1.1 LISP . 16 1.2 Logo . 16 1.3 StarLogo . 17 1.4 StarLogo: The Next Generation . 19 1.4.1 StarLogoT and NetLogo . 22 1.5 StarLogo Nova . 22 1.6 StarLogo Nova Technical Overview . 25 1.7 StarLogo Nova Block and Functionality Overview . 29 1.7.1 Agent blocks (figure 1-10) . 29 1.7.2 Detection blocks (figure 1-11) . 31 1.7.3 Environment blocks (figure 1-12) . 32 1.7.4 Interface blocks (figure 1-13) . 33 1.7.5 Keyboard blocks (figure 1-14) . 34 1.7.6 List blocks (figure 1-15) . 35 1.7.7 Logic blocks (figure 1-16) . 35 1.7.8 Math blocks (figure 1-17) . 36 1.7.9 Movement blocks (figure 1-18) . 37 1.7.10 Procedure blocks (figure 1-19) . 37 1.7.11 Sound blocks (figure 1-20) . 38 1.7.12 Trait blocks (figure 1-21) . 38 1.7.13 Variable blocks (figure 1-22) . 39 7 1.7.14 Debugger blocks (figure 1-23) . 40 2 StarLogo Nova Limitations and Rationale for Contributions 41 2.1 ActionScript3 Migration . 41 2.2 New Tooling . 43 2.3 Performance Engineering the Engine . 44 3 Flash Migration 45 3.1 Initial Migration Steps . 45 3.2 Migrating the Engine Core . 47 3.2.1 A Difficulty in Migrating the Engine Core . 47 3.2.2 A Simpler Interpreter . 50 3.3 Miscellaneous Migration Details . 55 3.3.1 JavaScript Maps vs Objects . 55 3.3.2 Singletons and Static Classes . 56 3.3.3 BlockTable . 56 3.3.4 Key Manager . 56 3.3.5 Dead Code . 57 3.3.6 General Statistics . 57 4 New Tooling 59 4.1 Migration Tools . 59 4.1.1 make and tsc ........................... 59 4.1.2 tslint ............................... 61 4.1.3 tsfmt ............................... 63 4.2 Automatic Testing . 65 4.3 Version Control Workflow . 65 5 Performance Engineering the Engine 67 5.1 Overview of JavaScript Engines . 67 5.1.1 Chrome V8 . 68 5.1.2 Firefox SpiderMonkey . 69 8 5.1.3 Safari JavaScriptCore . 70 5.1.4 Microsoft Edge Chakra . 71 5.1.5 Summary of JavaScript Engines . 71 5.2 Setting up a Testbed . 72 5.3 Interpreter Optimization Attempt . 74 5.4 Transpilation to JavaScript . 76 5.4.1 Outline of how JavaScript is generated . 77 5.4.2 Nuances to generating the JavaScript . 77 5.5 Optimizing the Transpiled Code . 81 5.5.1 Benchmark Suite Description . 81 5.5.2 Universal Optimizations . 83 5.5.3 Renderer Optimizations . 93 5.5.4 Collision Optimizations . 94 5.6 Negative Optimization Results . 97 5.7 Overall Benchmark Results . 101 6 Future contributions 103 6.1 Better Type Handling . 103 6.1.1 Current Type System . 103 6.1.2 Stronger Typing Challenges and Solutions . 104 6.2 Live code editing . 105 6.2.1 Live code editing groundwork . 105 6.2.2 Live code editing semantics . 106 6.2.3 Live code editing user interface . 107 6.3 Debugging . 108 6.3.1 Logging . 109 6.3.2 Stepping . 110 6.3.3 Alternative Stepping Design . 110 6.4 User Scripting . 111 6.4.1 Security . 111 9 6.5 Tooling . 112 7 Conclusion 115 10 List of Figures 1-1 Graphical representation of the Dragon Curve . 16 1-2 Logo code to generate the Dragon Curve . 17 1-3 Logo code to generate the Dragon Curve with errors . 19 1-4 Blocks code to generate the Dragon Curve . 21 1-5 Screenshot of the StarLogo Nova interface . 24 1-6 Block Specification of if ......................... 26 1-7 Compatible blocks for the if block . 27 1-8 Incompatible blocks for the if block . 27 1-9 StarLogo Nova Execution Engine Pseudocode . 29 1-10 Agent Blocks . 30 1-11 Detection Blocks . 31 1-12 Environment Blocks . 32 1-13 Interface Blocks . 33 1-14 Keyboard Blocks . 34 1-15 List Blocks . 35 1-16 Logic Blocks . 36 1-17 Math Blocks . 37 1-18 Movement Blocks . 38 1-19 Procedure Blocks . 39 1-20 Sound Blocks . 39 1-21 Trait Blocks . 40 1-22 Variable Blocks . 40 1-23 Debugger Blocks . 40 11 3-1 AS3 Interpreter code to compute if ................... 48 3-2 AS3 Interpreter code to compute CalcSum . 49 3-3 Simpler Interpreter code to compute if . 52 3-4 Simpler Interpreter code to compute CalcSum . 53 4-1 Makefile all Target . 62 4-2 Overriding tslint ............................ 63 4-3 tsfmt invocation . 64 5-1 Benchmark Script . 74 5-2 Rotate Benchmark . 74 5-3 JavaScript Code Generation for If . 78 5-4 JavaScript Code Generation for ProcCall . 79 5-5 Blocks for a Transpilation Example . 80 5-6 JavaScript for a Transpilation Example . 80 5-7 Profiler with no optimizations . ..