Remove some unnecessary list and array allocations.

This commit is contained in:
Jon Hanna
2017-01-04 22:22:26 +00:00
parent 3ae09a80f0
commit d408066f64
8 changed files with 76 additions and 55 deletions
@@ -24,6 +24,7 @@
#endregion #endregion
#if !(DOTNET || PORTABLE40 || PORTABLE) #if !(DOTNET || PORTABLE40 || PORTABLE)
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using Newtonsoft.Json.Utilities; using Newtonsoft.Json.Utilities;
@@ -181,7 +182,7 @@ namespace Newtonsoft.Json.Converters
} }
Array destinationArray = Array.CreateInstance(column.DataType.GetElementType(), o.Count); Array destinationArray = Array.CreateInstance(column.DataType.GetElementType(), o.Count);
Array.Copy(o.ToArray(), destinationArray, o.Count); ((IList)o).CopyTo(destinationArray, 0);
dr[columnName] = destinationArray; dr[columnName] = destinationArray;
} }
+7 -8
View File
@@ -710,15 +710,14 @@ namespace Newtonsoft.Json
Dictionary<string, bool> requiredProperties = _currentScope.RequiredProperties; Dictionary<string, bool> requiredProperties = _currentScope.RequiredProperties;
if (requiredProperties != null) if (requiredProperties != null && requiredProperties.Values.Any(v => !v))
{ {
List<string> unmatchedRequiredProperties = IEnumerable<string> unmatchedRequiredProperties = requiredProperties.Where(kv => !kv.Value).Select(kv => kv.Key);
requiredProperties.Where(kv => !kv.Value).Select(kv => kv.Key).ToList(); RaiseError("Required properties are missing from object: {0}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", unmatchedRequiredProperties
#if NET20 || NET35
if (unmatchedRequiredProperties.Count > 0) .ToArray()
{ #endif
RaiseError("Required properties are missing from object: {0}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", unmatchedRequiredProperties.ToArray())), schema); )), schema);
}
} }
} }
@@ -39,7 +39,11 @@ namespace Newtonsoft.Json.Linq.JsonPath
{ {
if (errorWhenNoMatch) if (errorWhenNoMatch)
{ {
throw new JsonException("Properties {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", Names.Select(n => "'" + n + "'").ToArray()), t.GetType().Name)); throw new JsonException("Properties {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, string.Join(", ", Names.Select(n => "'" + n + "'")
#if NET20 || NET35
.ToArray()
#endif
), t.GetType().Name));
} }
} }
} }
+5 -1
View File
@@ -75,7 +75,11 @@ namespace Newtonsoft.Json.Schema
public static string GetId(IEnumerable<JsonSchema> schemata) public static string GetId(IEnumerable<JsonSchema> schemata)
{ {
return string.Join("-", schemata.Select(s => s.InternalId).OrderBy(id => id, StringComparer.Ordinal).ToArray()); return string.Join("-", schemata.Select(s => s.InternalId).OrderBy(id => id, StringComparer.Ordinal)
#if NET20 || NET35
.ToArray()
#endif
);
} }
} }
} }
+23 -24
View File
@@ -216,35 +216,34 @@ namespace Newtonsoft.Json.Schema
private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type) private void WriteType(string propertyName, JsonWriter writer, JsonSchemaType type)
{ {
IList<JsonSchemaType> types; if (Enum.IsDefined(typeof(JsonSchemaType), type))
if (System.Enum.IsDefined(typeof(JsonSchemaType), type))
{ {
types = new List<JsonSchemaType> { type }; writer.WritePropertyName(propertyName);
writer.WriteValue(JsonSchemaBuilder.MapType(type));
} }
else else
{ {
types = EnumUtils.GetFlagsValues(type).Where(v => v != JsonSchemaType.None).ToList(); IEnumerator<JsonSchemaType> en = EnumUtils.GetFlagsValues(type).Where(v => v != JsonSchemaType.None).GetEnumerator();
if (en.MoveNext())
{
writer.WritePropertyName(propertyName);
JsonSchemaType first = en.Current;
if (en.MoveNext())
{
writer.WriteStartArray();
writer.WriteValue(JsonSchemaBuilder.MapType(first));
do
{
writer.WriteValue(JsonSchemaBuilder.MapType(en.Current));
} while (en.MoveNext());
writer.WriteEndArray();
}
else
{
writer.WriteValue(JsonSchemaBuilder.MapType(first));
}
}
} }
if (types.Count == 0)
{
return;
}
writer.WritePropertyName(propertyName);
if (types.Count == 1)
{
writer.WriteValue(JsonSchemaBuilder.MapType(types[0]));
return;
}
writer.WriteStartArray();
foreach (JsonSchemaType jsonSchemaType in types)
{
writer.WriteValue(JsonSchemaBuilder.MapType(jsonSchemaType));
}
writer.WriteEndArray();
} }
private void WritePropertyIfNotNull(JsonWriter writer, string propertyName, object value) private void WritePropertyIfNotNull(JsonWriter writer, string propertyName, object value)
@@ -285,8 +285,8 @@ namespace Newtonsoft.Json.Serialization
MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute); MemberSerialization memberSerialization = JsonTypeReflector.GetObjectMemberSerialization(objectType, ignoreSerializableAttribute);
List<MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static) IEnumerable<MemberInfo> allMembers = ReflectionUtils.GetFieldsAndProperties(objectType, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static)
.Where(m => !ReflectionUtils.IsIndexedProperty(m)).ToList(); .Where(m => !ReflectionUtils.IsIndexedProperty(m));
List<MemberInfo> serializableMembers = new List<MemberInfo>(); List<MemberInfo> serializableMembers = new List<MemberInfo>();
@@ -623,15 +623,17 @@ namespace Newtonsoft.Json.Serialization
private ConstructorInfo GetAttributeConstructor(Type objectType) private ConstructorInfo GetAttributeConstructor(Type objectType)
{ {
IList<ConstructorInfo> markedConstructors = objectType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(c => c.IsDefined(typeof(JsonConstructorAttribute), true)).ToList(); IEnumerator<ConstructorInfo> en = objectType.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).Where(c => c.IsDefined(typeof(JsonConstructorAttribute), true)).GetEnumerator();
if (markedConstructors.Count > 1) if (en.MoveNext())
{ {
throw new JsonException("Multiple constructors with the JsonConstructorAttribute."); ConstructorInfo conInfo = en.Current;
} if (en.MoveNext())
else if (markedConstructors.Count == 1) {
{ throw new JsonException("Multiple constructors with the JsonConstructorAttribute.");
return markedConstructors[0]; }
return conInfo;
} }
// little hack to get Version objects to deserialize correctly // little hack to get Version objects to deserialize correctly
@@ -645,16 +647,25 @@ namespace Newtonsoft.Json.Serialization
private ConstructorInfo GetParameterizedConstructor(Type objectType) private ConstructorInfo GetParameterizedConstructor(Type objectType)
{ {
IList<ConstructorInfo> constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance).ToList(); #if PORTABLE
IEnumerable<ConstructorInfo> constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
if (constructors.Count == 1) IEnumerator<ConstructorInfo> en = constructors.GetEnumerator();
if (en.MoveNext())
{
ConstructorInfo conInfo = en.Current;
if (!en.MoveNext())
{
return conInfo;
}
}
#else
ConstructorInfo[] constructors = objectType.GetConstructors(BindingFlags.Public | BindingFlags.Instance);
if (constructors.Length == 1)
{ {
return constructors[0]; return constructors[0];
} }
else #endif
{ return null;
return null;
}
} }
/// <summary> /// <summary>
@@ -1898,7 +1898,11 @@ namespace Newtonsoft.Json.Serialization
if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info) if (TraceWriter != null && TraceWriter.LevelFilter >= TraceLevel.Info)
{ {
string parameters = string.Join(", ", contract.CreatorParameters.Select(p => p.PropertyName).ToArray()); string parameters = string.Join(", ", contract.CreatorParameters.Select(p => p.PropertyName)
#if NET20 || NET35
.ToArray()
#endif
);
TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using creator with parameters: {1}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType, parameters)), null); TraceWriter.Trace(TraceLevel.Info, JsonPosition.FormatMessage(reader as IJsonLineInfo, reader.Path, "Deserializing {0} using creator with parameters: {1}.".FormatWith(CultureInfo.InvariantCulture, contract.UnderlyingType, parameters)), null);
} }
@@ -665,16 +665,15 @@ namespace Newtonsoft.Json.Utilities
foreach (IGrouping<string, MemberInfo> groupedMember in targetMembers.GroupBy(m => m.Name)) foreach (IGrouping<string, MemberInfo> groupedMember in targetMembers.GroupBy(m => m.Name))
{ {
int count = groupedMember.Count(); int count = groupedMember.Count();
IList<MemberInfo> members = groupedMember.ToList();
if (count == 1) if (count == 1)
{ {
distinctMembers.Add(members.First()); distinctMembers.Add(groupedMember.First());
} }
else else
{ {
IList<MemberInfo> resolvedMembers = new List<MemberInfo>(); IList<MemberInfo> resolvedMembers = new List<MemberInfo>();
foreach (MemberInfo memberInfo in members) foreach (MemberInfo memberInfo in groupedMember)
{ {
// this is a bit hacky // this is a bit hacky
// if the hiding property is hiding a base property and it is virtual // if the hiding property is hiding a base property and it is virtual