AOT - Mono Project

文章推薦指數: 80 %
投票人數:10人

Ahead of Time Compilation or AOT is a feature of the Mono runtime code generator. The Mono code generator can operate in two modes: Just-in-Time compilation ... Mono Menu Home Download Documentation News Community Youarehere:docs advanced aot Tableofcontents Tableofcontents AOT EditpageonGitHub AheadofTimeCompilationorAOTisafeatureoftheMonoruntimecodegenerator. TheMonocodegeneratorcanoperateintwomodes:Just-in-TimecompilationorJIT,andAhead-of-TimecompilationorAOT. AOTcompilationworksintwostages.Thefirststageconsistsofprecompilingtheassemblies.Thisisamanualprocessthatindividualdeploymentsmustdo.Thesecondstageisautomatic,theMonoruntimewillautomaticallyloadanyprecompiledcodethatyouhavegenerated. GeneratingAOTcodefromanassemblyisverysimple,justinvoketheMonoruntimewiththe--aotflag,likethis: mono--aotprogram.exe Thiswillgenerateafilecalled“program.exe.so”,whichcontainsthenativecodethatwasprecompiledbyMonoformostoftheILmethods.The--aotflagbydefaultwilllimititselftoILmethodswhichwillgiveyouthemostbenefits,butwillnotcompileabsolutelyeverythingyouneed.Seebelowformoredetails. AlthoughtheJITmodeisveryfast,andthedefaultoptimizationsinMonohavebeentunedtoprovideagoodbalancebetweenoptimizationsandJITspeed,AOTcompilationprovidesafewextrabenefits: Reducedstartuptime. Increasedmemorysharing. Potentialbetterperformance. Tableofcontents 1FullAOT 1.1KnownLimitations 1.1.1Limitation:Platform 1.1.2GenericValueTypeSharing 2SupportedPlatforms 3ReducedStartupTime 4IncreasedMemorySharing 5PotentialBetterPerformance 6Limitations 7Discussion 8AOTingallthesystemlibraries FullAOT Insomeoperatingsystemconfigurations(mostlyembeddedsystems)theoperatingsystemservicesforgeneratingcodedynamicallyarenotavailable,thispreventsMono’sJITfromworking.Inthosesystems,youcanuse--aot=fulltoensurethatMonoprecompileseverything,andthenusetheoption--full-aottoensurethatMononeverusestheJITengine. #DoafullAOT: $mono--aot=fullmscorlib.dll $mono--aot=fullsample.exe   #Run,butrequestthatMononevertriestoJIT: $mono--full-aotsample.exe FullAOTisafairlystraightforwardprocessexceptinthecaseofgenericinstantiations.InthosecasesMonomustperformastaticanalysisofthecodeanddetermineallofthepossibleinstantiationsofatypeandgeneratethecoderequired.ForexampleifaprogramusesaListandaListMonowilldetectthisandgenerateallofthereferencedmethodsforbothdatatypes. KnownLimitations IfyoudependonthefullAOToptionbecauseofOSlimitations,youshouldmakesuretotestyoursoftwareusingthe--full-aotoptiontoensurethatnodynamiccodeisusedbyyourapplication. ThistestingisrequiredbecausesomeofMono’sclasslibrariesgeneratecodedynamically(forexampleLINQ’sExpression.Compile()methodforexpressionASTs)orloadcodeatruntime(forexamplethedefaultoperationmodefortheXMLserializer,seeMONO_XML_SERIALIZER_THSonthemanualpagetoconfigurethis). Limitation:Platform FullAOTcurrentlyonlyworksonX86/AMD64/ARM. GenericValueTypeSharing ThisversionofAOTcompilationextendstheAOTcompilertosupportgenericcodegenerationforvaluetypes.Thiscanbeeitheranoptimization(generatefewerversionsofFoowhereXisavaluetype)tofillingholeswherepreviouslyyouwouldgetaruntimefailureduetoaFoowhereXisvaluetypefromnotbeingimplemented. SupportedPlatforms AOTisonlyavailableinafewplatforms: Mono:X86 x86-64 Mono:ARM. ReducedStartupTime Atstartup,theMonoruntimewillprobeifanAOTversionoftheassemblylivesside-by-side,andifso,insteadofJITingthemethodsinthegivenassembly,itwillloadthenativecodefromtheside-by-sidefile. Thisisparticularlyusefulforlargeprogramsthatmightneedtoexecutealotofcodebeforetheyareoperational(largeclasslibrariesforexample). IncreasedMemorySharing ThecodegeneratedbyAOTcompilationisposition-independentcode(PIC).Monowillloadthiscodeusingthe“mmap”kernelcall,andallofthecodewillactuallybesharedacrossmultipleMonoinstancesinthesystem. NotonlywilltheJITnotwastetimeormemoryingeneratingthecode,butthecodethatisexecutedisthesamecopyofcodeinmemorythatissharedacrossmultipleprocessesinasystem. PotentialBetterPerformance Whenyoupre-compileanassemblywiththe--aotflagtoMono,youalsocanturnonextraoptimizationsthatarenotpartofthedefaultoptimizationsthatMonoapplieswhileJITing.JITinghastobalancestartup-timevs.performanceofthegeneratedcode.Thismeansthatthemostadvancedoptimizationthatrequireamorethoroughcodeanalysisandwhichrunslowerarenotenabledbydefault. WithAOT,youcanturnonandoffspecificoptimizationsthatyouwanttoapplytoyourcode,optimizationsthatyouwouldnotusewiththeJITengineastheywouldslowdownyourprogramstartuptoomuch. Onecommonuseis: mono--aot-O=allprogram.exe The“all”flagtothe“-O”commandlineoptionwillturnonalloptimizations,butwesuggestthatyouactuallyprofileandtestwhethersomeparticularoptimizationsimproveordecreasetheperformanceofyourcode,asnotalloptimizationsworkequallywellwithalldifferentcodepatterns. Limitations CodegeneratedbytheAOTcompilationstepispositionindependent,unliketheJITcodewhichistunedfortheactualexecutionoftheprocess.ThismeansthatcertainprogramsmightrunslowerasthegeneratedcodeismoregeneralthanthespecificcodethattheJITcanproduce. IfyouwanttodisabletheuseoftheAOTgeneratedcodeforaparticularprogramexecution,usethe-O=-aotcommandlineflagtotheruntime. Discussion DocumentationontheMonoAOTimplementationisfoundintheMono:Runtime:Documentation:AOTpage. AOTingallthesystemlibraries YoucanusethefollowingcommandstoAOTallofthelibrariesonyoursystem: sudomono--aot/usr/lib/mono/4.5/mscorlib.dll foriin/usr/lib/mono/gac/*/*/*.dll;dosudomono--aot$i;done



請為這篇文章評分?