List of lectures TDDA69 Data and Program Structure

advertisement
Listoflectures
IntroductionandFunctionalProgramming
ImperativeProgrammingandDataStructures
3 Environment
4 Evaluation
5 ObjectOrientedProgramming
6 Macrosanddecorators
7 VirtualMachinesandBytecode
8 GarbageCollectionandNativeCode
9 DistributedComputing
10LogicProgramming
11Summary
1
2
TDDA69DataandProgramStructure
GarbageCollectionandNativeCode
CyrilleBerger
2/43
Lecturegoal
Lecturecontent
Learnhowtogenerate
nativecode
Learnhowmemoryismanagedin
differentprogramlanguaging
NativeCode
Just-In-Time,Ahead-Of-Time
FromBytecodetoNativecode
V8CaseStudy
Memory
GarbageCollection
AllocationAlgorithms
GarbageCollectionAlgorithms
3/43
4/43
PerformanceofVirtualMachines
NativeCodeGeneration
VirtualMachinesarefaster
thantreevistorsinterpreter...
...butstillslowerthannative
C++isanaverage15timesfasterthan
Python
C++isanavergae5timesfasterthan
JavaScriptV8
Source:http://
benchmarksgame.alioth.debian.org/
6
Just-In-Time,Ahead-Of-Time
Ahead-Of-Time
Just-In-Time,Ahead-Of-Time
classicaltypeofcompilation
Codeistranslatedtonativecodebefore
runningtheapplication
GCC,VisualStudioC++,ART...
Just-In-Time(JIT)
Codeistranslatedtonativecodewhile
runningtheapplication
JavaVM,.NET,Dalvik...
8
AdvantagesofJust-In-Time
Dynamicrecompilation
TheVirtualMachinerecompilespartofthe
programduringexecution
Adaptiveoptimization
FromBytecodetoNativecode
Optimizationdependingonthecurrent
context
Profile-guidedoptimization
Moresuitablefordynamic
programminglanguage
9
Nativecodeforstatictyping
StackMachinetoregistermachine
Generatingnativecodefor
statictypingisstraightforward
Whichfunctiontocallisknown
Usetheregistersaslocal
Example:
Letstranslatethefollowingonathreeregistersmachine:
Oneregistercontainsapointertothestack,theother
twoareforarithmeticoperations
PUSH1
PUSH2
ADD
PUSH4
PUSH5
MUL
DIV
inta=2;
f(a);
Whichoperatorstocallisknown
floatb=2.0;
floatc=2.0;
f(b+c);
11
12
Nativecodefordynamictyping(1/2)
Consider:
functionf(a)
{
console.log('Value:'+
a)
}
Whatisthetypeof
Nativecodefordynamictyping(2/2)
Treatallvariablesas
32bitsinteger
Ifthefirstbitis1,thenthevalueisanobject
Ifthefirstbitis0,thenthevalueisaninteger
Dynamicrecompilation
13
14
V8CaseStudy
V8CaseStudy
V8istheJavaScriptengineused
inGoogleChromeandnode.js
ImplementsaJIT
16
ClassRepresentationinV8(1/2)
ClassRepresentationinV8(1/2)
Whathappenwhen
addingadynamic
member?
Doesnotusea
Insteadcreateahiddenclassdefinition
functionPoint(x,y)
{
this.x=x;
this.y=y;
}
varp=newPoint(11,22);
varq=newPoint(33,44);
p:[11,22]
q:[33,44]
functionPoint(x,y)
{
this.x=x;
this.y=y;
}
varp=newPoint(11,22);
varq=newPoint(33,44);
q.z=55
Pointlayout
name:offset
x:0
y:1
Pointlayout
name:offset
p:[11,22]
q:[33,44]
Pointlayout
name:offset
x:0
Pointlayout
name:offset
x:0
y:1
Pointlayout
name:offset
x:0
y:1
z:2
17
Codegeneration
18
Theproblemofglobalvariables
varbase=10;
function
doSomething(val)
{
doSomethingElse()
returnbase+val;
Generateonenativefunctionperparameter
varp=newPoint(11,22);
varq=newPoint(33,44);
functionnorm(p)
{
returnmath.sqrt(p.x*p.x+p.y*p.y)
}
norm(p)
norm(q)
Whathappenif
doSomethingElse()changethe
typeofbase?
Anotherdifficultycausedbyside
effects
q.z=
55
norm(q)
Willgenerateonenativefunctionforthetwo
Inthiscasethecompilercaneasilyguessthe
typesofthevariablesbasedonthearguments
19
20
NativeCodeGeneration-Summary
Benefits:
Fastertoexecute
Inconvenients
MemoryManagment
Complexity
Lessportable
21
Low-level
Twooperations
malloc:allocateacontinuousarrayofbytes
free:disallocatethearray
Problem:peopleforgettofree
memory,whichintroducesleaksin
theprogram
Solution:useagarbagecollector
23
GarbageCollection
GarbageCollection
Automaticallyfreeunused
memory
AllocationAlgorithms
25
OneBigFreeSpace(1/2)
OneBigFreeSpace(2/2)
Before:
Pros
Verysimpleandfast
allocationalgorithm
Freespacenotsubjectto
fragmentation
After:
Cons
Requiresacompacting
garbagecollection
algorithm(slower)
Thatis,in-useblockshave
tobeshiftedandpointers
havetobeadjusted
duringgarbagecollection
27
28
FreeList
FirstfitAllocation
Before:
Pros
Canuseanon-compacting
garbagecollectionalgorithm
(faster)
Thatis,in-useblocksdonot
havetobeshiftedandpointers
donothavetobeadjusted
duringgarbagecollection
After(allocate3):
Cons
Morecomplicatedandslower
allocationalgorithm
Freespacesubjectto
fragmentation
Inthefigure,youcan'tallocatea
10-wordblock,eventhough
thereare10totalfreewords
29
Whenallocationfails
BestfitAllocation
Before:
30
Trytoallocate
After(allocate3):
Ifthere’snofreeblockbigenough...
Doagarbagecollection,thentryto
allocateagain
Ifthere’sstillnofreeblockbigenough...
Increasethesizeoftheheap(if
possible),thentrytoallocateagain
Ifyoucan’tincreasethesizeoftheheap...
OutOfMemoryError!
31
32
ReferenceCounting(1/2)
GarbageCollectionAlgorithms
Header
nWords:thesizeoftheblockarea
refCount:Thenumberofpointerspointingto
thisblock
34
Mark-Sweep(1/2)
ReferenceCounting(2/2)
Pros
Verysimple,non-compactinggarbagecollection
Heapmaintenancespreadthroughoutprogram
execution(insteadofsuspendingtheprogramwhen
thegarbagecollectorruns)
Cons
Header
Extrawordinblockheadertoholdreferencecount
Fragile;ifyouforgettoadjustreferencecountsonany
pointerassignment(includingpassingpointersas
subroutinearguments),disastercanhappen
Majorproblem:Cannotgarbagecollectcircularly
linkeddatastructures
nWords:thesizeoftheblockarea
mark:1ifinuse,0ifgarbage
Algorithm:
Markallblocksreachablefromtheroot
Atthispoint,allunmarkedblocksareunreachable,hence
Sweepallunmarkedblocksintothefreelist(non-
35
36
Mark-Sweep(2/2)
CopyingGarbage
Pros
Minimaloverheadinblockheader
Heapmaintenancenotrequiredonevery
pointerassignment(unlikereference
counting)
Cangarbagecollectcircularlylinkeddata
structures
Variationofthemark-sweep
Copyin-useblocksfromthefromspacetothetopofthetospace
(compacting)
Pros
Cons
Doesn’tdealwithheapfragmentation
Suspendtheentiresystemduringcollection
Onebigfreespaceorganizationmeansverysimpleallocation
Compactinggarbagecollectionmeansheapfragmentationdoesnotoccur
Cons
Heaprequirestwicethememorythatwouldotherwisebeneeded—mostofthetime,halfofthis
memoryis“wasted”
37
Tri-colormarking
38
Tri-colormarking-Algorithm
Threecolors:
Initialisation
White:candidatefordeletion
Black:objectswithnoconnectiontowhite
objectsandreachablefromroot
Gray:objectsreachablefromrootbutwith
possiblereferencestowhiteobjects
Whiteset:allobjectsnotdirectlyreachable
fromroot
Blackset:empty
Grayset:allobjectsdirectlyreachablefrom
root
Pickanobjectinthegray
Markasgrayallthewhiteobjectsitreferences
Markasblackthisobject
Endwhengraysetis
39
40
Generational
Tri-colormarking-Advantage
Canbeperformedon-the-fly
Inmanyprograms,the
newestobjectsarethemost
likelytobecomeunreachable
Hencetheideatoonlyput
recentobjectsinthewhiteset
Noneedforfreezingtheapplication
41
Summary
Just-In-Timecompilation
NativeCode
GarbageCollectionandMemory
Management
43/43
42
Download