Merge pull request #6602 from livarcocc/json_perf_improvements

Improve Json perf by using JObject.Load.
This commit is contained in:
Livar 2017-05-18 11:55:47 -07:00 committed by GitHub
commit 68246a46fa
2 changed files with 27 additions and 14 deletions

View file

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
@ -13,7 +14,11 @@ namespace Microsoft.DotNet.Cli.Utils
public RuntimeConfig(string runtimeConfigPath)
{
var runtimeConfigJson = OpenRuntimeConfig(runtimeConfigPath);
JObject runtimeConfigJson;
using (var streamReader = new StreamReader(new FileStream(runtimeConfigPath, FileMode.Open)))
{
runtimeConfigJson = OpenRuntimeConfig(streamReader);
}
Framework = ParseFramework(runtimeConfigJson);
@ -31,9 +36,11 @@ namespace Microsoft.DotNet.Cli.Utils
return false;
}
private JObject OpenRuntimeConfig(string runtimeConfigPath)
private JObject OpenRuntimeConfig(StreamReader streamReader)
{
return JObject.Parse(File.ReadAllText(runtimeConfigPath));
var reader = new JsonTextReader(streamReader);
return JObject.Load(reader);
}
private RuntimeConfigFramework ParseFramework(JObject runtimeConfigRoot)

View file

@ -3,10 +3,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading;
using Microsoft.DotNet.Cli.CommandLine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using LocalizableStrings = Microsoft.DotNet.Tools.Add.PackageReference.LocalizableStrings;
@ -52,7 +54,7 @@ namespace Microsoft.DotNet.Cli
{
var httpClient = new HttpClient();
string result;
Stream result;
try
{
@ -60,14 +62,18 @@ namespace Microsoft.DotNet.Cli
var response = httpClient.GetAsync($"https://api-v2v3search-0.nuget.org/query?q={match}&skip=0&take=100&prerelease=true", cancellation.Token)
.Result;
result = response.Content.ReadAsStringAsync().Result;
result = response.Content.ReadAsStreamAsync().Result;
}
catch (Exception)
{
yield break;
}
var json = JObject.Parse(result);
JObject json;
using (var reader = new JsonTextReader(new StreamReader(result)))
{
json = JObject.Load(reader);
}
foreach (var id in json["data"])
{