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

Commit8dc74f7

Browse files
AviAvniKevinRansom
authored andcommitted
Reduce memory allocations (dotnet#5965)
* reduce memory allocations* remove memory allocations
1 parent17ad03f commit8dc74f7

File tree

13 files changed

+83
-46
lines changed

13 files changed

+83
-46
lines changed

‎src/absil/il.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3975,7 +3975,7 @@ and refs_of_token s x =
39753975

39763976
andrefs_of_custom_attr s(cattr:ILAttribute)= refs_of_mspec s cattr.Method
39773977

3978-
andrefs_of_custom_attrs s(cas:ILAttributes)=List.iter(refs_of_custom_attr s) cas.AsList
3978+
andrefs_of_custom_attrs s(cas:ILAttributes)=Array.iter(refs_of_custom_attr s) cas.AsArray
39793979
andrefs_of_varargs s tyso= Option.iter(refs_of_tys s) tyso
39803980
andrefs_of_instr s x=
39813981
match xwith

‎src/absil/ilprint.fs‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ let output_seq sep f os (a:seq<_>) =
9898
output_string os sep;
9999
f os e.Current
100100

101+
letoutput_array sep f os(a:_[])=
102+
ifnot(Array.isEmpty a)then
103+
for iin0..a.Length-2do
104+
f os a.[i]
105+
output_string os sep
106+
f os(a.[a.Length-1])
107+
101108
letoutput_parens f os a= output_string os"("; f os a; output_string os")"
102109
letoutput_angled f os a= output_string os"<"; f os a; output_string os">"
103110
letoutput_bracks f os a= output_string os"["; f os a; output_string os"]"
@@ -437,12 +444,12 @@ let output_option f os = function None -> () | Some x -> f os x
437444

438445
letgoutput_alternative_ref env os(alt:IlxUnionAlternative)=
439446
output_id os alt.Name;
440-
alt.FieldDefs|>Array.toList|>output_parens(output_seq","(fun os fdef-> goutput_typ env os fdef.Type)) os
447+
alt.FieldDefs|> output_parens(output_array","(fun os fdef-> goutput_typ env os fdef.Type)) os
441448

442449
letgoutput_curef env os(IlxUnionRef(_,tref,alts,_,_))=
443450
output_string os" .classunion import";
444451
goutput_tref env os tref;
445-
output_parens(output_seq","(goutput_alternative_ref env)) os(Array.toListalts)
452+
output_parens(output_array","(goutput_alternative_ref env)) os alts
446453

447454
letgoutput_cuspec env os(IlxUnionSpec(IlxUnionRef(_,tref,_,_,_),i))=
448455
output_string os"class /* classunion */";
@@ -477,7 +484,7 @@ let goutput_custom_attr env os (attr: ILAttribute) =
477484
output_custom_attr_data os data
478485

479486
letgoutput_custom_attrs env os(attrs:ILAttributes)=
480-
List.iter(fun attr-> goutput_custom_attr env os attr; output_string os"\n") attrs.AsList
487+
Array.iter(fun attr-> goutput_custom_attr env os attr; output_string os"\n") attrs.AsArray
481488

482489
letgoutput_fdef _tref env os(fd:ILFieldDef)=
483490
output_string os" .field"
@@ -704,7 +711,7 @@ let rec goutput_instr env os inst =
704711
goutput_dlocref env os(mkILArrTy(typ,shape));
705712
output_string os".ctor";
706713
letrank= shape.Rank
707-
output_parens(output_seq","(goutput_typ env)) os(Array.toList(Array.create( rank) EcmaMscorlibILGlobals.typ_Int32))
714+
output_parens(output_array","(goutput_typ env)) os(Array.create( rank) EcmaMscorlibILGlobals.typ_Int32)
708715
| I_stelem_any(shape,dt)->
709716
if shape= ILArrayShape.SingleDimensionalthen
710717
output_string os"stelem.any"; goutput_typ env os dt
@@ -713,7 +720,9 @@ let rec goutput_instr env os inst =
713720
goutput_dlocref env os(mkILArrTy(dt,shape));
714721
output_string os"Set";
715722
letrank= shape.Rank
716-
output_parens(output_seq","(goutput_typ env)) os(Array.toList(Array.create( rank) EcmaMscorlibILGlobals.typ_Int32)@[dt])
723+
letarr= Array.create(rank+1) EcmaMscorlibILGlobals.typ_Int32
724+
arr.[rank]<- dt
725+
output_parens(output_array","(goutput_typ env)) os arr
717726
| I_ldelem_any(shape,tok)->
718727
if shape= ILArrayShape.SingleDimensionalthen
719728
output_string os"ldelem.any"; goutput_typ env os tok
@@ -724,7 +733,7 @@ let rec goutput_instr env os inst =
724733
goutput_dlocref env os(mkILArrTy(tok,shape));
725734
output_string os"Get";
726735
letrank= shape.Rank
727-
output_parens(output_seq","(goutput_typ env)) os(Array.toList(Array.create( rank) EcmaMscorlibILGlobals.typ_Int32))
736+
output_parens(output_array","(goutput_typ env)) os(Array.create( rank) EcmaMscorlibILGlobals.typ_Int32)
728737
| I_ldelema(ro,_,shape,tok)->
729738
if ro= ReadonlyAddressthen output_string os"readonly.";
730739
if shape= ILArrayShape.SingleDimensionalthen
@@ -736,7 +745,7 @@ let rec goutput_instr env os inst =
736745
goutput_dlocref env os(mkILArrTy(tok,shape));
737746
output_string os"Address";
738747
letrank= shape.Rank
739-
output_parens(output_seq","(goutput_typ env)) os(Array.toList(Array.create( rank) EcmaMscorlibILGlobals.typ_Int32))
748+
output_parens(output_array","(goutput_typ env)) os(Array.create( rank) EcmaMscorlibILGlobals.typ_Int32)
740749

741750
| I_box tok-> output_string os"box"; goutput_typ env os tok
742751
| I_unbox tok-> output_string os"unbox"; goutput_typ env os tok
@@ -890,7 +899,7 @@ let splitTypeLayout = function
890899
letgoutput_fdefs tref env os(fdefs:ILFieldDefs)=
891900
List.iter(fun f->(goutput_fdef tref env) os f; output_string os"\n") fdefs.AsList
892901
letgoutput_mdefs env os(mdefs:ILMethodDefs)=
893-
List.iter(fun f->(goutput_mdef env) os f; output_string os"\n") mdefs.AsList
902+
Array.iter(fun f->(goutput_mdef env) os f; output_string os"\n") mdefs.AsArray
894903
letgoutput_pdefs env os(pdefs:ILPropertyDefs)=
895904
List.iter(fun f->(goutput_pdef env) os f; output_string os"\n") pdefs.AsList
896905

‎src/absil/ilread.fs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,7 @@ and sigptrGetTy (ctxt: ILMetadataReader) numtypars bytes sigptr =
21012101
letdim i=
21022102
(if i< numLoBoundedthen Some(List.item i lobounds)else None),
21032103
(if i< numSizedthen Some(List.item i sizes)else None)
2104-
ILArrayShape(Array.toList(Array.init rank dim))
2104+
ILArrayShape(List.init rank dim)
21052105
mkILArrTy(ty, shape), sigptr
21062106

21072107
elif b0= et_VOIDthen ILType.Void, sigptr

‎src/absil/ilreflect.fs‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ module Zmap =
289289

290290
letequalTypes(s:Type)(t:Type)= s.Equals(t)
291291
letequalTypeLists ss tt= List.lengthsEqAndForall2 equalTypes ss tt
292+
letequalTypeArrays ss tt= Array.lengthsEqAndForall2 equalTypes ss tt
292293

293294
letgetGenericArgumentsOfType(typT:Type)=
294295
if typT.IsGenericTypethen typT.GetGenericArguments()else[||]
@@ -1423,7 +1424,7 @@ let convCustomAttr cenv emEnv (cattr: ILAttribute) =
14231424
(methInfo, data)
14241425

14251426
letemitCustomAttr cenv emEnv add cattr= add(convCustomAttr cenv emEnv cattr)
1426-
letemitCustomAttrs cenv emEnv add(cattrs:ILAttributes)=List.iter(emitCustomAttr cenv emEnv add) cattrs.AsList
1427+
letemitCustomAttrs cenv emEnv add(cattrs:ILAttributes)=Array.iter(emitCustomAttr cenv emEnv add) cattrs.AsArray
14271428

14281429
//----------------------------------------------------------------------------
14291430
// buildGenParams
@@ -1597,9 +1598,7 @@ let rec buildMethodPass3 cenv tref modB (typB:TypeBuilder) emEnv (mdef : ILMetho
15971598
(getGenericArgumentsOfType(typB.AsType()))
15981599
(getGenericArgumentsOfMethod methB))
15991600

1600-
match mdef.Return.CustomAttrs.AsListwith
1601-
|[]->()
1602-
|_->
1601+
ifnot(Array.isEmpty mdef.Return.CustomAttrs.AsArray)then
16031602
letretB= methB.DefineParameterAndLog(0, System.Reflection.ParameterAttributes.Retval,null)
16041603
emitCustomAttrs cenv emEnv(wrapCustomAttr retB.SetCustomAttribute) mdef.Return.CustomAttrs
16051604

@@ -1825,7 +1824,7 @@ let rec buildTypeDefPass2 cenv nesting emEnv (tdef : ILTypeDef) =
18251824
// add interface impls
18261825
tdef.Implements|> convTypes cenv emEnv|> List.iter(fun implT-> typB.AddInterfaceImplementationAndLog(implT));
18271826
// add methods, properties
1828-
letemEnv=List.fold(buildMethodPass2 cenv tref typB) emEnv tdef.Methods.AsList
1827+
letemEnv=Array.fold(buildMethodPass2 cenv tref typB) emEnv tdef.Methods.AsArray
18291828
letemEnv= List.fold(buildFieldPass2 cenv tref typB) emEnv tdef.Fields.AsList
18301829
letemEnv= List.fold(buildPropertyPass2 cenv tref typB) emEnv tdef.Properties.AsList
18311830
letemEnv= envPopTyvars emEnv
@@ -1942,7 +1941,7 @@ let createTypeRef (visited : Dictionary<_, _>, created : Dictionary<_, _>) emEnv
19421941
traverseType CollectTypes.All cx
19431942

19441943
if verbose2then dprintf"buildTypeDefPass4: Doing method constraints of%s\n" tdef.Name
1945-
for mdin tdef.Methods.AsListdo
1944+
for mdin tdef.Methods.AsArraydo
19461945
for gpin md.GenericParamsdo
19471946
for cxin gp.Constraintsdo
19481947
traverseType CollectTypes.All cx

‎src/absil/ilwrite.fs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,7 @@ and GenCustomAttrPass3Or4 cenv hca attr =
14011401
AddUnsharedRow cenv TableNames.CustomAttribute(GetCustomAttrRow cenv hca attr)|> ignore
14021402

14031403
andGenCustomAttrsPass3Or4 cenv hca(attrs:ILAttributes)=
1404-
attrs.AsList|>List.iter(GenCustomAttrPass3Or4 cenv hca)
1404+
attrs.AsArray|>Array.iter(GenCustomAttrPass3Or4 cenv hca)
14051405

14061406
// --------------------------------------------------------------------
14071407
// ILSecurityDecl --> DeclSecurity rows
@@ -2463,7 +2463,7 @@ let GenReturnAsParamRow (returnv : ILReturn) =
24632463
StringE0|]
24642464

