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,8 +1,9 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved. // 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. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System.IO; using System.IO;
namespace Microsoft.DotNet.Cli.Utils namespace Microsoft.DotNet.Cli.Utils
{ {
@ -12,12 +13,16 @@ namespace Microsoft.DotNet.Cli.Utils
public RuntimeConfigFramework Framework { get; } public RuntimeConfigFramework Framework { get; }
public RuntimeConfig(string runtimeConfigPath) public RuntimeConfig(string runtimeConfigPath)
{ {
var runtimeConfigJson = OpenRuntimeConfig(runtimeConfigPath); JObject runtimeConfigJson;
using (var streamReader = new StreamReader(new FileStream(runtimeConfigPath, FileMode.Open)))
Framework = ParseFramework(runtimeConfigJson); {
runtimeConfigJson = OpenRuntimeConfig(streamReader);
IsPortable = Framework != null; }
Framework = ParseFramework(runtimeConfigJson);
IsPortable = Framework != null;
} }
public static bool IsApplicationPortable(string entryAssemblyPath) public static bool IsApplicationPortable(string entryAssemblyPath)
@ -31,9 +36,11 @@ namespace Microsoft.DotNet.Cli.Utils
return false; 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) private RuntimeConfigFramework ParseFramework(JObject runtimeConfigRoot)

View file

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