Generic Lightweight Druntime

Slides:



Advertisements
Similar presentations
Templates in C++. Generic Programming Programming/developing algorithms with the abstraction of types The uses of the abstract type define the necessary.
Advertisements

1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
1 Java Grande Introduction  Grande Application: a GA is any application, scientific or industrial, that requires a large number of computing resources(CPUs,
Generic Subroutines and Exceptions CS351 – Programming Paradigms.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
CSE 332: C++ Algorithms II From Last Time: Search with Generic Iterators Third generalization: separate iterator type parameter We arrive at the find algorithm.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
ADT data abstraction. Abstraction  representation of concepts by their relevant features only  programming has two major categories of abstraction process.
CoCo: Sound and Adaptive Replacement of Java Collections Guoqing (Harry) Xu Department of Computer Science University of California, Irvine.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Dynamic Binding Implementation Object-Oriented Programming Spring
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
DGrid: A Library of Large-Scale Distributed Spatial Data Structures Pieter Hooimeijer,
Review. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Objects and Classes Objects and Classes –State.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
D Language Compiler as a Library
CSE1002 – Problem Solving with Object Oriented Programming
Generic Programming in C
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Scope and Code Generation
Templates.
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
7. Inheritance and Polymorphism
Semantic Analysis with Emphasis on Name Analysis
Threads Cannot Be Implemented As a Library
Alexandru Razvan Caciulescu University POLITEHNICA of Bucharest
11.1 The Concept of Abstraction
Optimization Code Optimization ©SoftMoore Consulting.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Generics, Lambdas, Reflections
CISC/CMPE320 - Prof. McLeod
Interfaces and Inheritance
Templates.
Continuing Chapter 11 Inheritance and Polymorphism
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Lecture 9 Concepts of Programming Languages
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9 Inheritance and Polymorphism
Programming with ANSI C ++
Objective of This Course
More About Data Types & Functions
A New Collections Framework for the Standard Library
Generic programming in Java
Object Oriented Programming in java
Topic 5 Polymorphism "“Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.”
CISC/CMPE320 - Prof. McLeod
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
CISC/CMPE320 - Prof. McLeod
CISC/CMPE320 - Prof. McLeod
2. Second Step for Learning C++ Programming • Data Type • Char • Float
CIS 199 Final Review.
Chapter 11 Inheritance and Polymorphism Part 2
Chapter 9: Pointers and String
point when a program element is bound to a characteristic or property
Subtype Substitution Principle
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Procedure Linkages Standard procedure linkage Procedure has
Rethinking the Default Class Hierarchy: an Object’s Tale
Refactoring.
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Generic Lightweight Druntime Lucia Madalina Cojocaru University POLITEHNICA of Bucharest lucia.mcojocaru@gmail.com

Generic Lightweight Druntime - DConf - May 2017

Generic Lightweight Druntime - DConf - May 2017 How? Witch hunt: compiler dark magic Lower to straight D code (in Druntime) Burn them at the stake! Refactor druntime to do their job Generic Lightweight Druntime - DConf - May 2017

Generic Lightweight Druntime - DConf - May 2017 Motivation DMD replaces certain code with calls to traditional untyped functions in the runtime library Call created during IR code gen phase TypeInfo inheritance chain Dynamically dispatched calls that are slow Generic Lightweight Druntime - DConf - May 2017

Generic Lightweight Druntime - DConf - May 2017 The Idea - Lowering Lower the code to calls to templates in Druntime Lowering happens during the semantic analysis phase Replace complex code generation in compiler and Druntime with simple templates Implementation focus shifts on Druntime Less duplication! Safety! Speedup! Generic Lightweight Druntime - DConf - May 2017

Generic Lightweight Druntime - DConf - May 2017 The Process if (__cmp(a, b)){…} int __cmp(T)(const T[] lhs, const T[] rhs) if (__traits(isScalar, T)) { … } int[2] a = [1, 2]; int[2] b = [3, 4]; if (a > b){…} Compiler semantic Runtime template Generic Lightweight Druntime - DConf - May 2017

Generic Lightweight Druntime - DConf - May 2017 First Steps Identify some functions and replace them Array operations Compare Equals Assign Evaluate performance Generic Lightweight Druntime - DConf - May 2017

Pros – [PRs] To Be Reviewed… Lots of contributors, significantly fewer than on phobos PR contrast => not enough compiler experts to understand internals and review PRs Generic Lightweight Druntime - DConf - May 2017

Pros – Simplify the Compiler dmd/src/ddmd/e2ir.d - Bits of code with various optimizations or specifics sprinkled all over the compiler Remove them, add straightforward implementation in druntime AST => IR where the call to the array compare function is generate During semantic analysis phase we are not aware that this will happen Generic Lightweight Druntime - DConf - May 2017

Pros – Reduce the Use of TypeInfo druntime/src/object.d Typeinfo - used to generate runtime info about types Used by the compiler Inheritance hierarchy Find the proper override => lots of indirections (slow at times) Lots of duplicate code. Lots of code that can be removed once templates are in place. Generic Lightweight Druntime - DConf - May 2017

Pros – Templates Everywhere! Generic Compiled when necessary Richer type information (@nogc, nothrow, pure) Inlineable Performance increase! We have the positive experience of C++ that has switched full bore from virtuals/indirect calls/traditional designs to template-based designs maintaining great speed and usability. Of course, we are certain we can greatly improve on that. Primitive types (e.g. int) cannot be directly held in the container. You must write a wrapper class (such as Java's Integer) to hold such items. The container is not typesafe. You must cast items from Object to their real type when removing them from the container. There are also no particular limits on the actual types of the items in the container, meaning a given container is capable of holding a random mix of types. All of this up- and down-casting can have a negative impact on performance. Generic Lightweight Druntime - DConf - May 2017

Pros – Performance! (Before) druntime/src/object.d struct C { int i; int opCmp(C c) const { return i - c.i; } } druntime/src/object.d druntime/src/adi.d Struct with opEquals 1. call to adCmp2 2. call to the appropriate compare in TypeInfo 3. call opEquals defined for that struct Generic Lightweight Druntime - DConf - May 2017

Pros – Performance! (After) dmd/expression.d struct C { int i; int opCmp(C c) const { return i - c.i; } } Struct with opEquals 1. call to template 2. call to opEquals Less indirection, templates are inlined => faster code druntime/src/object.d Generic Lightweight Druntime - DConf - May 2017

Cons – object.d Bloating Code from druntime/src/rt/typeinfo moves into object.d Compare, equals, getHash All template overloads defined in object.d New modules in druntime: import tradeoff Generic Lightweight Druntime - DConf - May 2017

Cons – Compilation Speed Simple projects - no difference Compilation speed may decrease Compile the templates several times Generic Lightweight Druntime - DConf - May 2017

Generic Lightweight Druntime - DConf - May 2017 Current State Array compare (__cmp) integrated in dmd and druntime Array equals in development (some dmd tests failing) Benchmarks Backburner: array assign, code cleanup Compare finish + benchmarks Array equals finished: passes druntime and phobos unittests, fails dmd test because of issue with compile time evaluation of static variables Generic Lightweight Druntime - DConf - May 2017

Generic Lightweight Druntime - DConf - May 2017 Benchmarks bool fun(C[] array1, C[] array2) {     return array1[] < array2[]; } void test(int size) {     C[n] array1, array2;     // create arrays of size n     ...     auto r = benchmark!({ fun(array1, array2); })(10_000); Generic Lightweight Druntime - DConf - May 2017

Array Compare Speedup (1/2) Generic Lightweight Druntime - DConf - May 2017

Array Compare Speedup (2/2) Generic Lightweight Druntime - DConf - May 2017

Array Equals Speedup (1/2) Generic Lightweight Druntime - DConf - May 2017

Array Equals Speedup (2/2) Generic Lightweight Druntime - DConf - May 2017

Generic Lightweight Druntime - DConf - May 2017 Hot Code! Integer and string comparisons happen often Lots of compile time string comparisons (equality) Several sorting algorithms in Phobos Generic Lightweight Druntime - DConf - May 2017

Generic Lightweight Druntime - DConf - May 2017 Further Work __equals, array assign Templates for non-array types Destructors, switch statements Code cleanup Generic Lightweight Druntime - DConf - May 2017

Generic Lightweight Druntime - DConf - May 2017 Conclusions Refactor druntime to use lowering and templates Simplify compiler logic and increases runtime performace  Simplify the language implementation and make it more powerful Speedup between 1x and 30x Generic Lightweight Druntime - DConf - May 2017