Skip to content

Commit

Permalink
STJ: set initial capacity of JsonObject if known (#96417) (#96486)
Browse files Browse the repository at this point in the history
* STJ: set initial capacity of JsonObject if known (#96417)

* fix nullable type typo

* just tweaking to run the gh actions again to confirm

* wip/squash. use `TryGetNonEnumeratedCount`/polyfill

* wip. go back to using just a check against ICollection

* wip/squash. remove System.Linq dependency

* wip. avoid adding a new field

* wip/squash. move more of the logic into the ctor

* wip/squash. use static private helper method

* wip/squash. use null-coalescing operator

also, caught one more spot to use the IsCaseInsensitive helper

* wip/squash. use more syntax sugar!

---------

Co-authored-by: Eirik Tsarpalis <[email protected]>
  • Loading branch information
olo-ntaylor and eiriktsarpalis authored Jan 10, 2024
1 parent 813ff97 commit 1303c3a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ public bool Remove(string propertyName)

if (dictionary is null)
{
bool caseInsensitive = Options.HasValue ? Options.Value.PropertyNameCaseInsensitive : false;
dictionary = new JsonPropertyDictionary<JsonNode?>(caseInsensitive);
dictionary = new JsonPropertyDictionary<JsonNode?>(IsCaseInsensitive(Options));

if (jsonElement.HasValue)
{
foreach (JsonProperty jElementProperty in jsonElement.Value.EnumerateObject())
Expand All @@ -224,6 +224,9 @@ public bool Remove(string propertyName)
return dictionary;
}

private static bool IsCaseInsensitive(JsonNodeOptions? options) =>
options?.PropertyNameCaseInsensitive ?? false;

/// <summary>
/// Provides a coherent view of the underlying representation of the current node.
/// The jsonElement value should be consumed if and only if dictionary value is null.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,19 @@ public JsonObject(JsonNodeOptions? options = null) : base(options) { }
/// <param name="options">Options to control the behavior.</param>
public JsonObject(IEnumerable<KeyValuePair<string, JsonNode?>> properties, JsonNodeOptions? options = null) : this(options)
{
bool isCaseInsensitive = IsCaseInsensitive(options);

JsonPropertyDictionary<JsonNode?> dictionary = properties is ICollection<KeyValuePair<string, JsonNode?>> propertiesCollection
? new(isCaseInsensitive, propertiesCollection.Count)
: new(isCaseInsensitive);

foreach (KeyValuePair<string, JsonNode?> node in properties)
{
Add(node.Key, node.Value);
dictionary.Add(node.Key, node.Value);
node.Value?.AssignParent(this);
}

_dictionary = dictionary;
}

/// <summary>
Expand Down Expand Up @@ -67,7 +76,7 @@ internal JsonObject(JsonElement element, JsonNodeOptions? options = null) : this
/// <summary>
/// Gets or creates the underlying dictionary containing the properties of the object.
/// </summary>
internal JsonPropertyDictionary<JsonNode?> Dictionary => _dictionary is { } dictionary ? dictionary : InitializeDictionary();
internal JsonPropertyDictionary<JsonNode?> Dictionary => _dictionary ?? InitializeDictionary();

internal override JsonNode DeepCloneCore()
{
Expand All @@ -80,7 +89,7 @@ internal override JsonNode DeepCloneCore()
: new JsonObject(Options);
}

bool caseInsensitive = Options.HasValue ? Options.Value.PropertyNameCaseInsensitive : false;
bool caseInsensitive = IsCaseInsensitive(Options);
var jObject = new JsonObject(Options)
{
_dictionary = new JsonPropertyDictionary<JsonNode?>(caseInsensitive, dictionary.Count)
Expand Down

0 comments on commit 1303c3a

Please sign in to comment.