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

Commit70e27ac

Browse files
auduchinokKevinRansom
authored andcommitted
Replace subsequent ContainsKey and Item calls with TryGetValue matches (dotnet#4731)
Thanks for this.
1 parent5a24b35 commit70e27ac

File tree

14 files changed

+186
-127
lines changed

14 files changed

+186
-127
lines changed

‎src/absil/il.fs‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ type LazyOrderedMultiMap<'Key,'Data when 'Key : equality>(keyf : 'Data -> 'Key,
173173
letquickMap=
174174
lazyItems|> lazyMap(fun entries->
175175
lett=new Dictionary<_,_>(entries.Length, HashIdentity.Structural)
176-
do entries|> List.iter(fun y->letkey= keyf yin t.[key]<- y::(if t.ContainsKey(key)then t.[key]else[]))
176+
do entries|> List.iter(fun y->
177+
letkey= keyf y
178+
letv=
179+
match t.TryGetValue(key)with
180+
|true, v-> v
181+
|_->[]
182+
t.[key]<- y:: v)
177183
t)
178184

179185
memberself.Entries()= lazyItems.Force()
@@ -182,7 +188,11 @@ type LazyOrderedMultiMap<'Key,'Data when 'Key : equality>(keyf : 'Data -> 'Key,
182188

183189
memberself.Filter(f)=new LazyOrderedMultiMap<'Key,'Data>(keyf, lazyItems|> lazyMap(List.filter f))
184190

185-
memberself.Itemwith get(x)=lett= quickMap.Force()inif t.ContainsKey xthen t.[x]else[]
191+
memberself.Item
192+
with get(x)=
193+
match quickMap.Force().TryGetValue(x)with
194+
|true, v-> v
195+
|_->[]
186196

187197

188198
//---------------------------------------------------------------------
@@ -1535,10 +1545,9 @@ type ILMethodDefs(f : (unit -> ILMethodDef[])) =
15351545
for i= arr.Length-1downto0do
15361546
lety= arr.[i]
15371547
letkey= y.Name
1538-
if t.ContainsKey keythen
1539-
t.[key]<- y:: t.[key]
1540-
else
1541-
t.[key]<-[ y]
1548+
match t.TryGetValue(key)with
1549+
|true, m-> t.[key]<- y:: m
1550+
|_-> t.[key]<-[y]
15421551
t)
15431552

15441553
interface IEnumerablewith
@@ -1549,7 +1558,12 @@ type ILMethodDefs(f : (unit -> ILMethodDef[])) =
15491558

15501559
memberx.AsArray= array.Value
15511560
memberx.AsList= array.Value|> Array.toList
1552-
memberx.FindByName nm=if dict.Value.ContainsKey nmthen dict.Value.[nm]else[]
1561+
1562+
memberx.FindByName(nm)=
1563+
match dict.Value.TryGetValue(nm)with
1564+
|true, m-> m
1565+
|_->[]
1566+
15531567
memberx.FindByNameAndArity(nm,arity)= x.FindByName nm|> List.filter(fun x-> List.length x.Parameters= arity)
15541568

15551569
[<NoComparison; NoEquality>]

‎src/absil/illib.fs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -907,11 +907,10 @@ let _ = eventually { use x = null in return 1 }
907907
typeUniqueStampGenerator<'Twhen'T:equality>()=
908908
letencodeTab=new Dictionary<'T,int>(HashIdentity.Structural)
909909
let mutablenItems=0
910-
letencode str=
911-
if encodeTab.ContainsKey(str)
912-
then
913-
encodeTab.[str]
914-
else
910+
letencode str=
911+
match encodeTab.TryGetValue(str)with
912+
|true, idx-> idx
913+
|_->
915914
letidx= nItems
916915
encodeTab.[str]<- idx
917916
nItems<- nItems+1

‎src/absil/ilread.fs‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -925,8 +925,14 @@ let mkCacheGeneric lowMem _inbase _nm _sz =
925925
|null-> cache:=new Dictionary<_,_>(11(* sz:int*))
926926
|_->()
927927
!cache
928-
if cache.ContainsKey idxthen(incr count; cache.[idx])
929-
elseletres= f idxin cache.[idx]<- res; res
928+
match cache.TryGetValue(idx)with
929+
|true, v->
930+
incr count
931+
v
932+
|_->
933+
letres= f idx
934+
cache.[idx]<- res
935+
res
930936

