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.
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.
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.
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.
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.
Fixes#755
Also consolidates WriteTokenInternal into matching WriteToken (slightly reduce
assembly size).
Also extend ExceptionAssert.Throws to return exception for further examination.
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.