24652465
letGenReturnPass3 cenv(returnv:ILReturn)=
2466-
if Option.isSome returnv.Marshal||not(isNil returnv.CustomAttrs.AsList)then
2466+
if Option.isSome returnv.Marshal||not(Array.isEmpty returnv.CustomAttrs.AsArray)then
24672467
letpidx= AddUnsharedRow cenv TableNames.Param(GenReturnAsParamRow returnv)
24682468
GenCustomAttrsPass3Or4 cenv(hca_ParamDef, pidx) returnv.CustomAttrs
24692469
match returnv.Marshalwith

‎src/fsharp/AttributeChecking.fs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -449,8 +449,8 @@ let MethInfoIsUnseen g m ty minfo =
449449
// We are only interested in filtering out the method on System.Object, so it is sufficient
450450
// just to look at the attributes on IL methods.
451451
if tcref.IsILTyconthen
452-
tcref.ILTyconRawMetadata.CustomAttrs.AsList
453-
|>List.exists(fun attr-> attr.Method.DeclaringType.TypeSpec.Name= typeof<TypeProviderEditorHideMethodsAttribute>.FullName)
452+
tcref.ILTyconRawMetadata.CustomAttrs.AsArray
453+
|>Array.exists(fun attr-> attr.Method.DeclaringType.TypeSpec.Name= typeof<TypeProviderEditorHideMethodsAttribute>.FullName)
454454
else
455455
false
456456
#else