931937
//-----------------------------------------------------------------------
932938
// Polymorphic general helpers for searching for particular rows.
@@ -2652,11 +2658,10 @@ and seekReadImplMap (ctxt: ILMetadataReader) nm midx =
26522658
andseekReadTopCode(ctxt:ILMetadataReader)pev mdv numtypars(sz:int)start seqpoints=
26532659
letlabelsOfRawOffsets=new Dictionary<_,_>(sz/2)
26542660
letilOffsetsOfLabels=new Dictionary<_,_>(sz/2)
2655-
lettryRawToLabel rawOffset=
2656-
if labelsOfRawOffsets.ContainsKey rawOffsetthen
2657-
Some(labelsOfRawOffsets.[rawOffset])
2658-
else
2659-
None
2661+
lettryRawToLabel rawOffset=
2662+
match labelsOfRawOffsets.TryGetValue(rawOffset)with
2663+
|true, v-> Some v
2664+
|_-> None
26602665

26612666
letrawToLabel rawOffset=
26622667
match tryRawToLabel rawOffsetwith
@@ -3092,11 +3097,9 @@ and seekReadMethodRVA (pectxt: PEReader) (ctxt: ILMetadataReader) (idx, nm, _int
30923097
end
30933098

30943099
letkey=(tryStart, tryFinish)
3095-
if sehMap.ContainsKey keythen
3096-
letprev= sehMap.[key]
3097-
sehMap.[key]<-(prev@[clause])
3098-
else
3099-
sehMap.[key]<-[clause])
3100+
match sehMap.TryGetValue(key)with
3101+
|true, prev-> sehMap.[key]<- prev@[clause]
3102+
|_-> sehMap.[key]<-[clause])
31003103
clauses
31013104
([], sehMap)||> Seq.fold(fun acc(KeyValue(key,bs))->[for bin bs->{Range=key; Clause=b}: ILExceptionSpec]@ acc)
31023105
seh:= sehClauses
@@ -3295,7 +3298,10 @@ let getPdbReader pdbPath fileName =
32953298
documentType= Some(pdbDocumentGetType pdbdoc),
32963299
file= url))
32973300

3298-
letdocfun url=if tab.ContainsKey urlthen tab.[url]else failwith("Document with URL"+url+" not found in list of documents in the PDB file")
3301+
letdocfun url=
3302+
match tab.TryGetValue(url)with
3303+
|true, doc-> doc
3304+
|_-> failwith("Document with URL"+ url+" not found in list of documents in the PDB file")
32993305
Some(pdbr, docfun)
33003306
with e-> dprintn("* Warning: PDB file could not be read and will be ignored:"+e.Message); None
33013307
#endif

‎src/absil/ilreflect.fs‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,15 +1317,21 @@ let emitCode cenv modB emEnv (ilG:ILGenerator) (code: ILCode) =
13171317
letemEnv=
13181318
(emEnv, code.Labels)||> Seq.fold(fun emEnv(KeyValue(label,pc))->
13191319
letlab= ilG.DefineLabelAndLog()
1320-
pc2lab.[pc]<-(if pc2lab.ContainsKey pcthen lab:: pc2lab.[pc]else[lab])
1320+
pc2lab.[pc]<-
1321+
match pc2lab.TryGetValue(pc)with
1322+
|true, labels-> lab:: labels
1323+
|_->[lab]
13211324
envSetLabel emEnv label lab)
13221325

13231326
// Build a table that contains the operations that define where exception handlers are
13241327
letpc2action= Dictionary()
13251328
letlab2pc= code.Labels
13261329
letadd lab action=
13271330
letpc= lab2pc.[lab]
1328-
pc2action.[pc]<-(if pc2action.ContainsKey pcthen pc2action.[pc]@[ action]else[ action])
1331+
pc2action.[pc]<-
1332+
match pc2action.TryGetValue(pc)with
1333+
|true, actions-> actions@[action]
1334+
|_->[action]
13291335

