From e07c937ab794ef9498483743b157503910ed139a Mon Sep 17 00:00:00 2001 From: petris Date: Wed, 20 Jul 2022 22:57:13 +0200 Subject: [PATCH 01/13] Optimize the SqlGuid struct Optimizes the SqlGuid struct according to the idea from #51836. --- .../src/System/Data/SQLTypes/SQLGuid.cs | 108 ++++++++++-------- 1 file changed, 59 insertions(+), 49 deletions(-) diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index 36c44d237f640..11e44d41d757b 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -2,6 +2,9 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Data.Common; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Serialization; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; @@ -16,18 +19,18 @@ namespace System.Data.SqlTypes [Serializable] [XmlSchemaProvider("GetXsdType")] [System.Runtime.CompilerServices.TypeForwardedFrom("System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")] - public struct SqlGuid : INullable, IComparable, IXmlSerializable, IEquatable + public struct SqlGuid : INullable, IComparable, ISerializable, IXmlSerializable, IEquatable { - private const int SizeOfGuid = 16; + private const int SizeOfGuid = 16; // sizeof(Guid) // NOTE: If any instance fields change, update SqlTypeWorkarounds type in System.Data.SqlClient. - private byte[]? m_value; // the SqlGuid is null if m_value is null + private Guid? _value; // the SqlGuid is null if _value is null // constructor // construct a SqlGuid.Null private SqlGuid(bool fNull) { - m_value = null; + _value = null; } public SqlGuid(byte[] value) @@ -35,26 +38,17 @@ public SqlGuid(byte[] value) if (value == null || value.Length != SizeOfGuid) throw new ArgumentException(SQLResource.InvalidArraySizeMessage); - m_value = new byte[SizeOfGuid]; - value.CopyTo(m_value, 0); - } - - internal SqlGuid(byte[] value, bool ignored) - { - if (value == null || value.Length != SizeOfGuid) - throw new ArgumentException(SQLResource.InvalidArraySizeMessage); - - m_value = value; + _value = new Guid(value); } public SqlGuid(string s) { - m_value = (new Guid(s)).ToByteArray(); + _value = new Guid(s); } public SqlGuid(Guid g) { - m_value = g.ToByteArray(); + _value = g; } public SqlGuid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k) @@ -62,22 +56,28 @@ public SqlGuid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, { } - - // INullable - public bool IsNull + private SqlGuid(SerializationInfo si, StreamingContext sc) { - get { return (m_value is null); } + ArgumentNullException.ThrowIfNull(si); + byte[]? value = (byte[]?)si.GetValue("m_value", typeof(byte[])); + if (value is null) + _value = null; + else + _value = new Guid(value); } + // INullable + public bool IsNull => _value is null; + // property: Value public Guid Value { get { - if (m_value is null) + if (_value is null) throw new SqlNullValueException(); else - return new Guid(m_value); + return _value.GetValueOrDefault(); } } @@ -95,18 +95,17 @@ public static explicit operator Guid(SqlGuid x) public byte[]? ToByteArray() { - byte[] ret = new byte[SizeOfGuid]; - m_value!.CopyTo(ret, 0); // TODO: NRE - return ret; + if (_value is null) + return null; + return _value.GetValueOrDefault().ToByteArray(); } public override string ToString() { - if (m_value is null) + if (_value is null) return SQLResource.NullString; - Guid g = new Guid(m_value); - return g.ToString(); + return _value.GetValueOrDefault().ToString(); } public static SqlGuid Parse(string s) @@ -117,31 +116,37 @@ public static SqlGuid Parse(string s) return new SqlGuid(s); } - // Comparison operators private static EComparison Compare(SqlGuid x, SqlGuid y) { // Comparison orders. ReadOnlySpan rgiGuidOrder = new byte[16] { 10, 11, 12, 13, 14, 15, 8, 9, 6, 7, 4, 5, 0, 1, 2, 3 }; + Debug.Assert(!x.IsNull); + Debug.Assert(!y.IsNull); + // Swap to the correct order to be compared - ReadOnlySpan xBytes = x.m_value; - ReadOnlySpan yBytes = y.m_value; + Span xBytes = stackalloc byte[sizeof(Guid)]; + bool xWrote = x._value.GetValueOrDefault().TryWriteSpan(xBytes); + Debug.Assert(xWrote); + + Span yBytes = stackalloc byte[sizeof(Guid)]; + bool yWrote = y._value.GetValueOrDefault().TryWriteSpan(yBytes); + Debug.Assert(yWrote); + for (int i = 0; i < SizeOfGuid; i++) { byte b1 = xBytes[rgiGuidOrder[i]]; byte b2 = yBytes[rgiGuidOrder[i]]; if (b1 != b2) { - return (b1 < b2) ? EComparison.LT : EComparison.GT; + return b1 < b2 ? EComparison.LT : EComparison.GT; } } return EComparison.EQ; } - - // Implicit conversions // Explicit conversions @@ -161,7 +166,7 @@ public static explicit operator SqlGuid(SqlBinary x) // Overloading comparison operators public static SqlBoolean operator ==(SqlGuid x, SqlGuid y) { - return (x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.EQ); + return x.IsNull || y.IsNull ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.EQ); } public static SqlBoolean operator !=(SqlGuid x, SqlGuid y) @@ -171,12 +176,12 @@ public static explicit operator SqlGuid(SqlBinary x) public static SqlBoolean operator <(SqlGuid x, SqlGuid y) { - return (x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.LT); + return x.IsNull || y.IsNull ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.LT); } public static SqlBoolean operator >(SqlGuid x, SqlGuid y) { - return (x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.GT); + return x.IsNull || y.IsNull ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.GT); } public static SqlBoolean operator <=(SqlGuid x, SqlGuid y) @@ -204,37 +209,37 @@ public static explicit operator SqlGuid(SqlBinary x) // Alternative method for operator == public static SqlBoolean Equals(SqlGuid x, SqlGuid y) { - return (x == y); + return x == y; } // Alternative method for operator != public static SqlBoolean NotEquals(SqlGuid x, SqlGuid y) { - return (x != y); + return x != y; } // Alternative method for operator < public static SqlBoolean LessThan(SqlGuid x, SqlGuid y) { - return (x < y); + return x < y; } // Alternative method for operator > public static SqlBoolean GreaterThan(SqlGuid x, SqlGuid y) { - return (x > y); + return x > y; } // Alternative method for operator <= public static SqlBoolean LessThanOrEqual(SqlGuid x, SqlGuid y) { - return (x <= y); + return x <= y; } // Alternative method for operator >= public static SqlBoolean GreaterThanOrEqual(SqlGuid x, SqlGuid y) { - return (x >= y); + return x >= y; } // Alternative method for conversions. @@ -249,7 +254,6 @@ public SqlBinary ToSqlBinary() return (SqlBinary)this; } - // IComparable // Compares this object to another object, returning an integer that // indicates the relationship. @@ -292,7 +296,7 @@ public bool Equals(SqlGuid other) => (this == other).Value; // For hashing purpose - public override int GetHashCode() => IsNull ? 0 : Value.GetHashCode(); + public override int GetHashCode() => IsNull ? 0 : _value.GetValueOrDefault().GetHashCode(); XmlSchema? IXmlSerializable.GetSchema() { return null; } @@ -303,23 +307,23 @@ void IXmlSerializable.ReadXml(XmlReader reader) { // Read the next value. reader.ReadElementString(); - m_value = null; + _value = null; } else { - m_value = new Guid(reader.ReadElementString()).ToByteArray(); + _value = new Guid(reader.ReadElementString()); } } void IXmlSerializable.WriteXml(XmlWriter writer) { - if (m_value is null) + if (_value is null) { writer.WriteAttributeString("xsi", "nil", XmlSchema.InstanceNamespace, "true"); } else { - writer.WriteString(XmlConvert.ToString(new Guid(m_value))); + writer.WriteString(XmlConvert.ToString(_value.GetValueOrDefault())); } } @@ -328,6 +332,12 @@ public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet) return new XmlQualifiedName("string", XmlSchema.Namespace); } + void ISerializable.GetObjectData(SerializationInfo si, StreamingContext sc) + { + ArgumentNullException.ThrowIfNull(si); + si.AddValue("m_value", ToByteArray()); + } + public static readonly SqlGuid Null = new SqlGuid(true); } // SqlGuid } // namespace System.Data.SqlTypes From 44ee4f5f36598a5e903134767e7e804afa8fd0ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Wed, 20 Jul 2022 23:32:16 +0200 Subject: [PATCH 02/13] Update SQLGuid.cs --- .../System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index 11e44d41d757b..42ad53a500306 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.Data.Common; +using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Runtime.Serialization; @@ -127,11 +128,11 @@ private static EComparison Compare(SqlGuid x, SqlGuid y) // Swap to the correct order to be compared Span xBytes = stackalloc byte[sizeof(Guid)]; - bool xWrote = x._value.GetValueOrDefault().TryWriteSpan(xBytes); + bool xWrote = x._value.GetValueOrDefault().TryWriteBytes(xBytes); Debug.Assert(xWrote); Span yBytes = stackalloc byte[sizeof(Guid)]; - bool yWrote = y._value.GetValueOrDefault().TryWriteSpan(yBytes); + bool yWrote = y._value.GetValueOrDefault().TryWriteBytes(yBytes); Debug.Assert(yWrote); for (int i = 0; i < SizeOfGuid; i++) From 961b4dac06f3f9a9218644c6cd0ecde267cb1940 Mon Sep 17 00:00:00 2001 From: petris Date: Thu, 21 Jul 2022 00:19:54 +0200 Subject: [PATCH 03/13] Apply suggestions --- .../src/System/Data/SQLTypes/SQLGuid.cs | 42 ++++++------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index 42ad53a500306..e8b3b3081d092 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -28,11 +28,6 @@ public struct SqlGuid : INullable, IComparable, ISerializable, IXmlSerializable, private Guid? _value; // the SqlGuid is null if _value is null // constructor - // construct a SqlGuid.Null - private SqlGuid(bool fNull) - { - _value = null; - } public SqlGuid(byte[] value) { @@ -59,7 +54,6 @@ public SqlGuid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, private SqlGuid(SerializationInfo si, StreamingContext sc) { - ArgumentNullException.ThrowIfNull(si); byte[]? value = (byte[]?)si.GetValue("m_value", typeof(byte[])); if (value is null) _value = null; @@ -71,16 +65,7 @@ private SqlGuid(SerializationInfo si, StreamingContext sc) public bool IsNull => _value is null; // property: Value - public Guid Value - { - get - { - if (_value is null) - throw new SqlNullValueException(); - else - return _value.GetValueOrDefault(); - } - } + public Guid Value => _value ?? throw new SqlNullValueException(); // Implicit conversion from Guid to SqlGuid public static implicit operator SqlGuid(Guid x) @@ -118,7 +103,7 @@ public static SqlGuid Parse(string s) } // Comparison operators - private static EComparison Compare(SqlGuid x, SqlGuid y) + private static unsafe EComparison Compare(SqlGuid x, SqlGuid y) { // Comparison orders. ReadOnlySpan rgiGuidOrder = new byte[16] { 10, 11, 12, 13, 14, 15, 8, 9, 6, 7, 4, 5, 0, 1, 2, 3 }; @@ -141,7 +126,7 @@ private static EComparison Compare(SqlGuid x, SqlGuid y) byte b2 = yBytes[rgiGuidOrder[i]]; if (b1 != b2) { - return b1 < b2 ? EComparison.LT : EComparison.GT; + return (b1 < b2) ? EComparison.LT : EComparison.GT; } } @@ -167,7 +152,7 @@ public static explicit operator SqlGuid(SqlBinary x) // Overloading comparison operators public static SqlBoolean operator ==(SqlGuid x, SqlGuid y) { - return x.IsNull || y.IsNull ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.EQ); + return (x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.EQ); } public static SqlBoolean operator !=(SqlGuid x, SqlGuid y) @@ -177,12 +162,12 @@ public static explicit operator SqlGuid(SqlBinary x) public static SqlBoolean operator <(SqlGuid x, SqlGuid y) { - return x.IsNull || y.IsNull ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.LT); + return (x.IsNull || y.) ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.LT); } public static SqlBoolean operator >(SqlGuid x, SqlGuid y) { - return x.IsNull || y.IsNull ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.GT); + return (x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.GT); } public static SqlBoolean operator <=(SqlGuid x, SqlGuid y) @@ -210,37 +195,37 @@ public static explicit operator SqlGuid(SqlBinary x) // Alternative method for operator == public static SqlBoolean Equals(SqlGuid x, SqlGuid y) { - return x == y; + return (x == y); } // Alternative method for operator != public static SqlBoolean NotEquals(SqlGuid x, SqlGuid y) { - return x != y; + return (x != y); } // Alternative method for operator < public static SqlBoolean LessThan(SqlGuid x, SqlGuid y) { - return x < y; + return (x < y); } // Alternative method for operator > public static SqlBoolean GreaterThan(SqlGuid x, SqlGuid y) { - return x > y; + return (x > y); } // Alternative method for operator <= public static SqlBoolean LessThanOrEqual(SqlGuid x, SqlGuid y) { - return x <= y; + return (x <= y); } // Alternative method for operator >= public static SqlBoolean GreaterThanOrEqual(SqlGuid x, SqlGuid y) { - return x >= y; + return (x >= y); } // Alternative method for conversions. @@ -335,10 +320,9 @@ public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet) void ISerializable.GetObjectData(SerializationInfo si, StreamingContext sc) { - ArgumentNullException.ThrowIfNull(si); si.AddValue("m_value", ToByteArray()); } - public static readonly SqlGuid Null = new SqlGuid(true); + public static readonly SqlGuid Null = default; } // SqlGuid } // namespace System.Data.SqlTypes From 0d02d195fb7409d65bf2c329b7a3f2883fff31e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Thu, 21 Jul 2022 00:23:57 +0200 Subject: [PATCH 04/13] Update SQLGuid.cs --- .../System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index e8b3b3081d092..e0e138700f312 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -162,7 +162,7 @@ public static explicit operator SqlGuid(SqlBinary x) public static SqlBoolean operator <(SqlGuid x, SqlGuid y) { - return (x.IsNull || y.) ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.LT); + return (x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.LT); } public static SqlBoolean operator >(SqlGuid x, SqlGuid y) From afb151e7f6d9d0bd2eaa3d48d579b7937ed1dc30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Thu, 21 Jul 2022 01:01:18 +0200 Subject: [PATCH 05/13] Update SQLGuid.cs --- .../System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index e0e138700f312..976cebbe8461d 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -323,6 +323,6 @@ void ISerializable.GetObjectData(SerializationInfo si, StreamingContext sc) si.AddValue("m_value", ToByteArray()); } - public static readonly SqlGuid Null = default; + public static readonly SqlGuid Null; } // SqlGuid } // namespace System.Data.SqlTypes From b5133bfd045eeaf98a98e90c976df895c04a2e26 Mon Sep 17 00:00:00 2001 From: petris Date: Thu, 21 Jul 2022 01:51:28 +0200 Subject: [PATCH 06/13] Update ref assembly --- .../System.Data.Common/ref/System.Data.Common.cs | 5 +++-- .../src/System/Data/SQLTypes/SQLGuid.cs | 10 +++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Data.Common/ref/System.Data.Common.cs b/src/libraries/System.Data.Common/ref/System.Data.Common.cs index 7f8e500b823aa..8bef9f271a765 100644 --- a/src/libraries/System.Data.Common/ref/System.Data.Common.cs +++ b/src/libraries/System.Data.Common/ref/System.Data.Common.cs @@ -3213,15 +3213,15 @@ void System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter wri public override string ToString() { throw null; } } [System.Xml.Serialization.XmlSchemaProviderAttribute("GetXsdType")] - public partial struct SqlGuid : System.Data.SqlTypes.INullable, System.IComparable, System.Xml.Serialization.IXmlSerializable, System.IEquatable + public partial struct SqlGuid : System.Data.SqlTypes.INullable, System.IComparable, System.Runtime.Serialization.ISerializable, System.Xml.Serialization.IXmlSerializable, System.IEquatable { - private object _dummy; private int _dummyPrimitive; public static readonly System.Data.SqlTypes.SqlGuid Null; public SqlGuid(byte[] value) { throw null; } public SqlGuid(System.Guid g) { throw null; } public SqlGuid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k) { throw null; } public SqlGuid(string s) { throw null; } + public SqlGuid(SerializationInfo si, StreamingContext sc) { throw null; } public bool IsNull { get { throw null; } } public System.Guid Value { get { throw null; } } public int CompareTo(System.Data.SqlTypes.SqlGuid value) { throw null; } @@ -3250,6 +3250,7 @@ public partial struct SqlGuid : System.Data.SqlTypes.INullable, System.IComparab System.Xml.Schema.XmlSchema System.Xml.Serialization.IXmlSerializable.GetSchema() { throw null; } void System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader reader) { } void System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter writer) { } + void ISerializable.GetObjectData(SerializationInfo si, StreamingContext sc) { } public byte[]? ToByteArray() { throw null; } public System.Data.SqlTypes.SqlBinary ToSqlBinary() { throw null; } public System.Data.SqlTypes.SqlString ToSqlString() { throw null; } diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index 976cebbe8461d..64b952f266384 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -52,7 +52,7 @@ public SqlGuid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, { } - private SqlGuid(SerializationInfo si, StreamingContext sc) + public SqlGuid(SerializationInfo si, StreamingContext sc) { byte[]? value = (byte[]?)si.GetValue("m_value", typeof(byte[])); if (value is null) @@ -103,20 +103,20 @@ public static SqlGuid Parse(string s) } // Comparison operators - private static unsafe EComparison Compare(SqlGuid x, SqlGuid y) + private static EComparison Compare(SqlGuid x, SqlGuid y) { // Comparison orders. - ReadOnlySpan rgiGuidOrder = new byte[16] { 10, 11, 12, 13, 14, 15, 8, 9, 6, 7, 4, 5, 0, 1, 2, 3 }; + ReadOnlySpan rgiGuidOrder = new byte[SizeOfGuid] { 10, 11, 12, 13, 14, 15, 8, 9, 6, 7, 4, 5, 0, 1, 2, 3 }; Debug.Assert(!x.IsNull); Debug.Assert(!y.IsNull); // Swap to the correct order to be compared - Span xBytes = stackalloc byte[sizeof(Guid)]; + Span xBytes = stackalloc byte[SizeOfGuid]; bool xWrote = x._value.GetValueOrDefault().TryWriteBytes(xBytes); Debug.Assert(xWrote); - Span yBytes = stackalloc byte[sizeof(Guid)]; + Span yBytes = stackalloc byte[SizeOfGuid]; bool yWrote = y._value.GetValueOrDefault().TryWriteBytes(yBytes); Debug.Assert(yWrote); From 42b30344192a2f6beecea5268899ed9110eef837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Thu, 21 Jul 2022 02:07:30 +0200 Subject: [PATCH 07/13] Update System.Data.Common.cs --- src/libraries/System.Data.Common/ref/System.Data.Common.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Data.Common/ref/System.Data.Common.cs b/src/libraries/System.Data.Common/ref/System.Data.Common.cs index 8bef9f271a765..5a711006adafe 100644 --- a/src/libraries/System.Data.Common/ref/System.Data.Common.cs +++ b/src/libraries/System.Data.Common/ref/System.Data.Common.cs @@ -3221,7 +3221,7 @@ public partial struct SqlGuid : System.Data.SqlTypes.INullable, System.IComparab public SqlGuid(System.Guid g) { throw null; } public SqlGuid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k) { throw null; } public SqlGuid(string s) { throw null; } - public SqlGuid(SerializationInfo si, StreamingContext sc) { throw null; } + public SqlGuid(System.Runtime.Serialization.SerializationInfo si, System.Runtime.Serialization.StreamingContext sc) { throw null; } public bool IsNull { get { throw null; } } public System.Guid Value { get { throw null; } } public int CompareTo(System.Data.SqlTypes.SqlGuid value) { throw null; } @@ -3250,7 +3250,7 @@ public partial struct SqlGuid : System.Data.SqlTypes.INullable, System.IComparab System.Xml.Schema.XmlSchema System.Xml.Serialization.IXmlSerializable.GetSchema() { throw null; } void System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader reader) { } void System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter writer) { } - void ISerializable.GetObjectData(SerializationInfo si, StreamingContext sc) { } + void System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo si, System.Runtime.Serialization.StreamingContext sc) { } public byte[]? ToByteArray() { throw null; } public System.Data.SqlTypes.SqlBinary ToSqlBinary() { throw null; } public System.Data.SqlTypes.SqlString ToSqlString() { throw null; } From c75c288f2c5a07db95e887232fc1dc004e97c7ec Mon Sep 17 00:00:00 2001 From: petris Date: Thu, 21 Jul 2022 03:14:56 +0200 Subject: [PATCH 08/13] Apply suggestions --- .../System.Data.Common/ref/System.Data.Common.cs | 3 +-- .../src/System/Data/SQLTypes/SQLGuid.cs | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/libraries/System.Data.Common/ref/System.Data.Common.cs b/src/libraries/System.Data.Common/ref/System.Data.Common.cs index 5a711006adafe..f3ab155795974 100644 --- a/src/libraries/System.Data.Common/ref/System.Data.Common.cs +++ b/src/libraries/System.Data.Common/ref/System.Data.Common.cs @@ -3221,7 +3221,6 @@ public partial struct SqlGuid : System.Data.SqlTypes.INullable, System.IComparab public SqlGuid(System.Guid g) { throw null; } public SqlGuid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, byte i, byte j, byte k) { throw null; } public SqlGuid(string s) { throw null; } - public SqlGuid(System.Runtime.Serialization.SerializationInfo si, System.Runtime.Serialization.StreamingContext sc) { throw null; } public bool IsNull { get { throw null; } } public System.Guid Value { get { throw null; } } public int CompareTo(System.Data.SqlTypes.SqlGuid value) { throw null; } @@ -3250,7 +3249,7 @@ public partial struct SqlGuid : System.Data.SqlTypes.INullable, System.IComparab System.Xml.Schema.XmlSchema System.Xml.Serialization.IXmlSerializable.GetSchema() { throw null; } void System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader reader) { } void System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter writer) { } - void System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo si, System.Runtime.Serialization.StreamingContext sc) { } + void System.Runtime.Serialization.ISerializable.GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) { } public byte[]? ToByteArray() { throw null; } public System.Data.SqlTypes.SqlBinary ToSqlBinary() { throw null; } public System.Data.SqlTypes.SqlString ToSqlString() { throw null; } diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index 64b952f266384..dff690f8be809 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -52,9 +52,9 @@ public SqlGuid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, { } - public SqlGuid(SerializationInfo si, StreamingContext sc) + private SqlGuid(SerializationInfo info, StreamingContext context) { - byte[]? value = (byte[]?)si.GetValue("m_value", typeof(byte[])); + byte[]? value = (byte[]?)info.GetValue("m_value", typeof(byte[])); if (value is null) _value = null; else @@ -318,9 +318,9 @@ public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet) return new XmlQualifiedName("string", XmlSchema.Namespace); } - void ISerializable.GetObjectData(SerializationInfo si, StreamingContext sc) + void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { - si.AddValue("m_value", ToByteArray()); + info.AddValue("m_value", ToByteArray()); } public static readonly SqlGuid Null; From 95cd600ad36d90e30f612c77384ead58426b609d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Thu, 21 Jul 2022 21:38:11 +0200 Subject: [PATCH 09/13] Update SQLGuid.cs --- .../System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index dff690f8be809..33fc30e61de99 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -320,7 +320,7 @@ public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet) void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) { - info.AddValue("m_value", ToByteArray()); + info.AddValue("m_value", ToByteArray(), typeof(byte[])); } public static readonly SqlGuid Null; From 899a0db1f6be2b033890f4bf79c856c12b6bc8bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Fri, 12 Aug 2022 20:50:03 +0200 Subject: [PATCH 10/13] Update SQLGuid.cs --- .../System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index 33fc30e61de99..cff92e6f48587 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -282,7 +282,7 @@ public bool Equals(SqlGuid other) => (this == other).Value; // For hashing purpose - public override int GetHashCode() => IsNull ? 0 : _value.GetValueOrDefault().GetHashCode(); + public override int GetHashCode() => _value.GetHashCode(); XmlSchema? IXmlSerializable.GetSchema() { return null; } From 815d51ed892b7a0c42ed4b7bb6223392bbe49212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Petryka?= <35800402+MichalPetryka@users.noreply.github.com> Date: Fri, 18 Nov 2022 19:48:07 +0100 Subject: [PATCH 11/13] Update SQLGuid.cs --- .../System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index 2cfe44f45f174..05b701e7bb83b 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -151,7 +151,8 @@ public static explicit operator SqlGuid(SqlBinary x) // Overloading comparison operators public static SqlBoolean operator ==(SqlGuid x, SqlGuid y) { - return (x.IsNull || y.IsNull) ? SqlBoolean.Null : new SqlBoolean(Compare(x, y) == EComparison.EQ); + return (x.IsNull || y.IsNull) ? SqlBoolean.Null : + new SqlBoolean(x._value.GetValueOrDefault() == y._value.GetValueOrDefault()); } public static SqlBoolean operator !=(SqlGuid x, SqlGuid y) @@ -276,9 +277,7 @@ public override bool Equals([NotNullWhen(true)] object? value) => /// Indicates whether the current instance is equal to another instance of the same type. /// An instance to compare with this instance. /// true if the current instance is equal to the other instance; otherwise, false. - public bool Equals(SqlGuid other) => - other.IsNull || IsNull ? other.IsNull && IsNull : - (this == other).Value; + public bool Equals(SqlGuid other) => _value == other._value; // For hashing purpose public override int GetHashCode() => _value.GetHashCode(); From c51b79c99679ddf8c4854be56ba58c3714a27be2 Mon Sep 17 00:00:00 2001 From: petris Date: Tue, 24 Jan 2023 21:21:04 +0100 Subject: [PATCH 12/13] Add a comment about binary serialization --- .../src/System/Data/SQLTypes/SQLGuid.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index 05b701e7bb83b..ab7850c1a7fa9 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -51,6 +51,9 @@ public SqlGuid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, { } + // Maintains legacy binary serialization behaviour + // see src/libraries/System.Runtime.Serialization.Formatters/tests/BinaryFormatterTestData.cs + // for test data private SqlGuid(SerializationInfo info, StreamingContext context) { byte[]? value = (byte[]?)info.GetValue("m_value", typeof(byte[])); @@ -60,6 +63,11 @@ private SqlGuid(SerializationInfo info, StreamingContext context) _value = new Guid(value); } + void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) + { + info.AddValue("m_value", ToByteArray(), typeof(byte[])); + } + // INullable public bool IsNull => _value is null; @@ -316,11 +324,6 @@ public static XmlQualifiedName GetXsdType(XmlSchemaSet schemaSet) return new XmlQualifiedName("string", XmlSchema.Namespace); } - void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) - { - info.AddValue("m_value", ToByteArray(), typeof(byte[])); - } - public static readonly SqlGuid Null; } // SqlGuid } // namespace System.Data.SqlTypes From f9d3d7d4a6e74eba5151e3d2cbb0c2420693d916 Mon Sep 17 00:00:00 2001 From: Jeff Handley Date: Sun, 29 Jan 2023 23:44:53 -0800 Subject: [PATCH 13/13] Expand on the comment for backwards-compatible binary serialization --- .../System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs index ab7850c1a7fa9..291a1693b9fc4 100644 --- a/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs +++ b/src/libraries/System.Data.Common/src/System/Data/SQLTypes/SQLGuid.cs @@ -51,7 +51,7 @@ public SqlGuid(int a, short b, short c, byte d, byte e, byte f, byte g, byte h, { } - // Maintains legacy binary serialization behaviour + // Maintains backwards-compatible binary serialization for the `private byte[] m_value` field // see src/libraries/System.Runtime.Serialization.Formatters/tests/BinaryFormatterTestData.cs // for test data private SqlGuid(SerializationInfo info, StreamingContext context)