‎src/fsharp/FindUnsolved.fs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ let accTycon cenv env (tycon:Tycon) =
199199
abstractSlotValsOfTycons[tycon]|> List.iter(accVal cenv env)
200200
tycon.AllFieldsArray|> Array.iter(accTyconRecdField cenv env tycon)
201201
if tycon.IsUnionTyconthen(* This covers finite unions.*)
202-
tycon.UnionCasesAsList|>List.iter(fun uc->
202+
tycon.UnionCasesArray|>Array.iter(fun uc->
203203
accAttribs cenv env uc.Attribs
204-
uc.RecdFields|>List.iter(accTyconRecdField cenv env tycon))
204+
uc.RecdFieldsArray|>Array.iter(accTyconRecdField cenv env tycon))
205205

206206
letaccTycons cenv env tycons= List.iter(accTycon cenv env) tycons
207207

‎src/fsharp/IlxGen.fs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ and TypeDefsBuilder() =
11631163
||not tdef.Fields.AsList.IsEmpty
11641164
||not tdef.Events.AsList.IsEmpty
11651165
||not tdef.Properties.AsList.IsEmpty
1166-
||not tdef.Methods.AsList.IsEmptythen
1166+
||not(Array.isEmptytdef.Methods.AsArray)then
11671167
yield tdef]
11681168

