Skip to content

Commit 1a73116

Browse files
committed
improve LDContext Dictionary access speed
1 parent 311603c commit 1a73116

3 files changed

Lines changed: 38 additions & 25 deletions

File tree

src/Core/Helper/Collections.fs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,32 @@ module Dictionary =
8888
)
8989
dict
9090

91+
92+
module StringDictionary =
93+
94+
open System.Collections.Generic
95+
96+
let ofSeq (s : seq<string*string>) : Dictionary<string,string> =
97+
s
98+
|> dict
99+
#if !FABLE_COMPILER
100+
|> Dictionary
101+
#else
102+
|> unbox
103+
#endif
104+
105+
let inline tryFind (key : string) (dict : Dictionary<string,'T>) =
106+
let b,v = dict.TryGetValue key
107+
if b then Some v
108+
else None
109+
110+
let inline addOrUpdate (key : string) (value : 'T) (dict : Dictionary<string,'T>) =
111+
if dict.ContainsKey key then
112+
dict.[key] <- value
113+
else
114+
dict.Add(key,value)
115+
116+
91117
module ResizeArray =
92118

93119
open System.Collections.Generic

src/Json/StringTable.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace rec ARCtrl.Json
1+
namespace rec ARCtrl.Json
22

33
open Thoth.Json.Core
44

@@ -27,7 +27,7 @@ module StringTable =
2727
Decode.array Decode.string
2828

2929
let encodeString (otm : StringTableMap) (s : string) =
30-
match Dictionary.tryFind s otm with
30+
match StringDictionary.tryFind s otm with
3131
| Some i -> Encode.int i
3232
| None ->
3333
let i = otm.Count

src/ROCrate/LDContext.fs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@ open System.Collections.Generic
44
open ARCtrl.Helper
55
open Fable.Core
66

7-
module Dictionary =
8-
9-
let ofSeq (s : seq<'Key*'T>) =
10-
let dict = Dictionary()
11-
s
12-
|> Seq.iter dict.Add
13-
dict
14-
15-
let tryFind (key : 'Key) (dict : Dictionary<'Key,'T>) =
16-
let b,v = dict.TryGetValue key
17-
if b then Some v
18-
else None
19-
207
module IRIHelper =
218

229
open ARCtrl.Helper.Regex
@@ -67,28 +54,28 @@ type LDContext(?mappings : Dictionary<string,string>, ?baseContexts : ResizeArra
6754
let compactReverseMappings : Dictionary<string,string*string> = Dictionary()
6855

6956
let addReverseMapping (key : string) (value : string) =
70-
Dictionary.addOrUpdate value key reverseMappings
57+
StringDictionary.addOrUpdate value key reverseMappings
7158
match value with
7259
| IRIHelper.CompactIri (prefix,suffix) ->
73-
Dictionary.addOrUpdate prefix (suffix,key) compactReverseMappings
74-
match Dictionary.tryFind prefix mappings with
60+
StringDictionary.addOrUpdate prefix (suffix,key) compactReverseMappings
61+
match StringDictionary.tryFind prefix mappings with
7562
| Some prefix ->
7663
let iri = IRIHelper.combine prefix suffix
77-
Dictionary.addOrUpdate iri key reverseMappings
64+
StringDictionary.addOrUpdate iri key reverseMappings
7865
| None -> ()
7966
| _ ->
80-
match Dictionary.tryFind key compactReverseMappings with
67+
match StringDictionary.tryFind key compactReverseMappings with
8168
| Some (suffix,term) ->
8269
let iri = IRIHelper.combine value suffix
83-
Dictionary.addOrUpdate iri term reverseMappings
70+
StringDictionary.addOrUpdate iri term reverseMappings
8471
| None -> ()
8572

8673
do for kvp in mappings do
8774
addReverseMapping kvp.Key kvp.Value
8875

8976
let rec tryFindTerm (term : string) : string option =
9077
let definition =
91-
match Dictionary.tryFind term mappings with
78+
match StringDictionary.tryFind term mappings with
9279
| Some v -> Some v
9380
| None ->
9481
baseContexts
@@ -103,7 +90,7 @@ type LDContext(?mappings : Dictionary<string,string>, ?baseContexts : ResizeArra
10390
| None -> None
10491

10592
let tryFindIri (iri : string) =
106-
match Dictionary.tryFind iri reverseMappings with
93+
match StringDictionary.tryFind iri reverseMappings with
10794
| Some v -> Some v
10895
| None ->
10996
baseContexts
@@ -124,7 +111,7 @@ type LDContext(?mappings : Dictionary<string,string>, ?baseContexts : ResizeArra
124111
and set(value) = name <- value
125112

126113
member this.AddMapping(term,definition) =
127-
Dictionary.addOrUpdate term definition mappings
114+
StringDictionary.addOrUpdate term definition mappings
128115
addReverseMapping term definition
129116

130117
member this.TryResolveTerm(term : string) =
@@ -151,7 +138,7 @@ type LDContext(?mappings : Dictionary<string,string>, ?baseContexts : ResizeArra
151138
| _ -> false
152139

153140
static member fromMappingSeq(mappings : seq<string*string>) =
154-
LDContext(Dictionary.ofSeq mappings)
141+
LDContext(StringDictionary.ofSeq mappings)
155142

156143
/// Append first context as base context to the second one inplace
157144
static member combine_InPlace (baseContext : LDContext) (specificContext : LDContext) : LDContext =

0 commit comments

Comments
 (0)