In this pap er we explain how to tune Lisp co de for p erformance by intro ducing the prop er typ e declarations using the appropriate data structures and compiler information We also explain how eciency is achieved by the compilers These techniques ID: 11022
Download Pdf The PPT/PDF document "Howtomake Lisp gofasterthanC DidierVerna..." 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.
suchasstructs,arraysandhashtables.Arraysal-lowyoutostoreandaccessLispobjectsaccordingtoarectilinearcoordinatesystem,andthuscanbecon-sideredastheequivalentofmalloc'edorcalloc'edmemoryareasinC.TheLispfunctionforcreat-ingarraysismake-array.InLisp,arrayscanbemultidimensional.Onlisting2,youcanseeacalltoarray-dimensionwhichretrievesthearray'srstranksize(noneedtostorethisinformationinadatastructureasinC).Rememberthatweareusing1Dlineararraystorepresentourimages.Thefunctionforaccessingarrayelementsisarefandassignationisperformedviasetf.Unsurprisingly,dotimesisamacroperformingaloopinamannersimilartotheforlanguageconstructinC.RunningthisfunctioninaLispinterpretershowsthatitisapproximately2300timesslowerthantheCversion(systemtimeandgarbagecollectionex-cluded).Thecompiledversionhoweverisonly60timesslowerthantheequivalentCcode.Finally,evenwithoptimizationturnedon(seesection5.5),wearestill20timesslowerthanC.Tounderstandwhywearegettingthispoorperfor-mance,onehastorealizethatourLispcodeisun-typed:contrarytoC,thevariablesandfunctionar-gumentsweusedcouldholdanyLispobject.Forinstance,weusearray-dimensiononthefunctionparameterto,butnothingpreventsusfrompassingsomethingelsethananarray;weperformarithmeticoperationsonthefunctionparameterval,butnoth-ingpreventsusfrompassingsomethingelsethananumber.Asaconsequence,thecompiledLispcodehastocheckdynamicallythatthevariablesweuseareofthepropertypewithrespecttotheoperationswewanttoapplytothem.OurnextstepshouldthenbetoprovidetypeinformationtotheLispcompiler,justaswedoinC.5TypingLispCode5.1TypingmechanismsTheCommon-Lispstandardprovidesmeanstode-claretheexpectedtypesofLispobjectsatcompiletime.Itshouldbenotedhoweverthatoneisneverforcedtodeclarethetypeofavariable:typescanbedeclaredwhentheyareknown,orleftunspeciedotherwise.TheLispcompilersareexpectedtodothebesttheycanaccordingtotheinformationtheyhave. Inaway,sincethestandardizationofCommon-Lisp,itisnotcorrectanymoretosaythatLispisadynam-icallytypedlanguage:itcanbeeitherdynamicallyorstaticallytypedattheprogrammer'swill.TherearedierentwaystospecifytypesinCommon-Lisp.Therstoneisbypassingspecicargumentstofunctions.Forinstance,ifyouwanttocreateanarrayandyouknowthatthisarraywillonlycontainsingleprecisionoatingpointnumbers,youcanpassthe:element-typekeywordparametertothefunc-tionmake-arraylikethis: (make-arraysize:element-type'single-float)Thenextwaytospecifytypesisbymeansofdecla-rations:thismechanismisusedtoprecisethetypeofafunctionparameterorafreshlyboundvariable.Atypedeclarationshouldappearneartherstoc-currenceofthevariableitrefersto.Listing3showsthenextversionofouradditionalgorithm,withtypedeclarationsissued. (defunadd(tofromval)(declare(type(simplearraysinglefloat())tofrom))(declare(typesinglefloatval))(let((size(arraydimensionto0)))(dotimes(isize)(setf(areftoi)(+(areffromi)val))))) Listing3:PixelAddition,TypedLispVersion Asyoucansee,wehavedeclaredthe(expected)typeofthe3functionparameters:twoarraysofsingle-floatvalues(inCommon-Lisp,simplear-raydenotestheequivalentofCarrays;thereareotherkindsofarraysthatwewillnotdescribehere),andasingle-floatparameter.The(*)specica-tionindicatesthatthearraysare1D,buttheactualsizeisunknown(atcompile-time).2Darrayswouldhavebeendescribedwith(**)forinstance.Athirdwaytoprovidetypedeclarationswillbede-scribedinsection6.2.5.2ObjectsRepresentationTounderstandwhytypedeclarationsmatterforop-timizingLispperformance,onehastorealizetheim-plicationsofdynamictypingabit.SinceLispobjectscanbeofanytype(worse:ofanysize),thevariablesinLispdon'tcarrytypeinformation.Rather,theobjectsthemselvesareresponsibleforprovidingtheirowntype.Thismeansthatmostofthetime,Lisp