Skip to content

Commit 6c43575

Browse files
committed
Uniform TryGetValue usage
1 parent c4c1d9c commit 6c43575

11 files changed

Lines changed: 84 additions & 86 deletions

File tree

src/absil/illib.fs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -985,15 +985,13 @@ type MemoizationTable<'T, 'U>(compute: 'T -> 'U, keyComparer: IEqualityComparer<
985985

986986
member t.Apply x =
987987
if (match canMemoize with None -> true | Some f -> f x) then
988-
let mutable res = Unchecked.defaultof<'U>
989-
let ok = table.TryGetValue(x, &res)
990-
if ok then res
991-
else
988+
match table.TryGetValue(x) with
989+
| true, res -> res
990+
| _ ->
992991
lock table (fun () ->
993-
let mutable res = Unchecked.defaultof<'U>
994-
let ok = table.TryGetValue(x, &res)
995-
if ok then res
996-
else
992+
match table.TryGetValue(x) with
993+
| true, res -> res
994+
| _ ->
997995
let res = compute x
998996
table.[x] <- res
999997
res)
@@ -1074,11 +1072,10 @@ module Tables =
10741072
let memoize f =
10751073
let t = new Dictionary<_, _>(1000, HashIdentity.Structural)
10761074
fun x ->
1077-
let mutable res = Unchecked.defaultof<_>
1078-
if t.TryGetValue(x, &res) then
1079-
res
1080-
else
1081-
res <- f x
1075+
match t.TryGetValue(x) with
1076+
| true, res -> res
1077+
| _ ->
1078+
let res = f x
10821079
t.[x] <- res
10831080
res
10841081

src/absil/ilread.fs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -903,12 +903,11 @@ let mkCacheInt32 lowMem _inbase _nm _sz =
903903
| null -> cache := new Dictionary<int32, _>(11)
904904
| _ -> ()
905905
!cache
906-
let mutable res = Unchecked.defaultof<_>
907-
let ok = cache.TryGetValue(idx, &res)
908-
if ok then
906+
match cache.TryGetValue(idx) with
907+
| true, res ->
909908
incr count
910909
res
911-
else
910+
| _ ->
912911
let res = f idx
913912
cache.[idx] <- res
914913
res

src/absil/ilwrite.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,9 @@ type MetadataTable<'T> =
464464
#if DEBUG
465465
tbl.lookups <- tbl.lookups + 1
466466
#endif
467-
let mutable res = Unchecked.defaultof<_>
468-
let ok = tbl.dict.TryGetValue(x, &res)
469-
if ok then res
470-
else tbl.AddSharedEntry x
467+
match tbl.dict.TryGetValue(x) with
468+
| true, res -> res
469+
| _ -> tbl.AddSharedEntry x
471470

472471

473472
/// This is only used in one special place - see further below.
@@ -769,11 +768,12 @@ let rec GetTypeRefAsTypeRefRow cenv (tref: ILTypeRef) =
769768
SharedRow [| ResolutionScope (rs1, rs2); nelem; nselem |]
770769

771770
and GetTypeRefAsTypeRefIdx cenv tref =
772-
let mutable res = 0
773-
if cenv.trefCache.TryGetValue(tref, &res) then res else
774-
let res = FindOrAddSharedRow cenv TableNames.TypeRef (GetTypeRefAsTypeRefRow cenv tref)
775-
cenv.trefCache.[tref] <- res
776-
res
771+
match cenv.trefCache.TryGetValue(tref) with
772+
| true, res -> res
773+
| _ ->
774+
let res = FindOrAddSharedRow cenv TableNames.TypeRef (GetTypeRefAsTypeRefRow cenv tref)
775+
cenv.trefCache.[tref] <- res
776+
res
777777

778778
and GetTypeDescAsTypeRefIdx cenv (scoref, enc, n) =
779779
GetTypeRefAsTypeRefIdx cenv (mkILNestedTyRef (scoref, enc, n))

src/fsharp/ExtensionTyping.fs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,18 @@ module internal ExtensionTyping =
336336
match ctxt with
337337
| NoEntries -> None
338338
| Entries(d, _) ->
339-
let mutable res = Unchecked.defaultof<_>
340-
if d.TryGetValue(st, &res) then Some res else None
339+
match d.TryGetValue(st) with
340+
| true, res -> Some res
341+
| _ -> None
341342

342343
member ctxt.TryGetTyconRef st =
343344
match ctxt with
344345
| NoEntries -> None
345346
| Entries(_, d) ->
346347
let d = d.Force()
347-
let mutable res = Unchecked.defaultof<_>
348-
if d.TryGetValue(st, &res) then Some res else None
348+
match d.TryGetValue(st) with
349+
| true, res -> Some res
350+
| _ -> None
349351

350352
member ctxt.RemapTyconRefs (f: obj->obj) =
351353
match ctxt with

src/fsharp/Optimizer.fs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -573,18 +573,18 @@ let GetInfoForLocalValue cenv env (v: Val) m =
573573
// Abstract slots do not have values
574574
if v.IsDispatchSlot then UnknownValInfo
575575
else
576-
let mutable res = Unchecked.defaultof<_>
577-
let ok = cenv.localInternalVals.TryGetValue(v.Stamp, &res)
578-
if ok then res else
579-
match env.localExternalVals.TryFind v.Stamp with
580-
| Some vval -> vval
581-
| None ->
582-
if v.MustInline then
583-
errorR(Error(FSComp.SR.optValueMarkedInlineButWasNotBoundInTheOptEnv(fullDisplayTextOfValRef (mkLocalValRef v)), m))
576+
match cenv.localInternalVals.TryGetValue(v.Stamp) with
577+
| true, res -> res
578+
| _ ->
579+
match env.localExternalVals.TryFind v.Stamp with
580+
| Some vval -> vval
581+
| None ->
582+
if v.MustInline then
583+
errorR(Error(FSComp.SR.optValueMarkedInlineButWasNotBoundInTheOptEnv(fullDisplayTextOfValRef (mkLocalValRef v)), m))
584584
#if CHECKED
585-
warning(Error(FSComp.SR.optLocalValueNotFoundDuringOptimization(v.DisplayName), m))
585+
warning(Error(FSComp.SR.optLocalValueNotFoundDuringOptimization(v.DisplayName), m))
586586
#endif
587-
UnknownValInfo
587+
UnknownValInfo
588588

589589
let TryGetInfoForCcu env (ccu: CcuThunk) = env.globalModuleInfos.TryFind(ccu.AssemblyName)
590590

src/fsharp/TastPickle.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ type Table<'T> =
7171
tbl.rows.Add x
7272
n
7373
member tbl.FindOrAdd x =
74-
let mutable res = Unchecked.defaultof<_>
75-
let ok = tbl.tbl.TryGetValue(x, &res)
76-
if ok then res else tbl.Add x
74+
match tbl.tbl.TryGetValue(x) with
75+
| true, res -> res
76+
| _ -> tbl.Add x
7777

7878

7979
static member Create n =

src/fsharp/TcGlobals.fs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -870,24 +870,24 @@ type public TcGlobals(compilingFslib: bool, ilg:ILGlobals, fslibCcu: CcuThunk, d
870870
TType_app (tcref, tinst)
871871
else
872872
let dict = getDecompileTypeDict()
873-
let mutable builder = Unchecked.defaultof<_>
874-
if dict.TryGetValue(tcref.Stamp, &builder) then builder tinst
875-
else TType_app (tcref, tinst)
873+
match dict.TryGetValue(tcref.Stamp) with
874+
| true, builder -> builder tinst
875+
| _ -> TType_app (tcref, tinst)
876876

877877
/// For cosmetic purposes "improve" some .NET types, e.g. Int32 --> int32.
878878
/// Doing this normalization is a fairly performance critical piece of code as it is frequently invoked
879879
/// in the process of converting .NET metadata to F# internal compiler data structures (see import.fs).
880880
let improveTy (tcref: EntityRef) tinst =
881881
if compilingFslib then
882882
let dict = getBetterTypeDict1()
883-
let mutable builder = Unchecked.defaultof<_>
884-
if dict.TryGetValue(tcref.LogicalName, &builder) then builder tcref tinst
885-
else TType_app (tcref, tinst)
883+
match dict.TryGetValue(tcref.LogicalName) with
884+
| true, builder -> builder tcref tinst
885+
| _ -> TType_app (tcref, tinst)
886886
else
887887
let dict = getBetterTypeDict2()
888-
let mutable builder = Unchecked.defaultof<_>
889-
if dict.TryGetValue(tcref.Stamp, &builder) then builder tinst
890-
else TType_app (tcref, tinst)
888+
match dict.TryGetValue(tcref.Stamp) with
889+
| true, builder -> builder tinst
890+
| _ -> TType_app (tcref, tinst)
891891

892892

893893
override x.ToString() = "<TcGlobals>"

src/fsharp/TypeChecker.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12394,16 +12394,16 @@ module TcRecdUnionAndEnumDeclarations = begin
1239412394
let ValidateFieldNames (synFields: SynField list, tastFields: RecdField list) =
1239512395
let seen = Dictionary()
1239612396
for (sf, f) in List.zip synFields tastFields do
12397-
let mutable synField = Unchecked.defaultof<_>
12398-
if seen.TryGetValue(f.Name, &synField) then
12397+
match seen.TryGetValue(f.Name) with
12398+
| true, synField ->
1239912399
match sf, synField with
1240012400
| Field(_, _, Some id, _, _, _, _, _), Field(_, _, Some(_), _, _, _, _, _) ->
1240112401
error(Error(FSComp.SR.tcFieldNameIsUsedModeThanOnce(id.idText), id.idRange))
1240212402
| Field(_, _, Some id, _, _, _, _, _), Field(_, _, None, _, _, _, _, _)
1240312403
| Field(_, _, None, _, _, _, _, _), Field(_, _, Some id, _, _, _, _, _) ->
1240412404
error(Error(FSComp.SR.tcFieldNameConflictsWithGeneratedNameForAnonymousField(id.idText), id.idRange))
1240512405
| _ -> assert false
12406-
else
12406+
| _ ->
1240712407
seen.Add(f.Name, sf)
1240812408

1240912409
let TcUnionCaseDecl cenv env parent thisTy tpenv (UnionCase (synAttrs, id, args, xmldoc, vis, m)) =

src/fsharp/lexhelp.fs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,13 @@ type LightSyntaxStatus(initial:bool,warn:bool) =
4343
type LexResourceManager() =
4444
let strings = new System.Collections.Generic.Dictionary<string, Parser.token>(1024)
4545
member x.InternIdentifierToken(s) =
46-
let mutable res = Unchecked.defaultof<_>
47-
let ok = strings.TryGetValue(s, &res)
48-
if ok then res else
49-
let res = IDENT s
50-
(strings.[s] <- res; res)
51-
46+
match strings.TryGetValue(s) with
47+
| true, res -> res
48+
| _ ->
49+
let res = IDENT s
50+
strings.[s] <- res
51+
res
52+
5253
/// Lexer parameters
5354
type lexargs =
5455
{ defines: string list

src/ilx/EraseUnions.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,9 @@ let emitDataSwitch ilg (cg: ICodeGen<'Mark>) (avoidHelpers, cuspec, cases) =
587587
for (i,case) in cases do dict.[i] <- case
588588
let failLab = cg.GenerateDelayMark ()
589589
let emitCase i _ =
590-
let mutable res = Unchecked.defaultof<_>
591-
let ok = dict.TryGetValue(i, &res)
592-
if ok then res else cg.CodeLabel failLab
590+
match dict.TryGetValue(i) with
591+
| true, res -> res
592+
| _ -> cg.CodeLabel failLab
593593

594594
let dests = Array.mapi emitCase cuspec.AlternativesArray
595595
cg.EmitInstrs (mkGetTag ilg cuspec)

0 commit comments

Comments
 (0)