Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache zero values to reduce boxing in PrimitiveType.DefaultValue #3552

Merged
merged 14 commits into from
May 24, 2024
4 changes: 0 additions & 4 deletions src/NHibernate/Async/Type/Int16Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ namespace NHibernate.Type
public partial class Int16Type : PrimitiveType, IDiscriminatorType, IVersionType
{

#region IVersionType Members

public virtual Task<object> NextAsync(object current, ISessionImplementor session, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
Expand Down Expand Up @@ -57,7 +55,5 @@ public virtual Task<object> SeedAsync(ISessionImplementor session, CancellationT
return Task.FromException<object>(ex);
}
}

#endregion
}
}
19 changes: 6 additions & 13 deletions src/NHibernate/Type/AbstractCharType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ namespace NHibernate.Type
[Serializable]
public abstract class AbstractCharType : PrimitiveType, IDiscriminatorType
{
public AbstractCharType(SqlType sqlType)
: base(sqlType) {}

public override object DefaultValue
/// <summary />
public AbstractCharType(SqlType sqlType) : base(sqlType)
{
get { throw new NotSupportedException("not a valid id type"); }
}

public override object DefaultValue => throw new NotSupportedException("not a valid id type");

public override object Get(DbDataReader rs, int index, ISessionImplementor session)
{
string dbValue = Convert.ToString(rs[index]);
Expand All @@ -30,15 +29,9 @@ public override object Get(DbDataReader rs, int index, ISessionImplementor sessi
return '\0'; // This line should never be executed
}

public override System.Type PrimitiveClass
{
get { return typeof(char); }
}
public override System.Type PrimitiveClass => typeof(char);

public override System.Type ReturnedClass
{
get { return typeof(char); }
}
public override System.Type ReturnedClass => typeof(char);

public override void Set(DbCommand cmd, object value, int index, ISessionImplementor session)
{
Expand Down
13 changes: 4 additions & 9 deletions src/NHibernate/Type/AbstractDateTimeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace NHibernate.Type
[Serializable]
public abstract partial class AbstractDateTimeType : PrimitiveType, IIdentifierType, ILiteralType, IVersionType
{
private static readonly DateTime BaseDateValue = DateTime.MinValue;
private static readonly object BaseDateValue = DateTime.MinValue;

/// <summary>
/// Returns the <see cref="DateTimeKind" /> for the type.
Expand All @@ -31,17 +31,12 @@ public abstract partial class AbstractDateTimeType : PrimitiveType, IIdentifierT
/// <see cref="DateTime.Now" /> otherwise.</value>
protected virtual DateTime Now => Kind == DateTimeKind.Utc ? DateTime.UtcNow : DateTime.Now;

/// <summary>
/// Default constructor.
/// </summary>
protected AbstractDateTimeType() : base(SqlTypeFactory.DateTime)
/// <summary />
protected AbstractDateTimeType() : this(SqlTypeFactory.DateTime)
{
}

/// <summary>
/// Constructor for overriding the default <see cref="SqlType"/>.
/// </summary>
/// <param name="sqlTypeDateTime">The <see cref="SqlType"/> to use.</param>
/// <summary />
protected AbstractDateTimeType(SqlType sqlTypeDateTime) : base(sqlTypeDateTime)
{
}
Expand Down
33 changes: 9 additions & 24 deletions src/NHibernate/Type/ByteType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ namespace NHibernate.Type
[Serializable]
public partial class ByteType : PrimitiveType, IDiscriminatorType, IVersionType
{
private static readonly byte ZERO = 0;
private static readonly object ZeroObject = (byte) 0;

public ByteType()
: base(SqlTypeFactory.Byte)
/// <summary />
public ByteType() : base(SqlTypeFactory.Byte)
{
}

Expand All @@ -32,26 +32,17 @@ public override object Get(DbDataReader rs, int index, ISessionImplementor sessi
};
}

public override System.Type ReturnedClass
{
get { return typeof(byte); }
}
public override System.Type ReturnedClass => typeof(byte);

public override System.Type PrimitiveClass
{
get { return typeof(byte); }
}
public override System.Type PrimitiveClass => typeof(byte);

public override void Set(DbCommand cmd, object value, int index, ISessionImplementor session)
{
var dp = cmd.Parameters[index];
dp.Value = dp.DbType == DbType.Int16 ? Convert.ToInt16(value) : Convert.ToByte(value);
}

public override string Name
{
get { return "Byte"; }
}
public override string Name => "Byte";

public override string ObjectToSQLString(object value, Dialect.Dialect dialect)
{
Expand Down Expand Up @@ -84,17 +75,11 @@ public virtual object Next(object current, ISessionImplementor session)

public virtual object Seed(ISessionImplementor session)
{
return ZERO;
return ZeroObject;
}

public IComparer Comparator
{
get { return Comparer.DefaultInvariant; }
}
public IComparer Comparator => Comparer.DefaultInvariant;

public override object DefaultValue
{
get { return ZERO; }
}
public override object DefaultValue => ZeroObject;
}
}
35 changes: 7 additions & 28 deletions src/NHibernate/Type/DateTimeOffSetType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,26 @@ namespace NHibernate.Type
[Serializable]
public partial class DateTimeOffsetType : PrimitiveType, IIdentifierType, ILiteralType, IVersionType
{
static readonly DateTimeOffset BaseDateValue = DateTimeOffset.MinValue;
private static readonly object BaseDateValue = DateTimeOffset.MinValue;

/// <summary>
/// Default constructor.
/// </summary>
public DateTimeOffsetType() : base(SqlTypeFactory.DateTimeOffSet)
{
}

/// <summary>
/// Constructor for specifying a datetimeoffset with a scale. Use <see cref="SqlTypeFactory.GetDateTimeOffset"/>.
/// </summary>
/// <param name="sqlType">The sql type to use for the type.</param>
/// <summary />
public DateTimeOffsetType(DateTimeOffsetSqlType sqlType) : base(sqlType)
{
}

public override string Name
{
get { return "DateTimeOffset"; }
}
public override string Name => "DateTimeOffset";

public override System.Type ReturnedClass
{
get { return typeof (DateTimeOffset); }
}
public override System.Type ReturnedClass => typeof (DateTimeOffset);

public override System.Type PrimitiveClass
{
get { return typeof (DateTimeOffset); }
}
public override System.Type PrimitiveClass => typeof (DateTimeOffset);

public override object DefaultValue
{
get { return BaseDateValue; }
}
public override object DefaultValue => BaseDateValue;

public IComparer Comparator
{
get { return Comparer<DateTimeOffset>.Default; }
}
public IComparer Comparator => Comparer<DateTimeOffset>.Default;

public override void Set(DbCommand st, object value, int index, ISessionImplementor session)
{
Expand Down
5 changes: 3 additions & 2 deletions src/NHibernate/Type/DateType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public class DateType : AbstractDateTimeType, IParameterizedType
// Since v5.0
[Obsolete("Use DateTime.MinValue.")]
public static readonly DateTime BaseDateValue = DateTime.MinValue;
private DateTime customBaseDate = _baseDateValue;

private static readonly DateTime _baseDateValue = DateTime.MinValue;

/// <summary>Default constructor</summary>
private object customBaseDate = _baseDateValue;

/// <summary />
public DateType() : base(SqlTypeFactory.Date)
{
}
Expand Down
24 changes: 8 additions & 16 deletions src/NHibernate/Type/DecimalType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ namespace NHibernate.Type
[Serializable]
public class DecimalType : PrimitiveType, IIdentifierType
{
private static readonly object ZeroObject = 0m;

/// <summary />
public DecimalType()
: this(SqlTypeFactory.Decimal)
{
}

/// <summary />
public DecimalType(SqlType sqlType) : base(sqlType)
{
}
Expand All @@ -27,30 +31,18 @@ public override object Get(DbDataReader rs, int index, ISessionImplementor sessi
return Convert.ToDecimal(rs[index]);
}

public override System.Type ReturnedClass
{
get { return typeof(Decimal); }
}
public override System.Type ReturnedClass => typeof(Decimal);

public override void Set(DbCommand st, object value, int index, ISessionImplementor session)
{
st.Parameters[index].Value = Convert.ToDecimal(value);
}

public override string Name
{
get { return "Decimal"; }
}
public override string Name => "Decimal";

public override System.Type PrimitiveClass
{
get { return typeof (Decimal); }
}
public override System.Type PrimitiveClass => typeof (Decimal);

public override object DefaultValue
{
get { return 0m; }
}
public override object DefaultValue => ZeroObject;

// Since 5.2
[Obsolete("This method has no more usages and will be removed in a future version.")]
Expand Down
29 changes: 11 additions & 18 deletions src/NHibernate/Type/DoubleType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ namespace NHibernate.Type
[Serializable]
public class DoubleType : PrimitiveType
{
/// <summary></summary>
private static readonly object ZeroObject = 0D;

/// <summary />
public DoubleType() : base(SqlTypeFactory.Double)
{
}

public DoubleType(SqlType sqlType) : base(sqlType) {}
/// <summary />
public DoubleType(SqlType sqlType) : base(sqlType)
{
}

public override object Get(DbDataReader rs, int index, ISessionImplementor session)
{
Expand All @@ -31,21 +36,15 @@ public override object Get(DbDataReader rs, int index, ISessionImplementor sessi
}

/// <summary></summary>
public override System.Type ReturnedClass
{
get { return typeof(double); }
}
public override System.Type ReturnedClass => typeof(double);

public override void Set(DbCommand st, object value, int index, ISessionImplementor session)
{
st.Parameters[index].Value = Convert.ToDouble(value);
}

/// <summary></summary>
public override string Name
{
get { return "Double"; }
}
public override string Name => "Double";

// Since 5.2
[Obsolete("This method has no more usages and will be removed in a future version.")]
Expand All @@ -54,15 +53,9 @@ public override object FromStringValue(string xml)
return double.Parse(xml);
}

public override System.Type PrimitiveClass
{
get { return typeof(double); }
}
public override System.Type PrimitiveClass => typeof(double);

public override object DefaultValue
{
get { return 0D; }
}
public override object DefaultValue => ZeroObject;

public override string ObjectToSQLString(object value, Dialect.Dialect dialect)
{
Expand Down
24 changes: 7 additions & 17 deletions src/NHibernate/Type/GuidType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ namespace NHibernate.Type
[Serializable]
public class GuidType : PrimitiveType, IDiscriminatorType
{
/// <summary></summary>
private static readonly object EmptyObject = Guid.Empty;

/// <summary />
public GuidType() : base(SqlTypeFactory.Guid)
{
}
Expand All @@ -34,10 +36,7 @@ public override object Get(DbDataReader rs, int index, ISessionImplementor sessi
}

/// <summary></summary>
public override System.Type ReturnedClass
{
get { return typeof(Guid); }
}
public override System.Type ReturnedClass => typeof(Guid);

public override void Set(DbCommand cmd, object value, int index, ISessionImplementor session)
{
Expand All @@ -47,10 +46,7 @@ public override void Set(DbCommand cmd, object value, int index, ISessionImpleme
}

/// <summary></summary>
public override string Name
{
get { return "Guid"; }
}
public override string Name => "Guid";

// Since 5.2
[Obsolete("This method has no more usages and will be removed in a future version.")]
Expand All @@ -71,15 +67,9 @@ public object StringToObject(string xml)
#pragma warning restore 618
}

public override System.Type PrimitiveClass
{
get { return typeof(Guid); }
}
public override System.Type PrimitiveClass => typeof(Guid);

public override object DefaultValue
{
get { return Guid.Empty; }
}
public override object DefaultValue => EmptyObject;

public override string ObjectToSQLString(object value, Dialect.Dialect dialect)
{
Expand Down
Loading
Loading