Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit39d5d12

Browse files
dsymeKevinRansom
authored andcommitted
[CompilerPerf] reduce allocations (#2615)
* reduce allocations* reduce allocations* remove #if
1 parent0464323 commit39d5d12

17 files changed

+198
-155
lines changed

‎src/absil/il.fs‎

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ open Microsoft.FSharp.Compiler.AbstractIL.Internal.Library
1515
openSystem.Collections
1616
openSystem.Collections.Generic
1717
openSystem.Collections.Concurrent
18+
openSystem.Runtime.CompilerServices
1819

1920
letlogging=false
2021

@@ -2132,60 +2133,61 @@ let isILDoubleTy ty = isILValuePrimaryAssemblyTy ty tname_Double
21322133
// Rescoping
21332134
// --------------------------------------------------------------------
21342135

2135-
letqrescope_scoref scoref scoref_old=
2136-
match scoref,scoref_oldwith
2137-
|_,ILScopeRef.Local-> Some scoref
2138-
| ILScopeRef.Local,_-> None
2139-
|_,ILScopeRef.Module_-> Some scoref
2140-
| ILScopeRef.Module_,_-> None
2141-
|_-> None
2142-
letqrescope_tref scoref(x:ILTypeRef)=
2143-
match qrescope_scoref scoref x.Scopewith
2144-
| None-> None
2145-
| Some s-> Some(ILTypeRef.Create(s,x.Enclosing,x.Name))
2146-
2147-
letrescopeILScopeRef x y=match qrescope_scoref x ywith Some x-> x| None-> y
2148-
letrescopeILTypeRef x y=match qrescope_tref x ywith Some x-> x| None-> y
2136+
letrescopeILScopeRef scoref scoref1=
2137+
match scoref,scoref1with
2138+
|_,ILScopeRef.Local-> scoref
2139+
| ILScopeRef.Local,_-> scoref1
2140+
|_,ILScopeRef.Module_-> scoref
2141+
| ILScopeRef.Module_,_-> scoref1
2142+
|_-> scoref1
2143+
2144+
letrescopeILTypeRef scoref(tref1:ILTypeRef)=
2145+
letscoref1= tref1.Scope
2146+
letscoref2= rescopeILScopeRef scoref scoref1
2147+
if scoref1=== scoref2then tref1
2148+
else ILTypeRef.Create(scoref2,tref1.Enclosing,tref1.Name)
21492149

21502150
// ORIGINAL IMPLEMENTATION (too many allocations
21512151
// { tspecTypeRef=rescopeILTypeRef scoref tref;
21522152
// tspecInst=rescopeILTypes scoref tinst }
2153-
let recrescopeILTypeSpecQuick scoref(tspec:ILTypeSpec)=
2154-
lettref= tspec.TypeRef
2155-
lettinst= tspec.GenericArgs
2156-
letqtref= qrescope_tref scoref tref
2157-
if isNil tinst&& Option.isNone qtrefthen
2158-
None(* avoid reallocation in the common case*)
2153+
let recrescopeILTypeSpec scoref(tspec1:ILTypeSpec)=
2154+
lettref1= tspec1.TypeRef
2155+
lettinst1= tspec1.GenericArgs
2156+
lettref2= rescopeILTypeRef scoref tref1
2157+
2158+
// avoid reallocation in the common case
2159+
if tref1=== tref2then
2160+
if isNil tinst1then tspec1else
2161+
lettinst2= rescopeILTypes scoref tinst1
2162+
if tinst1=== tinst2then tspec1else
2163+
ILTypeSpec.Create(tref2, tinst2)
21592164
else
2160-
match qtrefwith
2161-
| None-> Some(ILTypeSpec.Create(tref, rescopeILTypes scoref tinst))
2162-
| Some tref-> Some(ILTypeSpec.Create(tref, rescopeILTypes scoref tinst))
2163-
2164-
andrescopeILTypeSpec x y=
2165-
match rescopeILTypeSpecQuick x ywith
2166-
| Some x-> x
2167-
| None-> y
2165+
lettinst2= rescopeILTypes scoref tinst1
2166+
ILTypeSpec.Create(tref2, tinst2)
21682167

21692168
andrescopeILType scoref typ=
21702169
match typwith
21712170
| ILType.Ptr t-> ILType.Ptr(rescopeILType scoref t)
21722171
| ILType.FunctionPointer t-> ILType.FunctionPointer(rescopeILCallSig scoref t)
21732172
| ILType.Byref t-> ILType.Byref(rescopeILType scoref t)
2174-
| ILType.Boxed cr->
2175-
match rescopeILTypeSpecQuick scoref crwith
2176-
| Some res-> mkILBoxedType res
2177-
| None-> typ// avoid reallocation in the common case
2178-
| ILType.Array(s,ty)-> ILType.Array(s,rescopeILType scoref ty)
2179-
| ILType.Value cr->
2180-
match rescopeILTypeSpecQuick scoref crwith
2181-
| Some res-> ILType.Value res
2182-
| None-> typ// avoid reallocation in the common case
2173+
| ILType.Boxed cr1->
2174+
letcr2= rescopeILTypeSpec scoref cr1
2175+
if cr1=== cr2then typelse
2176+
mkILBoxedType cr2
2177+
| ILType.Array(s,ety1)->
2178+
letety2= rescopeILType scoref ety1
2179+
if ety1=== ety2then typelse
2180+
ILType.Array(s,ety2)
2181+
| ILType.Value cr1->
2182+
letcr2= rescopeILTypeSpec scoref cr1
2183+
if cr1=== cr2then typelse
2184+
ILType.Value cr2
21832185
| ILType.Modified(b,tref,ty)-> ILType.Modified(b,rescopeILTypeRef scoref tref, rescopeILType scoref ty)
21842186
| x-> x
21852187

21862188
andrescopeILTypes scoref i=
21872189
if isNil ithen i
2188-
else List.map(rescopeILType scoref) i
2190+
else List.mapq(rescopeILType scoref) i
21892191

21902192
andrescopeILCallSig scoref csig=
21912193
mkILCallSig(csig.CallingConv,rescopeILTypes scoref csig.ArgTypes,rescopeILType scoref csig.ReturnType)

‎src/absil/illib.fs‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,18 +310,30 @@ module List =
310310

311311
ch[][] l
312312

313+
let reccheckq l1 l2=
314+
match l1,l2with
315+
| h1::t1,h2::t2-> h1=== h2&& checkq t1 t2
316+
|_->true
317+
313318
letmapq(f:'T->'T)inp=
314319
assertnot(typeof<'T>.IsValueType)
315320
match inpwith
316321
|[]-> inp
322+
|[h1a]->
323+
leth2a= f h1a
324+
if h1a=== h2athen inpelse[h2a]
325+
|[h1a; h1b]->
326+
leth2a= f h1a
327+
leth2b= f h1b
328+
if h1a=== h2a&& h1b=== h2bthen inpelse[h2a; h2b]
329+
|[h1a; h1b; h1c]->
330+
leth2a= f h1a
331+
leth2b= f h1b
332+
leth2c= f h1c
333+
if h1a=== h2a&& h1b=== h2b&& h1c=== h2cthen inpelse[h2a; h2b; h2c]
317334
|_->
318335
letres= List.map f inp
319-
let reccheck l1 l2=
320-
match l1,l2with
321-
| h1::t1,h2::t2->
322-
System.Runtime.CompilerServices.RuntimeHelpers.Equals(h1,h2)&& check t1 t2
323-
|_->true
324-
if check inp resthen inpelse res
336+
if checkq inp resthen inpelse res
325337

326338
letfrontAndBack l=
327339
let recloop acc l=
@@ -457,6 +469,19 @@ module List =
457469
letexistsSquared f xss= xss|> List.exists(fun xs-> xs|> List.exists(fun x-> f x))
458470
letmapiFoldSquared f z xss= mapFoldSquared f z(xss|> mapiSquared(fun i j x->(i,j,x)))
459471

472+
[<Struct>]
473+
typeValueOption<'T>=
474+
| VSomeof'T
475+
| VNone
476+
memberx.IsSome=match xwith VSome_->true| VNone->false
477+
memberx.IsNone=match xwith VSome_->false| VNone->true
478+
memberx.Value=match xwith VSome r-> r| VNone-> failwith"ValueOption.Value: value is None"
479+
480+
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
481+
moduleValueOption=
482+
let inlineofOption x=match xwith Some x-> VSome x| None-> VNone
483+
let inlinebind f x=match xwith VSome x-> f x| VNone-> VNone
484+
460485
moduleString=
461486
letindexNotFound()= raise(new System.Collections.Generic.KeyNotFoundException("An index for the character was not found in the string"))
462487

‎src/fsharp/AccessibilityLogic.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ let private IsTyconAccessibleViaVisibleTo ad (tcrefOfViewedItem:TyconRef) =
123123
/// Indicates if given IL based TyconRef is accessible. If TyconRef is nested then we'll
124124
/// walk though the list of enclosing types and test if all of them are accessible
125125
letprivateIsILTypeInfoAccessible amap m ad(tcrefOfViewedItem:TyconRef)=
126-
letscoref,enc,tdef= tcrefOfViewedItem.ILTyconInfo
126+
let(TILObjectReprData(scoref,enc,tdef))= tcrefOfViewedItem.ILTyconInfo
127127
let reccheck parentTycon path=
128128
letilTypeDefAccessible=
129129
match parentTyconwith

‎src/fsharp/AttributeChecking.fs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ let GetAttribInfosOfEntity g amap m (tcref:TyconRef) =
138138
//| Some args -> f3 args
139139
//| None -> None
140140
#endif
141-
| ILTypeMetadata(scoref,tdef)->
141+
| ILTypeMetadata(TILObjectReprData(scoref,_,tdef))->
142142
tdef.CustomAttrs|> AttribInfosOfIL g amap scoref m
143143
| FSharpOrArrayOrByrefOrTupleOrExnTypeMetadata->
144144
tcref.Attribs|> List.map(fun a-> FSAttribInfo(g, a))
@@ -190,7 +190,7 @@ let TryBindTyconRefAttribute g m (AttribInfo (atref,_) as args) (tcref:TyconRef)
190190
| Some args-> f3 args
191191
| None-> None
192192
#endif
193-
| ILTypeMetadata(_,tdef)->
193+
| ILTypeMetadata(TILObjectReprData(_,_,tdef))->
194194
match TryDecodeILAttribute g atref tdef.CustomAttrswith
195195
| Some attr-> f1 attr
196196
|_-> None

‎src/fsharp/IlxGen.fs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -716,11 +716,11 @@ let AddStorageForVal (g: TcGlobals) (v,s) eenv =
716716
| None-> eenv
717717
| Some vref->
718718
match vref.TryDerefwith
719-
|None->
719+
|VNone->
720720
//let msg = sprintf "could not dereference external value reference to something in FSharp.Core.dll during code generation, v.MangledName = '%s', v.Range = %s" v.MangledName (stringOfRange v.Range)
721721
//System.Diagnostics.Debug.Assert(false, msg)
722722
eenv
723-
|Some gv->
723+
|VSome gv->
724724
{ eenvwith valsInScope= eenv.valsInScope.Add gv s}
725725
else
726726
eenv
@@ -4058,7 +4058,7 @@ and GenDelegateExpr cenv cgbuf eenvouter expr (TObjExprMethod((TSlotSig(_,delega
40584058
try
40594059
if isILAppTy cenv.g delegateTythen
40604060
lettcref= tcrefOfAppTy cenv.g delegateTy
4061-
let_,_,tdef= tcref.ILTyconInfo
4061+
lettdef= tcref.ILTyconRawMetadata
40624062
match tdef.Methods.FindByName".ctor"with
40634063
|[ctorMDef]->
40644064
match ctorMDef.Parameterswith
@@ -6497,7 +6497,8 @@ and GenTypeDef cenv mgbuf lazyInitInfo eenv m (tycon:Tycon) =
64976497
&& cenv.g.attrib_SerializableAttribute.IsSome
64986498

64996499
match tycon.TypeReprInfowith
6500-
| TILObjectRepr(_,_,td)->
6500+
| TILObjectRepr_->
6501+
lettd= tycon.ILTyconRawMetadata
65016502
{tdwith Access= access
65026503
CustomAttrs= mkILCustomAttrs ilCustomAttrs
65036504
GenericParams= ilGenParams}, None

‎src/fsharp/InfoReader.fs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ let GetImmediateIntrinsicMethInfosOfType (optFilter,ad) g amap m typ =
6666
| None-> st.PApplyArray((fun st-> st.GetMethods()),"GetMethods", m)
6767
[for miin meths-> ProvidedMeth(amap,mi.Coerce(m),None,m)]
6868
#endif
69-
| ILTypeMetadata(_,tdef)->
69+
| ILTypeMetadata(TILObjectReprData(_,_,tdef))->
7070
letmdefs= tdef.Methods
7171
letmdefs=(match optFilterwith None-> mdefs.AsList| Some nm-> mdefs.FindByName nm)
7272
mdefs|> List.map(fun mdef-> MethInfo.CreateILMeth(amap, m, typ, mdef))
@@ -141,7 +141,7 @@ let GetImmediateIntrinsicPropInfosOfType (optFilter,ad) g amap m typ =
141141
|> Seq.map(fun pi-> ProvidedProp(amap,pi,m))
142142
|> List.ofSeq
143143
#endif
144-
| ILTypeMetadata(_,tdef)->
144+
| ILTypeMetadata(TILObjectReprData(_,_,tdef))->
145145
lettinfo= ILTypeInfo.FromType g typ
146146
letpdefs= tdef.Properties
147147
letpdefs=match optFilterwith None-> pdefs.AsList| Some nm-> pdefs.LookupByName nm
@@ -189,7 +189,7 @@ type InfoReader(g:TcGlobals, amap:Import.ImportMap) =
189189
| Tainted.Null->[]
190190
| fi->[ ProvidedField(amap,fi,m)]
191191
#endif
192-
| ILTypeMetadata(_,tdef)->
192+
| ILTypeMetadata(TILObjectReprData(_,_,tdef))->
193193
lettinfo= ILTypeInfo.FromType g typ
194194
letfdefs= tdef.Fields
195195
letfdefs=match optFilterwith None-> fdefs.AsList| Some nm-> fdefs.LookupByName nm
@@ -214,7 +214,7 @@ type InfoReader(g:TcGlobals, amap:Import.ImportMap) =
214214
| Tainted.Null->[]
215215
| ei->[ ProvidedEvent(amap,ei,m)]
216216
#endif
217-
| ILTypeMetadata(_,tdef)->
217+
| ILTypeMetadata(TILObjectReprData(_,_,tdef))->
218218
lettinfo= ILTypeInfo.FromType g typ
219219
letedefs= tdef.Events
220220
letedefs=match optFilterwith None-> edefs.AsList| Some nm-> edefs.LookupByName nm

‎src/fsharp/NameResolution.fs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ type Item =
233233

234234
letvalRefHash(vref:ValRef)=
235235
match vref.TryDerefwith
236-
|None->0
237-
|Some v-> LanguagePrimitives.PhysicalHash v
236+
|VNone->0
237+
|VSome v-> LanguagePrimitives.PhysicalHash v
238238

239239
/// Represents a record field resolution and the information if the usage is deprecated.
240240
typeFieldResolution= FieldResolutionofRecdFieldRef*bool
@@ -422,7 +422,7 @@ let private GetCSharpStyleIndexedExtensionMembersForTyconRef (amap:Import.Import
422422
try
423423
letrs=
424424
match metadataOfTycon tcrefOfStaticClass.Deref, minfowith
425-
| ILTypeMetadata(scoref,_), ILMeth(_,ILMethInfo(_,_,_,ilMethod,_),_)->
425+
| ILTypeMetadata(TILObjectReprData(scoref,_,_)), ILMeth(_,ILMethInfo(_,_,_,ilMethod,_),_)->
426426
match ilMethod.ParameterTypeswith
427427
| firstTy::_->
428428
match firstTywith

‎src/fsharp/NicePrint.fs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,8 @@ module private TastDefinitionPrinting =
17331733
Some(wordL(tagText"(#\"<Common IL Type Omitted>\" #)"))
17341734
| TMeasureableRepr ty->
17351735
Some(layoutType denv ty)
1736-
| TILObjectRepr(_,_,td)->
1736+
| TILObjectRepr_->
1737+
lettd= tycon.ILTyconRawMetadata
17371738
Some(PrintIL.layoutILTypeDef denv td)
17381739
|_-> None
17391740

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp