List of lectures TDDA69 Data and Program Structure

advertisement
Listoflectures
TDDA69DataandProgramStructure
GarbageCollectionandNativeCode
CyrilleBerger
1IntroductionandFunctionalProgramming
2ImperativeProgrammingandDataStructures
3Environment
4Evaluation
5ObjectOrientedProgramming
6Macrosanddecorators
7VirtualMachinesandBytecode
8GarbageCollectionandNativeCode
9DistributedComputing
10LogicProgramming
11Summary
2/46
Lecturecontent
NativeCodeGeneration
Just-In-Time,Ahead-Of-Time
FromBytecodetoNativecode
V8CaseStudy
NativeCodeGeneration
MemoryManagment
GarbageCollection
AllocationAlgorithms
GarbageCollectionAlgorithms
3/46
PerformanceofVirtualMachines
VirtualMachinesarefasterthantree
vistorsinterpreter...
...butstillslowerthannativecode
Just-In-Time,Ahead-Of-Time
C++isanaverage15timesfasterthanPython
C++isanaverage5timesfasterthanJavaScript
V8
Source:http://
benchmarksgame.alioth.debian.org/
5
Just-In-Time,Ahead-Of-Time
Ahead-Of-Time
Ahead-Of-Time
Compiledbeforedistribution
classicaltypeofcompilation
Codeistranslatedtonativecodebeforerunning
theapplication
GCC,VisualStudioC++,ART...
Simple
Examples:GCC,VisualStudio...
Compiledatinstallation
Theprogramisoptimisedfortheplatform
Morecomplexinstallation
Examples:ART...
Just-In-Time(JIT)
Codeistranslatedtonativecodewhilerunning
theapplication
JavaVM,.NET,Dalvik...
7
8
Just-In-Time
Adaptiveoptimization
classA
{
functiona(){...}
functionb(){...a()...}
}
classB:A
{
}
a()canbeinlinedinb()
Nowweload:
classC:A
{
functiona(){...}
}
a()cannotbeinlinedinb()
Dynamicrecompilation
TheVirtualMachinerecompilespartofthe
programduringexecution
Adaptiveoptimization
Optimizationdependingonthecurrentcontext
Profile-guidedoptimization
Fordynamicprogramminglanguage,
itcanoptimizeforthedifferenttypes
9
10
StackMachinetoregistermachine
Usetheregistersaslocalcache
Example:
FromBytecodetoNativecode
Letstranslatethefollowingonathreeregistersmachine:
Oneregistercontainsapointertothestack,theothertwoarefor
arithmeticoperations
PUSH1
PUSH2
ADD
PUSH4
PUSH5
MUL
DIV
12
Nativecodefordynamictyping(1/2)
Nativecodeforstatictyping
Generatingnativecodeforstatictyping
isstraightforward
Whichfunctiontocallisknown
Consider:
functionf(a)
{
console.log('Value:'+a)
}
Whatisthetypeofa?
inta=2;
f(a);
Whichoperatorstocallisknown
floatb=2.0;
floatc=2.0;
f(b+c);
13
14
Nativecodefordynamictyping(2/2)
Treatallvariablesasapointertoan
object
Treatallvariablesas32/64bitsintegers
V8CaseStudy
Ifthefirstbitis1,thenthevalueisanobject
Ifthefirstbitis0,thenthevalueisa31/63bitsinteger
Dynamicrecompilation,compilea
versionoffunctionforeachpossible
types
15
ObjectRepresentationinV8(1/2)
V8CaseStudy
Doesnotuseadictionnary!
Insteadcreateahiddenclassdefinition
V8istheJavaScriptengineusedin
GoogleChromeandnode.js
ImplementsaJITcompiler
functionPoint(x,y)
{
this.x=x;
this.y=y;
}
varp=newPoint(11,22);
varq=newPoint(33,44);
p:[11,22]
q:[33,44]
Pointlayout
name:offset
x:0
y:1
17
ObjectRepresentationinV8(1/2)
functionPoint(x,y)
{
this.x=x;
this.y=y;
}
varp=newPoint(11,22);
varq=newPoint(33,44);
q.z=55
Codegeneration
Pointlayout
name:offset
Whathappenwhen
addingadynamic
member?
p:[11,22]
q:[33,44]
18
Generateonenativefunctionperparametertype
varp=newPoint(11,22);
varq=newPoint(33,44);
functionnorm(p)
{
returnmath.sqrt(p.x*p.x+p.y*p.y)
}
norm(p)
norm(q)
q.z=55
norm(q)
Pointlayout
name:offset
x:0
Pointlayout
name:offset
x:0
y:1
Pointlayout
name:offset
x:0
y:1
z:2
Willgenerateonenativefunctionforthetwolayouts
Inthiscasethecompilercaneasilyguessthetypesofthe
variablesbasedonthearguments
19
20
Advantages/InconvenientsofAhead-Of-Time
Theproblemofglobalvariables
varbase=10;
functiondoSomething(val)
{
doSomethingElse()
returnbase+val;
}
Advantages
Programisreadytouseafterinstallation
Inconvenients
Staticoptimizationgiveslowercode
WhathappenifdoSomethingElse()
changethetypeofbase?
Anotherdifficultycausedbysideeffects
21
Advantages/InconvenientsofJust-In-Time
22
NativeCodeGeneration-Summary
Advantages
Benefits:
Adaptiveoptimization
Moresuitablefordynamicprogramming
language
Fastertoexecute
Inconvenients
Complexity
Lessportable
Inconvenients
Slowdownduringexecutionduetocompilation
Debetableiftheadaptiveoptimizationgivesa
noticeableperformanceimprovments
23
24
Low-level
Twooperations
MemoryManagment
malloc:allocateacontinuousarrayofbytes
free:disallocatethearray
Problem:peopleforgettofree
memory,whichintroducesleaksin
theprogram
Solution:useagarbagecollector
26
GarbageCollection
GarbageCollection
Automaticallyfreeunusedmemory
Needanallocationalgorithm
Needagarbagecollectionalgorithm
28
OneBigFreeSpace(1/2)
Pros
Verysimpleandfastallocation
algorithm
Freespacenotsubjectto
fragmentation
AllocationAlgorithms
Cons
Requiresacompacting
garbagecollectionalgorithm
(slower)
Thatis,in-useblockshavetobe
shiftedandpointershavetobe
adjustedduringgarbage
collection
30
OneBigFreeSpace(2/2)
Before:
FreeList
Pros
After:
Canuseanon-compactinggarbage
collectionalgorithm(faster)
Thatis,in-useblocksdonothaveto
beshiftedandpointersdonothave
tobeadjustedduringgarbage
collection
Cons
Morecomplicatedandslower
allocationalgorithm
Freespacesubjecttofragmentation
Inthefigure,youcan'tallocatea10wordblock,eventhoughthereare10
totalfreewords
31
32
FirstfitAllocation
Before:
BestfitAllocation
After(allocate3):
Before:
33
After(allocate3):
34
Whenallocationfails
Trytoallocate
Ifthere’snofreeblockbigenough...
Doagarbagecollection,thentryto
allocateagain
GarbageCollectionAlgorithms
Ifthere’sstillnofreeblockbigenough...
Increasethesizeoftheheap(ifpossible),
thentrytoallocateagain
Ifyoucan’tincreasethesizeoftheheap...
OutOfMemoryError!
35
ReferenceCounting(1/2)
ReferenceCounting(2/2)
Pros
Verysimple,non-compactinggarbagecollection
Heapmaintenancespreadthroughoutprogramexecution(instead
ofsuspendingtheprogramwhenthegarbagecollectorruns)
Cons
Extrawordinblockheadertoholdreferencecount
Fragile;ifyouforgettoadjustreferencecountsonanypointer
assignment(includingpassingpointersassubroutinearguments),
disastercanhappen
Majorproblem:Cannotgarbagecollectcircularlylinkeddata
structures
Header
nWords:thesizeoftheblockarea
refCount:Thenumberofpointerspointingtothis
block
37
Mark-Sweep(1/2)
38
Mark-Sweep(2/2)
Pros
Minimaloverheadinblockheader
Heapmaintenancenotrequiredoneverypointer
assignment(unlikereferencecounting)
Cangarbagecollectcircularlylinkeddata
structures
Header
nWords:thesizeoftheblockarea
mark:1ifinuse,0ifgarbage
Cons
Algorithm:
Doesn’tdealwithheapfragmentation
Suspendtheentiresystemduringcollection
Markallblocksreachablefromtheroot
Atthispoint,allunmarkedblocksareunreachable,hencegarbage
Sweepallunmarkedblocksintothefreelist(non-compacting)
39
40
CopyingGarbage
Tri-colormarking
Threecolors:
White:candidatefordeletion
Black:objectswithnoconnectiontowhiteobjects
andreachablefromroot
Gray:objectsreachablefromrootbutwith
possiblereferencestowhiteobjects
Variationofthemark-sweep
Copyin-useblocksfromthefromspacetothetopofthetospace(compacting)
Pros
Onebigfreespaceorganizationmeansverysimpleallocation
Compactinggarbagecollectionmeansheapfragmentationdoesnotoccur
Cons
Heaprequirestwicethememorythatwouldotherwisebeneeded—mostofthetime,halfofthismemoryis
“wasted”
41
Tri-colormarking-Algorithm
Initialisation
42
Tri-colormarking-Advantage
Canbeperformedon-the-fly
Whiteset:allobjectsnotdirectlyreachablefrom
root
Blackset:empty
Grayset:allobjectsdirectlyreachablefromroot
Noneedforfreezingtheapplication
Pickanobjectinthegrayset
Markasgrayallthewhiteobjectsitreferences
Markasblackthisobject
Endwhengraysetisempty
43
44
Generational
Summary
Inmanyprograms,thenewest
objectsarethemostlikelytobecome
unreachable
Hencetheideatoonlyputrecent
objectsinthewhiteset
Just-In-Timecompilation
NativeCodegeneration
GarbageCollectionandMemory
Management
45
46/46
Download