Merge pull request #741 from JonHanna/faster_nullable_value_access

Use GetValueOrDefault() over Value when Nullable<T> known non-null
This commit is contained in:
James Newton-King
2015-12-16 12:40:35 +13:00
17 changed files with 59 additions and 59 deletions
+1 -1
View File
@@ -198,7 +198,7 @@ namespace Newtonsoft.Json.Bson
{ {
if (calculatedlengthPrefix != null) if (calculatedlengthPrefix != null)
{ {
_writer.Write(calculatedlengthPrefix.Value); _writer.Write(calculatedlengthPrefix.GetValueOrDefault());
} }
WriteUtf8Bytes(s, byteCount); WriteUtf8Bytes(s, byteCount);
+2 -2
View File
@@ -118,7 +118,7 @@ namespace Newtonsoft.Json
} }
if (currentPosition != null) if (currentPosition != null)
{ {
capacity += currentPosition.Value.CalculateLength(); capacity += currentPosition.GetValueOrDefault().CalculateLength();
} }
StringBuilder sb = new StringBuilder(capacity); StringBuilder sb = new StringBuilder(capacity);
@@ -131,7 +131,7 @@ namespace Newtonsoft.Json
} }
if (currentPosition != null) if (currentPosition != null)
{ {
currentPosition.Value.WriteTo(sb); currentPosition.GetValueOrDefault().WriteTo(sb);
} }
return sb.ToString(); return sb.ToString();
+16 -16
View File
@@ -847,7 +847,7 @@ namespace Newtonsoft.Json
if (_dateTimeZoneHandling != null && reader.DateTimeZoneHandling != _dateTimeZoneHandling) if (_dateTimeZoneHandling != null && reader.DateTimeZoneHandling != _dateTimeZoneHandling)
{ {
previousDateTimeZoneHandling = reader.DateTimeZoneHandling; previousDateTimeZoneHandling = reader.DateTimeZoneHandling;
reader.DateTimeZoneHandling = _dateTimeZoneHandling.Value; reader.DateTimeZoneHandling = _dateTimeZoneHandling.GetValueOrDefault();
} }
else else
{ {
@@ -857,7 +857,7 @@ namespace Newtonsoft.Json
if (_dateParseHandling != null && reader.DateParseHandling != _dateParseHandling) if (_dateParseHandling != null && reader.DateParseHandling != _dateParseHandling)
{ {
previousDateParseHandling = reader.DateParseHandling; previousDateParseHandling = reader.DateParseHandling;
reader.DateParseHandling = _dateParseHandling.Value; reader.DateParseHandling = _dateParseHandling.GetValueOrDefault();
} }
else else
{ {
@@ -867,7 +867,7 @@ namespace Newtonsoft.Json
if (_floatParseHandling != null && reader.FloatParseHandling != _floatParseHandling) if (_floatParseHandling != null && reader.FloatParseHandling != _floatParseHandling)
{ {
previousFloatParseHandling = reader.FloatParseHandling; previousFloatParseHandling = reader.FloatParseHandling;
reader.FloatParseHandling = _floatParseHandling.Value; reader.FloatParseHandling = _floatParseHandling.GetValueOrDefault();
} }
else else
{ {
@@ -914,15 +914,15 @@ namespace Newtonsoft.Json
} }
if (previousDateTimeZoneHandling != null) if (previousDateTimeZoneHandling != null)
{ {
reader.DateTimeZoneHandling = previousDateTimeZoneHandling.Value; reader.DateTimeZoneHandling = previousDateTimeZoneHandling.GetValueOrDefault();
} }
if (previousDateParseHandling != null) if (previousDateParseHandling != null)
{ {
reader.DateParseHandling = previousDateParseHandling.Value; reader.DateParseHandling = previousDateParseHandling.GetValueOrDefault();
} }
if (previousFloatParseHandling != null) if (previousFloatParseHandling != null)
{ {
reader.FloatParseHandling = previousFloatParseHandling.Value; reader.FloatParseHandling = previousFloatParseHandling.GetValueOrDefault();
} }
if (_maxDepthSet) if (_maxDepthSet)
{ {
@@ -1003,35 +1003,35 @@ namespace Newtonsoft.Json
if (_formatting != null && jsonWriter.Formatting != _formatting) if (_formatting != null && jsonWriter.Formatting != _formatting)
{ {
previousFormatting = jsonWriter.Formatting; previousFormatting = jsonWriter.Formatting;
jsonWriter.Formatting = _formatting.Value; jsonWriter.Formatting = _formatting.GetValueOrDefault();
} }
DateFormatHandling? previousDateFormatHandling = null; DateFormatHandling? previousDateFormatHandling = null;
if (_dateFormatHandling != null && jsonWriter.DateFormatHandling != _dateFormatHandling) if (_dateFormatHandling != null && jsonWriter.DateFormatHandling != _dateFormatHandling)
{ {
previousDateFormatHandling = jsonWriter.DateFormatHandling; previousDateFormatHandling = jsonWriter.DateFormatHandling;
jsonWriter.DateFormatHandling = _dateFormatHandling.Value; jsonWriter.DateFormatHandling = _dateFormatHandling.GetValueOrDefault();
} }
DateTimeZoneHandling? previousDateTimeZoneHandling = null; DateTimeZoneHandling? previousDateTimeZoneHandling = null;
if (_dateTimeZoneHandling != null && jsonWriter.DateTimeZoneHandling != _dateTimeZoneHandling) if (_dateTimeZoneHandling != null && jsonWriter.DateTimeZoneHandling != _dateTimeZoneHandling)
{ {
previousDateTimeZoneHandling = jsonWriter.DateTimeZoneHandling; previousDateTimeZoneHandling = jsonWriter.DateTimeZoneHandling;
jsonWriter.DateTimeZoneHandling = _dateTimeZoneHandling.Value; jsonWriter.DateTimeZoneHandling = _dateTimeZoneHandling.GetValueOrDefault();
} }
FloatFormatHandling? previousFloatFormatHandling = null; FloatFormatHandling? previousFloatFormatHandling = null;
if (_floatFormatHandling != null && jsonWriter.FloatFormatHandling != _floatFormatHandling) if (_floatFormatHandling != null && jsonWriter.FloatFormatHandling != _floatFormatHandling)
{ {
previousFloatFormatHandling = jsonWriter.FloatFormatHandling; previousFloatFormatHandling = jsonWriter.FloatFormatHandling;
jsonWriter.FloatFormatHandling = _floatFormatHandling.Value; jsonWriter.FloatFormatHandling = _floatFormatHandling.GetValueOrDefault();
} }
StringEscapeHandling? previousStringEscapeHandling = null; StringEscapeHandling? previousStringEscapeHandling = null;
if (_stringEscapeHandling != null && jsonWriter.StringEscapeHandling != _stringEscapeHandling) if (_stringEscapeHandling != null && jsonWriter.StringEscapeHandling != _stringEscapeHandling)
{ {
previousStringEscapeHandling = jsonWriter.StringEscapeHandling; previousStringEscapeHandling = jsonWriter.StringEscapeHandling;
jsonWriter.StringEscapeHandling = _stringEscapeHandling.Value; jsonWriter.StringEscapeHandling = _stringEscapeHandling.GetValueOrDefault();
} }
CultureInfo previousCulture = null; CultureInfo previousCulture = null;
@@ -1063,23 +1063,23 @@ namespace Newtonsoft.Json
// reset writer back to previous options // reset writer back to previous options
if (previousFormatting != null) if (previousFormatting != null)
{ {
jsonWriter.Formatting = previousFormatting.Value; jsonWriter.Formatting = previousFormatting.GetValueOrDefault();
} }
if (previousDateFormatHandling != null) if (previousDateFormatHandling != null)
{ {
jsonWriter.DateFormatHandling = previousDateFormatHandling.Value; jsonWriter.DateFormatHandling = previousDateFormatHandling.GetValueOrDefault();
} }
if (previousDateTimeZoneHandling != null) if (previousDateTimeZoneHandling != null)
{ {
jsonWriter.DateTimeZoneHandling = previousDateTimeZoneHandling.Value; jsonWriter.DateTimeZoneHandling = previousDateTimeZoneHandling.GetValueOrDefault();
} }
if (previousFloatFormatHandling != null) if (previousFloatFormatHandling != null)
{ {
jsonWriter.FloatFormatHandling = previousFloatFormatHandling.Value; jsonWriter.FloatFormatHandling = previousFloatFormatHandling.GetValueOrDefault();
} }
if (previousStringEscapeHandling != null) if (previousStringEscapeHandling != null)
{ {
jsonWriter.StringEscapeHandling = previousStringEscapeHandling.Value; jsonWriter.StringEscapeHandling = previousStringEscapeHandling.GetValueOrDefault();
} }
if (_dateFormatStringSet) if (_dateFormatStringSet)
{ {
+2 -2
View File
@@ -490,7 +490,7 @@ namespace Newtonsoft.Json
else else
{ {
InternalWriteValue(JsonToken.Float); InternalWriteValue(JsonToken.Float);
WriteValueInternal(JsonConvert.ToString(value.Value, FloatFormatHandling, QuoteChar, true), JsonToken.Float); WriteValueInternal(JsonConvert.ToString(value.GetValueOrDefault(), FloatFormatHandling, QuoteChar, true), JsonToken.Float);
} }
} }
@@ -517,7 +517,7 @@ namespace Newtonsoft.Json
else else
{ {
InternalWriteValue(JsonToken.Float); InternalWriteValue(JsonToken.Float);
WriteValueInternal(JsonConvert.ToString(value.Value, FloatFormatHandling, QuoteChar, true), JsonToken.Float); WriteValueInternal(JsonConvert.ToString(value.GetValueOrDefault(), FloatFormatHandling, QuoteChar, true), JsonToken.Float);
} }
} }
+3 -3
View File
@@ -361,7 +361,7 @@ namespace Newtonsoft.Json
JsonSchemaType? currentNodeType = GetCurrentNodeSchemaType(); JsonSchemaType? currentNodeType = GetCurrentNodeSchemaType();
if (currentNodeType != null) if (currentNodeType != null)
{ {
if (JsonSchemaGenerator.HasFlag(schema.Disallow, currentNodeType.Value)) if (JsonSchemaGenerator.HasFlag(schema.Disallow, currentNodeType.GetValueOrDefault()))
{ {
RaiseError("Type {0} is disallowed.".FormatWith(CultureInfo.InvariantCulture, currentNodeType), schema); RaiseError("Type {0} is disallowed.".FormatWith(CultureInfo.InvariantCulture, currentNodeType), schema);
} }
@@ -835,7 +835,7 @@ namespace Newtonsoft.Json
else else
#endif #endif
{ {
notDivisible = !IsZero(Convert.ToInt64(value, CultureInfo.InvariantCulture) % schema.DivisibleBy.Value); notDivisible = !IsZero(Convert.ToInt64(value, CultureInfo.InvariantCulture) % schema.DivisibleBy.GetValueOrDefault());
} }
if (notDivisible) if (notDivisible)
@@ -907,7 +907,7 @@ namespace Newtonsoft.Json
if (schema.DivisibleBy != null) if (schema.DivisibleBy != null)
{ {
double remainder = FloatingPointRemainder(value, schema.DivisibleBy.Value); double remainder = FloatingPointRemainder(value, schema.DivisibleBy.GetValueOrDefault());
if (!IsZero(remainder)) if (!IsZero(remainder))
{ {
+17 -17
View File
@@ -1091,7 +1091,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1108,7 +1108,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1124,7 +1124,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1141,7 +1141,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1157,7 +1157,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1173,7 +1173,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1189,7 +1189,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1205,7 +1205,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1222,7 +1222,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1238,7 +1238,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1254,7 +1254,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1271,7 +1271,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1287,7 +1287,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1303,7 +1303,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1320,7 +1320,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
#endif #endif
@@ -1337,7 +1337,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
@@ -1353,7 +1353,7 @@ namespace Newtonsoft.Json
} }
else else
{ {
WriteValue(value.Value); WriteValue(value.GetValueOrDefault());
} }
} }
+1 -1
View File
@@ -229,7 +229,7 @@ namespace Newtonsoft.Json.Linq
JsonToken? endToken = GetEndToken(c); JsonToken? endToken = GetEndToken(c);
if (endToken != null) if (endToken != null)
{ {
SetToken(endToken.Value); SetToken(endToken.GetValueOrDefault());
_current = c; _current = c;
_parent = c; _parent = c;
return true; return true;
+2 -2
View File
@@ -683,12 +683,12 @@ namespace Newtonsoft.Json.Linq
return JTokenType.String; return JTokenType.String;
} }
switch (current.Value) switch (current.GetValueOrDefault())
{ {
case JTokenType.Comment: case JTokenType.Comment:
case JTokenType.String: case JTokenType.String:
case JTokenType.Raw: case JTokenType.Raw:
return current.Value; return current.GetValueOrDefault();
default: default:
return JTokenType.String; return JTokenType.String;
} }
@@ -14,7 +14,7 @@ namespace Newtonsoft.Json.Linq.JsonPath
{ {
if (Index != null) if (Index != null)
{ {
JToken v = GetTokenIndex(t, errorWhenNoMatch, Index.Value); JToken v = GetTokenIndex(t, errorWhenNoMatch, Index.GetValueOrDefault());
if (v != null) if (v != null)
{ {
@@ -60,8 +60,8 @@ namespace Newtonsoft.Json.Linq.JsonPath
if (errorWhenNoMatch) if (errorWhenNoMatch)
{ {
throw new JsonException("Array slice of {0} to {1} returned no results.".FormatWith(CultureInfo.InvariantCulture, throw new JsonException("Array slice of {0} to {1} returned no results.".FormatWith(CultureInfo.InvariantCulture,
Start != null ? Start.Value.ToString(CultureInfo.InvariantCulture) : "*", Start != null ? Start.GetValueOrDefault().ToString(CultureInfo.InvariantCulture) : "*",
End != null ? End.Value.ToString(CultureInfo.InvariantCulture) : "*")); End != null ? End.GetValueOrDefault().ToString(CultureInfo.InvariantCulture) : "*"));
} }
} }
} }
@@ -84,7 +84,7 @@ namespace Newtonsoft.Json.Schema
WritePropertyIfNotNull(_writer, JsonSchemaConstants.TransientPropertyName, schema.Transient); WritePropertyIfNotNull(_writer, JsonSchemaConstants.TransientPropertyName, schema.Transient);
if (schema.Type != null) if (schema.Type != null)
{ {
WriteType(JsonSchemaConstants.TypePropertyName, _writer, schema.Type.Value); WriteType(JsonSchemaConstants.TypePropertyName, _writer, schema.Type.GetValueOrDefault());
} }
if (!schema.AllowAdditionalProperties) if (!schema.AllowAdditionalProperties)
{ {
@@ -143,7 +143,7 @@ namespace Newtonsoft.Json.Schema
} }
if (schema.Disallow != null) if (schema.Disallow != null)
{ {
WriteType(JsonSchemaConstants.DisallowPropertyName, _writer, schema.Disallow.Value); WriteType(JsonSchemaConstants.DisallowPropertyName, _writer, schema.Disallow.GetValueOrDefault());
} }
if (schema.Extends != null && schema.Extends.Count > 0) if (schema.Extends != null && schema.Extends.Count > 0)
{ {
@@ -192,7 +192,7 @@ namespace Newtonsoft.Json.Serialization
} }
} }
return _hasRequiredOrDefaultValueProperties.Value; return _hasRequiredOrDefaultValueProperties.GetValueOrDefault();
} }
} }
@@ -2096,7 +2096,7 @@ namespace Newtonsoft.Json.Serialization
contract, contract,
reader.Depth, reader.Depth,
context.Property, context.Property,
context.Presence.Value, context.Presence.GetValueOrDefault(),
!context.Used); !context.Used);
} }
} }
@@ -262,7 +262,7 @@ namespace Newtonsoft.Json.Serialization
} }
} }
if (!isReference.Value) if (!isReference.GetValueOrDefault())
{ {
return false; return false;
} }
@@ -408,7 +408,7 @@ namespace Newtonsoft.Json.Serialization
#endif #endif
} }
return _dynamicCodeGeneration.Value; return _dynamicCodeGeneration.GetValueOrDefault();
} }
} }
@@ -437,7 +437,7 @@ namespace Newtonsoft.Json.Serialization
#endif #endif
} }
return _fullyTrusted.Value; return _fullyTrusted.GetValueOrDefault();
} }
} }
+3 -3
View File
@@ -138,7 +138,7 @@ namespace Newtonsoft.Json.Utilities
return val1; return val1;
} }
return Math.Min(val1.Value, val2.Value); return Math.Min(val1.GetValueOrDefault(), val2.GetValueOrDefault());
} }
public static int? Max(int? val1, int? val2) public static int? Max(int? val1, int? val2)
@@ -152,7 +152,7 @@ namespace Newtonsoft.Json.Utilities
return val1; return val1;
} }
return Math.Max(val1.Value, val2.Value); return Math.Max(val1.GetValueOrDefault(), val2.GetValueOrDefault());
} }
public static double? Max(double? val1, double? val2) public static double? Max(double? val1, double? val2)
@@ -166,7 +166,7 @@ namespace Newtonsoft.Json.Utilities
return val1; return val1;
} }
return Math.Max(val1.Value, val2.Value); return Math.Max(val1.GetValueOrDefault(), val2.GetValueOrDefault());
} }
public static bool ApproxEquals(double d1, double d2) public static bool ApproxEquals(double d1, double d2)
@@ -862,8 +862,8 @@ namespace Newtonsoft.Json.Utilities
if (assemblyDelimiterIndex != null) if (assemblyDelimiterIndex != null)
{ {
typeName = fullyQualifiedTypeName.Substring(0, assemblyDelimiterIndex.Value).Trim(); typeName = fullyQualifiedTypeName.Substring(0, assemblyDelimiterIndex.GetValueOrDefault()).Trim();
assemblyName = fullyQualifiedTypeName.Substring(assemblyDelimiterIndex.Value + 1, fullyQualifiedTypeName.Length - assemblyDelimiterIndex.Value - 1).Trim(); assemblyName = fullyQualifiedTypeName.Substring(assemblyDelimiterIndex.GetValueOrDefault() + 1, fullyQualifiedTypeName.Length - assemblyDelimiterIndex.GetValueOrDefault() - 1).Trim();
} }
else else
{ {