Druntime Lucia Madalina Cojocaru University POLITEHNICA of Bucharest luciamcojocarugmailcom Generic Lightweight Druntime DConf May 2017 2 How Witch hunt compiler dark magic Lower to straight D code in ID: 596848
Download Presentation The PPT/PDF document "Generic Lightweight" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Generic Lightweight
Druntime
Lucia Madalina Cojocaru University POLITEHNICA of Bucharestlucia.mcojocaru@gmail.comSlide2
Generic Lightweight Druntime - DConf - May 2017
2Slide3
How?Witch hunt: compiler dark magicLower to straight D code (in Druntime)Burn them at the stake!Refactor druntime to do their job
Generic Lightweight Druntime - DConf - May 2017
3Slide4
MotivationDMD replaces certain code with calls to traditional untyped functions in the runtime libraryCall created during IR code gen phaseTypeInfo inheritance chainDynamically dispatched calls that are slow
Generic Lightweight Druntime - DConf - May 2017
4Slide5
The Idea - LoweringLower the code to calls to templates in DruntimeLowering happens during the semantic analysis phaseReplace complex code generation in compiler and Druntime with simple templatesImplementation focus shifts on
DruntimeLess duplication! Safety! Speedup!Generic Lightweight Druntime - DConf - May 2017
5Slide6
The ProcessGeneric Lightweight Druntime - DConf - May 20176
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){…}Slide7
First StepsIdentify some functions and replace themArray operationsCompareEqualsAssignEvaluate performance
Generic Lightweight Druntime - DConf - May 2017
7Slide8
Pros – [PRs] To Be Reviewed…
Generic Lightweight Druntime - DConf - May 20178Slide9
Pros – Simplify the Compiler
Generic Lightweight Druntime - DConf - May 20179
dmd/src/ddmd/e2ir.dSlide10
Pros – Reduce the Use of TypeInfo
Generic Lightweight Druntime - DConf - May 201710
druntime/src/object.dSlide11
Pros – Templates Everywhere!GenericCompiled when necessaryRicher type information (@nogc, nothrow, pure)
InlineablePerformance increase!Generic Lightweight Druntime - DConf - May 2017
11Slide12
Pros – Performance! (Before)struct C{
int
i; int
opCmp
(C c)
const
{ return
i
-
c.i
; }
}
Generic Lightweight Druntime - DConf - May 2017
12
druntime
/
src
/
object.d
druntime
/
src
/
adi.ddruntime/src/object.dSlide13
Pros – Performance! (After)struct C{
int
i; int
opCmp
(C c)
const
{ return
i
-
c.i
; }
}
Generic Lightweight Druntime - DConf - May 2017
13
dmd
/
expression.d
druntime
/
src
/
object.dSlide14
Cons – object.d BloatingCode from druntime/src/rt/
typeinfo moves into object.dCompare, equals, getHashAll template overloads defined in object.dNew modules in
druntime: import tradeoffGeneric Lightweight Druntime - DConf - May 201714Slide15
Cons – Compilation SpeedSimple projects - no differenceCompilation speed may decreaseCompile the templates several times
Generic Lightweight Druntime - DConf - May 201715Slide16
Current StateArray compare (__cmp) integrated in dmd and druntimeArray equals in development (some dmd tests failing)
BenchmarksBackburner: array assign, code cleanupGeneric Lightweight Druntime - DConf - May 2017
16Slide17
Benchmarksbool 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
17Slide18
Array Compare Speedup (1/2)
Generic Lightweight Druntime - DConf - May 2017
18Slide19
Array Compare Speedup (2/2)
Generic Lightweight Druntime - DConf - May 2017
19Slide20
Array Equals Speedup (1/2)
Generic Lightweight Druntime - DConf - May 2017
20Slide21
Array Equals Speedup (2/2)
Generic Lightweight Druntime - DConf - May 2017
21Slide22
Hot Code!Integer and string comparisons happen oftenLots of compile time string comparisons (equality)Several sorting algorithms in Phobos
Generic Lightweight Druntime - DConf - May 2017
22Slide23
Further Work__equals, array assignTemplates for non-array typesDestructors, switch statementsCode cleanup
Generic Lightweight Druntime - DConf - May 2017
23Slide24
ConclusionsRefactor druntime to use lowering and templatesSimplify compiler logic and increases runtime performace Simplify the language implementation and make it more powerful
Speedup between 1x and 30xGeneric Lightweight Druntime - DConf - May 2017
24