Skip to content

Commit 66f585b

Browse files
dtivelgoofballLogic
authored andcommitted
Improve framing performance with large sets of objects. (#32)
1 parent 508c988 commit 66f585b

File tree

3 files changed

+65
-6
lines changed

3 files changed

+65
-6
lines changed

src/json-ld.net/Core/JsonLdApi.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,14 +1072,34 @@ internal virtual void GenerateNodeMap(JToken element, JObject
10721072
/// <exception cref="JsonLD.Core.JsonLdError"></exception>
10731073
internal virtual void GenerateNodeMap(JToken element, JObject
10741074
nodeMap, string activeGraph, JToken activeSubject, string activeProperty, JObject list)
1075+
{
1076+
GenerateNodeMap(element, nodeMap, activeGraph, activeSubject, activeProperty, list, skipSetContainsCheck: false);
1077+
}
1078+
1079+
private void GenerateNodeMap(JToken element, JObject nodeMap,
1080+
string activeGraph, JToken activeSubject, string activeProperty, JObject list, bool skipSetContainsCheck)
10751081
{
10761082
// 1)
10771083
if (element is JArray)
10781084
{
1085+
JsonLdSet set = null;
1086+
1087+
if (list == null)
1088+
{
1089+
set = new JsonLdSet();
1090+
}
1091+
10791092
// 1.1)
10801093
foreach (JToken item in (JArray)element)
10811094
{
1082-
GenerateNodeMap(item, nodeMap, activeGraph, activeSubject, activeProperty, list);
1095+
skipSetContainsCheck = false;
1096+
1097+
if (set != null)
1098+
{
1099+
skipSetContainsCheck = set.Add(item);
1100+
}
1101+
1102+
GenerateNodeMap(item, nodeMap, activeGraph, activeSubject, activeProperty, list, skipSetContainsCheck);
10831103
}
10841104
return;
10851105
}
@@ -1204,7 +1224,7 @@ internal virtual void GenerateNodeMap(JToken element, JObject
12041224
if (list == null)
12051225
{
12061226
// 6.6.2.1+2)
1207-
JsonLdUtils.MergeValue(node, activeProperty, reference);
1227+
JsonLdUtils.MergeValue(node, activeProperty, reference, skipSetContainsCheck);
12081228
}
12091229
else
12101230
{

src/json-ld.net/Core/JsonLdSet.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json.Linq;
4+
5+
namespace JsonLD.Core
6+
{
7+
internal sealed class JsonLdSet
8+
{
9+
private readonly Lazy<HashSet<string>> _objects;
10+
11+
internal JsonLdSet()
12+
{
13+
_objects = new Lazy<HashSet<string>>(() => new HashSet<string>(StringComparer.Ordinal));
14+
}
15+
16+
internal bool Add(JToken token)
17+
{
18+
if (token == null)
19+
{
20+
throw new ArgumentNullException(nameof(token));
21+
}
22+
23+
if (token is JObject)
24+
{
25+
var id = token["@id"];
26+
27+
return id != null && _objects.Value.Add(id.Value<string>());
28+
}
29+
30+
return false;
31+
}
32+
}
33+
}

src/json-ld.net/Core/JsonLdUtils.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,12 @@ public static bool DeepContains(JArray values, JToken value)
148148
return false;
149149
}
150150

151-
internal static void MergeValue(JObject obj, string key, JToken
152-
value)
151+
internal static void MergeValue(JObject obj, string key, JToken value)
152+
{
153+
MergeValue(obj, key, value, skipSetContainsCheck: false);
154+
}
155+
156+
internal static void MergeValue(JObject obj, string key, JToken value, bool skipSetContainsCheck)
153157
{
154158
if (obj == null)
155159
{
@@ -161,8 +165,10 @@ internal static void MergeValue(JObject obj, string key, JToken
161165
values = new JArray();
162166
obj[key] = values;
163167
}
164-
if ("@list".Equals(key) || (value is JObject && ((IDictionary<string, JToken>
165-
)value).ContainsKey("@list")) || !DeepContains(values, (JToken)value))
168+
if (skipSetContainsCheck ||
169+
"@list".Equals(key) ||
170+
(value is JObject && ((IDictionary<string, JToken>)value).ContainsKey("@list")) ||
171+
!DeepContains(values, (JToken)value))
166172
{
167173
values.Add(value);
168174
}

0 commit comments

Comments
 (0)