13301336
for ein code.Exceptionsdo
13311337
let(startTry,_endTry)= e.Range
@@ -1354,12 +1360,18 @@ let emitCode cenv modB emEnv (ilG:ILGenerator) (code: ILCode) =
13541360
letinstrs= code.Instrs
13551361

13561362
for pc=0to instrs.Lengthdo
1357-
if pc2action.ContainsKey pcthen
1358-
for actionin pc2action.[pc]do
1363+
match pc2action.TryGetValue(pc)with
1364+
|true, actions->
1365+
for actionin actionsdo
13591366
action()
1360-
if pc2lab.ContainsKey pcthen
1361-
for labin pc2lab.[pc]do
1362-
ilG.MarkLabelAndLog lab
1367+
|_->()
1368+
1369+
match pc2lab.TryGetValue(pc)with
1370+
|true, labels->
1371+
for labin labelsdo
1372+
ilG.MarkLabelAndLog(lab)
1373+
|_->()
1374+
13631375
if pc< instrs.Lengththen
13641376
match instrs.[pc]with
13651377
| I_br lwhen code.Labels.[l]= pc+1->()// compress I_br to next instruction

‎src/absil/ilwrite.fs‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,9 +1643,10 @@ module Codebuf =
16431643
// a safe approximation because code only gets smaller.
16441644
ifnot(origAvailBrFixups.ContainsKey tg)then
16451645
dprintn("branch target"+ formatCodeLabel tg+" not found in code")
1646-
letorigDest=
1647-
if origAvailBrFixups.ContainsKey tgthen origAvailBrFixups.[tg]
1648-
else666666
1646+
letorigDest=
1647+
match origAvailBrFixups.TryGetValue(tg)with
1648+
|true, fixup-> fixup
1649+
|_->666666
16491650
letorigRelOffset= origDest- origEndOfInstr
16501651
-128<= origRelOffset&& origRelOffset<=127
16511652
end
@@ -1718,18 +1719,16 @@ module Codebuf =
17181719

17191720
// Now apply the adjusted fixups in the new code
17201721
newReqdBrFixups|> List.iter(fun(newFixupLoc,endOfInstr,tg,small)->
1721-
ifnot(newAvailBrFixups.ContainsKey tg)then
1722-
failwith("target"+formatCodeLabel tg+" not found in new fixups")
1723-
try
1724-
letn= newAvailBrFixups.[tg]
1725-
letrelOffset=(n- endOfInstr)
1726-
if smallthen
1727-
if Bytes.get newCode newFixupLoc<>0x98then failwith"br fixupsanity check failed"
1728-
newCode.[newFixupLoc]<- b0 relOffset
1729-
else
1730-
checkFixup32 newCode newFixupLoc0xf00dd00fl
1731-
applyFixup32 newCode newFixupLoc relOffset
1732-
with:? KeyNotFoundException->())
1722+
match newAvailBrFixups.TryGetValue(tg)with
1723+
|true, n->
1724+
letrelOffset= n- endOfInstr
1725+
if smallthen
1726+
if Bytes.get newCode newFixupLoc<>0x98then failwith"br fixupsanity check failed"
1727+
newCode.[newFixupLoc]<- b0 relOffset
1728+
else
1729+
checkFixup32 newCode newFixupLoc0xf00dd00fl
1730+
applyFixup32 newCode newFixupLoc relOffset
1731+
|_-> failwith("target"+ formatCodeLabel tg+" not found in new fixups"))
17331732

17341733
newCode, newReqdStringFixups, newExnClauses, newSeqPoints, newScopes
17351734

@@ -2162,14 +2161,19 @@ module Codebuf =
21622161
// Build a table mapping Abstract IL pcs to positions in the generated code buffer
21632162
letpc2pos= Array.zeroCreate(instrs.Length+1)
21642163
letpc2labs= Dictionary()
2165-
for(KeyValue(lab, pc))in code.Labelsdo
2166-
if pc2labs.ContainsKey pcthen pc2labs.[pc]<- lab:: pc2labs.[pc]else pc2labs.[pc]<-[lab]
2164+
for KeyValue(lab, pc)in code.Labelsdo
2165+
match pc2labs.TryGetValue(pc)with
2166+
|true, labels->
2167+
pc2labs.[pc]<- lab:: labels
2168+
|_-> pc2labs.[pc]<-[lab]
21672169