11691169
memberb.FindTypeDefBuilder(nm)=
@@ -6527,15 +6527,15 @@ and GenTypeDef cenv mgbuf lazyInitInfo eenv m (tycon:Tycon) =
65276527
(match ilTypeDefKindwith ILTypeDefKind.ValueType->true|_->false)&&
65286528
// All structs are sequential by default
65296529
// Structs with no instance fields get size 1, pack 0
6530-
tycon.AllFieldsAsList|>List.forall(fun f-> f.IsStatic)
6530+
tycon.AllFieldsArray|>Array.forall(fun f-> f.IsStatic)
65316531

65326532
isEmptyStruct&& cenv.opts.workAroundReflectionEmitBugs&&not tycon.TyparsNoRange.IsEmpty
65336533

65346534
// Compute a bunch of useful things for each field
65356535
letisCLIMutable=(TryFindFSharpBoolAttribute cenv.g cenv.g.attrib_CLIMutableAttribute tycon.Attribs= Sometrue)
65366536
letfieldSummaries=
65376537

6538-
[for fspecin tycon.AllFieldsAsListdo
6538+
[for fspecin tycon.AllFieldsArraydo
65396539

65406540
letuseGenuineField= useGenuineField tycon fspec
65416541

@@ -6852,7 +6852,7 @@ and GenTypeDef cenv mgbuf lazyInitInfo eenv m (tycon:Tycon) =
68526852

68536853
// All structs are sequential by default
68546854
// Structs with no instance fields get size 1, pack 0
6855-
if tycon.AllFieldsAsList|>List.exists(fun f->not f.IsStatic)||
6855+
if tycon.AllFieldsArray|>Array.exists(fun f->not f.IsStatic)||
68566856
// Reflection emit doesn't let us emit 'pack' and 'size' for generic structs.
68576857
// In that case we generate a dummy field instead
68586858
(cenv.opts.workAroundReflectionEmitBugs&&not tycon.TyparsNoRange.IsEmpty)

‎src/fsharp/PostInferenceChecks.fs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,9 +2140,9 @@ let CheckEntityDefn cenv env (tycon:Entity) =
21402140
superOfTycon g tycon|> CheckTypeNoByrefs cenv env m
21412141

21422142
if tycon.IsUnionTyconthen
2143-
tycon.UnionCasesAsList|>List.iter(fun uc->
2143+
tycon.UnionCasesArray|>Array.iter(fun uc->
21442144
CheckAttribs cenv env uc.Attribs
2145-
uc.RecdFields|>List.iter(CheckRecdFieldtrue cenv env tycon))
2145+
uc.RecdFieldsArray|>Array.iter(CheckRecdFieldtrue cenv env tycon))
21462146

21472147
// Access checks
21482148
letaccess= AdjustAccess(IsHiddenTycon env.sigToImplRemapInfo tycon)(fun()-> tycon.CompilationPath) tycon.Accessibility

‎src/fsharp/TastPickle.fs‎

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ type PickledDataWithReferences<'rawData> =
3636
{/// The data that uses a collection of CcuThunks internally
3737
RawData:'rawData
3838
/// The assumptions that need to be fixed up
39-
FixupThunks:list<CcuThunk>}
39+
FixupThunks:CcuThunk[]}
4040

4141
memberx.Fixup loader=
42-
x.FixupThunks|>List.iter(fun reqd-> reqd.Fixup(loader reqd.AssemblyName))
42+
x.FixupThunks|>Array.iter(fun reqd-> reqd.Fixup(loader reqd.AssemblyName))
4343
x.RawData
4444

4545
/// Like Fixup but loader may return None, in which case there is no fixup.
4646
memberx.OptionalFixup loader=
4747
x.FixupThunks
48-
|>List.iter(fun reqd->
48+
|>Array.iter(fun reqd->
4949
match loader reqd.AssemblyNamewith
5050
| Some(loaded)-> reqd.Fixup(loaded)
5151
| None-> reqd.FixupOrphaned())
@@ -461,9 +461,21 @@ let p_array_ext extraf f (x: 'T[]) st =
461461
| Some f-> f st
462462
p_array_core f x st
463463

464-
465-
letp_list f x st= p_array f(Array.ofList x) st
466-
letp_list_ext extraf f x st= p_array_ext extraf f(Array.ofList x) st
464+
letp_list_core f(xs:'T list)st=
465+
for xin xsdo
466+
f x st
467+
468+
letp_list f x st=
469+
p_int(List.length x) st
470+
p_list_core f x st
471+
letp_list_ext extraf f x st=
472+
letn= List.length x
473+
letn=if Option.isSome extrafthen n|||0x80000000else n
474+
p_int n st
475+
match extrafwith
476+
| None->()
477+
| Some f-> f st
478+
p_list_core f x st
467479

468480
letp_List f(x:'T list)st= p_list f x st
469481

@@ -548,8 +560,22 @@ let u_array_ext extraf f st =
548560
letarr= u_array_core f(n&&&0x7FFFFFFF) st
549561
extraItem, arr
550562

551-
letu_list f st= Array.toList(u_array f st)
552-
letu_list_ext extra f st=letv,res= u_array_ext extra f stin v, Array.toList res
563+
letu_list_core f n st=
564+
[for_in1..ndo
565+
yield f st]
566+
567+
letu_list f st=
568+
letn= u_int st
569+
u_list_core f n st
570+
letu_list_ext extra f st=
571+
letn= u_int st
572+
letextraItem=
573+
if n&&&0x80000000=0x80000000then
574+
Some(extra st)
575+
else
576+
None
577+
letlist= u_list_core f(n&&&0x7FFFFFFF) st
578+
extraItem, list
553579

554580
#if FLAT_LIST_AS_LIST
555581
#else
@@ -572,7 +598,10 @@ let u_array_revi f st =
572598
res
573599

574600
// Mark up default constraints with a priority in reverse order: last gets 0 etc. See comment on TyparConstraint.DefaultsTo
575-
letu_list_revi f st= Array.toList(u_array_revi f st)
601+
letu_list_revi f st=
602+
letn= u_int st
603+
[for i=0to n-1do
604+
yield f st(n-1-i)]
576605

577606

578607
letu_wrap(f:'U->'T)(u:'U unpickler):'T unpickler=(fun st-> f(u st))
@@ -835,7 +864,7 @@ let unpickleObjWithDanglingCcus file ilscope (iILModule:ILModuleDef option) u (p
835864
#endif
836865
res
837866

838-
{RawData=data; FixupThunks=Array.toListccuTab.itbl_rows}
867+
{RawData=data; FixupThunks=ccuTab.itbl_rows}
839868

840869

841870
//=========================================================================
@@ -1733,7 +1762,7 @@ and p_tycon_repr x st =
17331762
// The leading "p_byte 1" and "p_byte 0" come from the F# 2.0 format, which used an option value at this point.
17341763
match xwith
17351764
| TRecdRepr fs-> p_byte1 st; p_byte0 st; p_rfield_table fs st;false
1736-
| TUnionRepr x-> p_byte1 st; p_byte1 st;p_list p_unioncase_spec(Array.toListx.CasesTable.CasesByIndex) st;false
1765+
| TUnionRepr x-> p_byte1 st; p_byte1 st;p_array p_unioncase_spec(x.CasesTable.CasesByIndex) st;false
17371766
| TAsmRepr ilty-> p_byte1 st; p_byte2 st; p_ILType ilty st;false
17381767
| TFSharpObjectRepr r-> p_byte1 st; p_byte3 st; p_tycon_objmodel_data r st;false
17391768
| TMeasureableRepr ty-> p_byte1 st; p_byte4 st; p_ty ty st;false
@@ -1793,7 +1822,7 @@ and p_recdfield_spec x st =
17931822
p_access x.rfield_access st
17941823

17951824
andp_rfield_table x st=
1796-
p_list p_recdfield_spec(Array.toListx.FieldsByIndex) st
1825+
p_array p_recdfield_spec(x.FieldsByIndex) st
17971826

17981827
andp_entity_spec_data(x:Entity)st=
17991828
p_tyar_specs(x.entity_typars.Force(x.entity_range)) st

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp