Commit Graph
46 Commits
Author SHA1 Message Date
Jon Hanna 8956d3ca9c Fix issues that are variants of those #1182 (#1184)
* Catch excessive objects in json being parsed, even if following a comment.

* Include line info on excessive content for deserialisation.
2017-01-19 10:33:30 +13:00
Jon Hanna 9727996b1a Throw for CheckAdditionalContent when extra content is preceded by comments. (#1183)
Fixes #1182
2017-01-16 14:48:52 +13:00
Jon Hanna b3e2d61708 Optimise XmlNodeConverter (#1176)
* Short-circuit SerializeGroupedNodes on no child nodes.

Avoids creating unused Dictionary.

* Don't create Dictionary in ReadAttributeElements if it'll be empty.

* Reduce calls to JsonReader.TokenType virtual property.

* Remove duplicate finished boolean.

Different reasons for finishing the ReadAttributeElements loop are
noted, but the difference isn't acted on, so merge them.

* Check correct XML is from correct TokenType immediately.

(This changes the exception message in the case of it also being an
invalid objectType, but not the exception type).
2017-01-15 18:14:00 +13:00
Jon Hanna 0ef9ec292b Don't explicitly check for Failed or Cancelled Statuses (#1179)
Between discussion at #1137 and a few rounds of revision at #1159 we
arrived at an optimisation where tasks that are both heavily hit and
very likely to return in a completed state are checked for a status of
either RanToCompletion, Canceled or Faulted and a path is taken where
no await is needed for any of those.

The Canceled and Faulted tasks are returned immediately, allowing that
state to be passed to the caller.

However, these are exceptional cases, and if we just continued the
subsequent await would raise the appropriate exception. Checking for
all three cases instead of just RanToCompletion is optimising for those
execptional conditions at cost (in the form of a larger comparison
segment of code) to the more common cases of the task either being
completed or still running.

Simplify this back to a check just for RanToCompletion.
2017-01-15 14:34:37 +13:00
Jon Hanna 3b583b886f Replace some null checks with conditionl operator, reducing field access (#1178) 2017-01-15 09:48:42 +13:00
Jon Hanna 7cb4f634e7 Optimise BinaryConverter's reflection object creation. (#1177)
Make the test for a type being S.D.L.Binary a single test as Binary is
a sealed class and therefore there's no need to test bases.

Make the stored ReflectionObject static, reducing size and repeated
allocations if BinaryConverter is explicitly created by the user.
2017-01-15 09:47:55 +13:00
Jon Hanna dcae8e220d Some optimisations to async reading. (#1171)
MoveToContentAsync is often called in states where it will immediately
return true without any reading. In this case return AsyncUtils.True
avoiding allocation or async state-machine.

DoReadAsync is a very heavily hit method, and can often be fulfilled
without creating another state machine, so do this.

The remaining looping async case is the only use of ParsePostValueAsync,
so inline it into the calling method, so there is one less task
allocation.

Return value of EatWhitespaceAsync is never used, returning void reduces
the number of locals an hence the size of the state-machine. Do the same
for EatWhitespace though the impact is less.
2017-01-13 20:41:18 +13:00
Jon Hanna 82bfde49e0 Don't eat exceptions from faulted Tasks.
If explicitly testing for IsCompleted then one must also test for
IsFaulted and if not handling the exception, return the faulted Task.
2017-01-10 11:12:46 +00:00
Jon Hanna dd8d6217a1 Pass CancellationToken in JObject and JConstructor.WriteToAsync
Missed by mistake.
2017-01-09 11:28:41 +00:00
Jon Hanna c9b2ec8055 Async JToken methods 2017-01-09 23:44:08 +13:00
Jon Hanna 7a93892d03 Optimise WriteEscapedJavaScriptString
Skip attempting write when string is empty.

FirstCharToEscape was introduced for the async version and gives a
faster means to identify the first block before any escaping, so may as
well use it here too.

Conversely, WriteEscapedStringThroughBuffer was introduced to share
code between the sync and async versions, but later optimisations meant
it was no longer used by the async. Inline the code back again.
2017-01-09 17:12:00 +13:00
Jon Hanna ff3bfaf51b Use singleton for zero-element arrays.
Create an easily-inlinable method that accesses the array used
by Enumerable.Empty (and hence not just shared within this project
but likely with the client assembly or other libraries it uses)
and use it anywhere a zero-element array is used, reducing allocations.
2017-01-09 01:50:03 +00:00
Jon Hanna ea6a21c4f3 Optimise DynamicProxyMetaObject (#1152)
* Remove unnecessary ToArray call.

* Remove Value property.

One use compares with null, so cast to T is a waste.

This leaves just one other use, so it can be inlined.

* Remove _dontFallbackFirst

Is always true. Remove it and branches for it being false.

* Don't use Expression.Convert to reference types to object.

The end-result is a no-op, so don't waste the allocation.
2017-01-09 14:44:56 +13:00
Jon Hanna f1aa441b62 Short-circuit on reference equality in ValueEquals and JValue.Compare
Covers the both-null case previously checked for, so no extra cost, and
skips all further work.

Also make two `is` checks a null-check after `as`.
2017-01-09 01:18:02 +00:00
Jon Hanna cad2ec3a09 Reduce enumeration of rightResults in BooleanQueryExpression.IsMatch (#1155)
If Operator is Exists or leftResults is empty don't obtain rightResults,
as not relevant.

Otherwise if rightResults is not an ICollection<JToken> load it into an
List so it won't be recomputed on each enumeration of leftResults.
2017-01-09 14:12:49 +13:00
Jon Hanna ec2659e3ad Merge ifs into following switch on same expression in ReadAsBytes (#1147) 2017-01-09 14:01:18 +13:00
Jon Hanna 859d12e338 Remove redundant call to Cast<>() (#1154) 2017-01-09 10:38:36 +13:00
Jon Hanna cc7f5d80eb Merge 10 branches on propertyAttribute != null into one. (#1149) 2017-01-08 15:48:32 +13:00
Jon Hanna f4c17aedb2 Coalesce nulls rather than null test where applicable. (#1150) 2017-01-08 15:36:31 +13:00
Jon Hanna 2061a8bb9a Simplify Equals override in JValue (#1148)
otherValue == null, including obj == null, both result in false and
will also result in false if continued into the Equals(JValue) method
in the first branch, so having separate branches is just extra work.
2017-01-08 15:33:03 +13:00
Jon Hanna ed99856139 Use singleons for Null, Undefined, true and false BsonTokens. (#1145)
With Null and Undefined BsonValue's, one such value is
indistinguishable from another, so singletons can be used for them,
reducing allocations.

With BsonValue containing booleans as well as there being only so
there's a similar advantage with the extra benefit that if there is
only one true value then a reference-equals check can be used instead
of unboxing to determine the value to write.
2017-01-08 09:10:10 +13:00
Jon Hanna 218c25c859 Call nullable WriteValue overloads in TraceJsonWriter (#1139)
Fixes #1138
2017-01-07 20:25:46 +13:00
Jon Hanna 260dfedac5 Don't throw on BsonWriter.WriteValue for null Uri or byte[] (#1143)
Fixes #1142
2017-01-07 18:38:38 +13:00
Jon Hanna 0b187df8cb Null-coalesce within enumerable types. (#1136)
Reduces field-access.
2017-01-07 08:13:04 +13:00
Jon Hanna f663060005 Asynchronous methods in readers and writers. (#1122)
Contributes to #650
2017-01-06 15:33:39 +13:00
Jon Hanna a95665b481 Replace is-and-cast with as-and-nullcheck
Gives slightly better performance, so applying across the board
adds up.
2017-01-05 01:52:04 +00:00
Jon Hanna 4db6579593 Optimise Extensions.Values
Branch on key being null or not once, rather than once per loop.

Remove up-cast of token to JToken to allow jitter to either elide
JValue path if impossible or turn into null-test when inevitable.

Replace is-and-cast with as-and-null-test for quicker cast in cases
where cast is still done.
2017-01-05 00:49:54 +00:00
Jon Hanna d408066f64 Remove some unnecessary list and array allocations. 2017-01-04 22:22:26 +00:00
Jon Hanna f5c64fdd09 Optimise linqbridge Select 2017-01-04 16:20:58 +00:00
Jon Hanna 896448860a Optimise LinqBridge.Enumerable.Where 2017-01-04 15:48:12 +00:00
Jon Hanna c003ad0d98 Optimise decimal predicate sum 2017-01-04 15:45:48 +00:00
Jon Hanna 32d8b4a68a Optimise LinqBridge Any 2017-01-04 15:45:06 +00:00
Jon Hanna dbc7374995 Optimise LinqBridge Count 2017-01-04 15:17:22 +00:00
Jon Hanna e08d1f7524 Optimise ToArray for list source 2017-01-04 15:01:48 +00:00
Jon Hanna 5bd4521b53 Load stack for Reverse using enumerable-taking ctor 2017-01-04 14:53:16 +00:00
Jon Hanna 1829b71255 Optimise LinqBridge.Enumerable.Cast for direct cast available.
(This changes the behaviour to return same type, but framework Linq has
the same optimisation).
2017-01-04 14:52:30 +00:00
Jon Hanna b567a066e6 Use null-propogation where applicable and useful.
Primarily where it means one fewer field-access or simplifies
event delegate invocation, though one or two cases have just the
lesser advantage of allowing an IL stack dup rather than local access.
2017-01-03 20:08:12 +00:00
Jon Hanna ecd265dad9 Don't assign _largeByteBuffer until clear it will be used.
BsonBinaryWriter._largeByteBuffer is lazily-allocated, but a path
following the allocation doesn't use it. Move the allocation into the
path that makes use of it, to avoid the null-check and potentially the
allocation.
2016-12-31 14:35:55 +00:00
Jon Hanna 5648cb50f7 Don't write duplicate nulls to trace for byte[] 2016-12-30 15:20:09 +00:00
Jon Hanna 2abc38a0ad Don't write duplicate nulls to trace for object or Uri
Fixes #1120
2016-12-30 15:06:52 +00:00
Jon Hanna bf2e5a29aa Correct ParamName in exception for invalid JsonToken on ‎WriteToken
Fixes #755

Also consolidates WriteTokenInternal into matching WriteToken (slightly reduce
assembly size).

Also extend ExceptionAssert.Throws to return exception for further examination.
2015-12-20 21:34:21 +00:00
Jon Hanna d764cfae1a Null Check on JsonWriter.WriteToken
Fixes #752
2015-12-20 14:23:01 +00:00
Jon Hanna a06716c6a6 Refactor some cases of repeated code into separate methods.
Make the MoveToContent() and ReadToContent() produced from this public.
2015-12-20 00:28:40 +00:00
Jon Hanna 40bd051809 Suppress finaliser in Dispose if there's a protected Dispose(bool)
Fixes #746
2015-12-18 19:03:36 +00:00
Jon Hanna dbbe944b19 Move reflection calls until after branches that may mean they're unused
A handful of cases where reflection calls are made prior to branches that
might lead to the results not being used.
2015-12-17 18:27:12 +00:00
Jon Hanna 7e6194b61b Use GetValueOrDefault() over Value when Nullable<T> known non-null
GetValueOrDefault() is an easily-inlined access to a field as this
will hold the correct value for both HasValue and !HasValue cases.
The Value property in comparison contains a branch on a check that the
Nullable<T> has its internal hasValue flag to decide whether to throw
InvalidOperationException or not.

Hence when a nullable type is known to have a value (not be null) and
one wants the value, rather than to possibly trigger the exception-throw
GetValueOrDefault() has a slight performance benefit over Value.

Accordingly, replace calls to the Value property with calls to
GetValueOrDefault() when previous checks or assignments guarantee the
value is not null.
2015-12-15 19:15:58 +00:00