21682170
// Emit the instructions
21692171
for pc=0to instrs.Lengthdo
2170-
if pc2labs.ContainsKey pcthen
2171-
for labin pc2labs.[pc]do
2172-
codebuf.RecordAvailBrFixup lab
2172+
match pc2labs.TryGetValue(pc)with
2173+
|true, labels->
2174+
for labin labelsdo
2175+
codebuf.RecordAvailBrFixup(lab)
2176+
|_->()
21732177
pc2pos.[pc]<- codebuf.code.Position
21742178
if pc< instrs.Lengththen
21752179
match instrs.[pc]with

‎src/fsharp/ExtensionTyping.fs‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,16 +1199,8 @@ module internal ExtensionTyping =
11991199
staticParameters|> Array.map(fun sp->
12001200
lettypeBeforeArgumentsName= typeBeforeArguments.PUntaint((fun st-> st.Name), m)
12011201
letspName= sp.PUntaint((fun sp-> sp.Name), m)
1202-
ifnot(argSpecsTable.ContainsKey spName)then
1203-
if sp.PUntaint((fun sp-> sp.IsOptional), m)then
1204-
match sp.PUntaint((fun sp-> sp.RawDefaultValue), m)with
1205-
|null-> error(Error(FSComp.SR.etStaticParameterRequiresAValue(spName, typeBeforeArgumentsName, typeBeforeArgumentsName, spName), range0))
1206-
| v-> v
1207-
else
1208-
error(Error(FSComp.SR.etProvidedTypeReferenceMissingArgument(spName), range0))
1209-
else
1210-
letarg= argSpecsTable.[spName]
1211-
1202+
match argSpecsTable.TryGetValue(spName)with
1203+
|true, arg->
12121204
/// Find the name of the representation type for the static parameter
12131205
letspReprTypeName=
12141206
sp.PUntaint((fun sp->
@@ -1232,7 +1224,16 @@ module internal ExtensionTyping =
12321224
|"System.Char"-> box(char arg)
12331225
|"System.Boolean"-> box(arg="True")
12341226
|"System.String"-> box(string arg)
1235-
| s-> error(Error(FSComp.SR.etUnknownStaticArgumentKind(s, typeLogicalName), range0)))
1227+
| s-> error(Error(FSComp.SR.etUnknownStaticArgumentKind(s, typeLogicalName), range0))
1228+
1229+
|_->
1230+
if sp.PUntaint((fun sp-> sp.IsOptional), m)then
1231+
match sp.PUntaint((fun sp-> sp.RawDefaultValue), m)with
1232+
|null-> error(Error(FSComp.SR.etStaticParameterRequiresAValue(spName, typeBeforeArgumentsName, typeBeforeArgumentsName, spName), range0))
1233+
| v-> v
1234+
else
1235+
error(Error(FSComp.SR.etProvidedTypeReferenceMissingArgument(spName), range0)))
1236+
12361237

12371238
match TryApplyProvidedType(typeBeforeArguments, None, staticArgs, range0)with
12381239
| Some(typeWithArguments, checkTypeName)->

‎src/fsharp/IlxGen.fs‎

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,10 +1053,10 @@ type PropKey = PropKey of string * ILTypes * ILThisConvention
10531053

10541054
letAddPropertyDefToHash(m:range)(ht:Dictionary<PropKey,(int* ILPropertyDef)>)(pdef:ILPropertyDef)=
10551055
letnm= PropKey(pdef.Name, pdef.Args, pdef.CallingConv)
1056-
if ht.ContainsKey nmthen
1057-
letidx,pd= ht.[nm]
1056+
match ht.TryGetValue(nm)with
1057+
|true,(idx, pd)->
10581058
ht.[nm]<-(idx, MergePropertyPair m pd pdef)
1059-
else
1059+
|_->
10601060
ht.[nm]<-(ht.Count, pdef)
10611061

10621062

@@ -1291,10 +1291,9 @@ type CodeGenBuffer(m:range,
12911291

12921292
let reclab2pc n lbl=
12931293
if n= System.Int32.MaxValuethen error(InternalError("recursive label graph",m))
1294-
if codeLabelToCodeLabel.ContainsKey lblthen
1295-
lab2pc(n+1) codeLabelToCodeLabel.[lbl]
1296-
else
1297-
codeLabelToPC.[lbl]
1294+
match codeLabelToCodeLabel.TryGetValue(lbl)with
1295+
|true, l-> lab2pc(n+1) l
1296+
|_-> codeLabelToPC.[lbl]
12981297

12991298
let mutablelastSeqPoint= None
13001299

‎src/fsharp/InfoReader.fs‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,16 @@ type PropertyCollector(g, amap, m, typ, optFilter, ad) =
102102
letprops=new Dictionary<PropInfo,PropInfo>(hashIdentity)
103103

104104
letadd pinfo=
105-
if props.ContainsKey(pinfo)then
106-
match props.[pinfo], pinfowith
107-
| FSProp(_,typ,Some vref1,_), FSProp(_,_,_,Some vref2)
108-
| FSProp(_,typ,_,Some vref2), FSProp(_,_,Some vref1,_)->
109-
letpinfo= FSProp(g,typ,Some vref1,Some vref2)
110-
props.[pinfo]<- pinfo
111-
|_->
112-
// This assert fires while editing bad code. We will give a warning later in check.fs
113-
//assert ("unexpected case"= "")
114-
()
115-
else
105+
match props.TryGetValue(pinfo), pinfowith
106+
|(true, FSProp(_, typ, Some vref1,_)), FSProp(_,_,_, Some vref2)
107+
|(true, FSProp(_, typ,_, Some vref2)), FSProp(_,_, Some vref1,_)->
108+
letpinfo= FSProp(g,typ,Some vref1,Some vref2)
109+
props.[pinfo]<- pinfo
110+
|(true,_),_->
111+
// This assert fires while editing bad code. We will give a warning later in check.fs
112+
//assert ("unexpected case"= "")
113+
()
114+
|_->
116115
props.[pinfo]<- pinfo
117116

118117
memberx.Collect(membInfo:ValMemberInfo,vref:ValRef)=

‎src/fsharp/MethodCalls.fs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,10 +1162,11 @@ module ProvidedMethodCalls =
11621162
// sub in the appropriate argument
11631163
// REVIEW: "thisArg" pointer should be first, if present
11641164
letvRaw= pe.PUntaint(id,m)
1165-
ifnot(varConv.ContainsKey vRaw)then
1166-
lettypeProviderDesignation= ExtensionTyping.DisplayNameOfTypeProvider(pe.TypeProvider, m)
1167-
error(NumberedError(FSComp.SR.etIncorrectParameterExpression(typeProviderDesignation,vRaw.Name), m))
1168-
varConv.[vRaw]
1165+
match varConv.TryGetValue(vRaw)with
1166+
|true, v-> v
1167+
|_->
1168+
lettypeProviderDesignation= ExtensionTyping.DisplayNameOfTypeProvider(pe.TypeProvider, m)
1169+
error(NumberedError(FSComp.SR.etIncorrectParameterExpression(typeProviderDesignation,vRaw.Name), m))
11691170

11701171
andexprToExpr expr=
11711172
let_,(resExpr,_)= exprToExprAndWitnessfalse expr

‎src/fsharp/PostInferenceChecks.fs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,8 +1412,10 @@ let CheckEntityDefn cenv env (tycon:Entity) =
14121412

14131413
letimmediateProps= GetImmediateIntrinsicPropInfosOfType(None,AccessibleFromSomewhere) cenv.g cenv.amap m typ
14141414

1415-
letgetHash(hash:Dictionary<string,_>)nm=
1416-
if hash.ContainsKey(nm)then hash.[nm]else[]
1415+
letgetHash(hash:Dictionary<string,_>)nm=
1416+
match hash.TryGetValue(nm)with
1417+
|true, h-> h
1418+
|_->[]
14171419

14181420
// precompute methods grouped by MethInfo.LogicalName
14191421
lethashOfImmediateMeths=

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp