Skip to content

Commit c3662c9

Browse files
authored
Cache zero values to reduce boxing in PrimitiveType.DefaultValue (#3552)
1 parent aaf03e6 commit c3662c9

20 files changed

+142
-374
lines changed

src/NHibernate/Async/Type/Int16Type.cs

-4
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ namespace NHibernate.Type
2424
public partial class Int16Type : PrimitiveType, IDiscriminatorType, IVersionType
2525
{
2626

27-
#region IVersionType Members
28-
2927
public virtual Task<object> NextAsync(object current, ISessionImplementor session, CancellationToken cancellationToken)
3028
{
3129
if (cancellationToken.IsCancellationRequested)
@@ -57,7 +55,5 @@ public virtual Task<object> SeedAsync(ISessionImplementor session, CancellationT
5755
return Task.FromException<object>(ex);
5856
}
5957
}
60-
61-
#endregion
6258
}
6359
}

src/NHibernate/Type/AbstractCharType.cs

+6-13
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ namespace NHibernate.Type
1111
[Serializable]
1212
public abstract class AbstractCharType : PrimitiveType, IDiscriminatorType
1313
{
14-
public AbstractCharType(SqlType sqlType)
15-
: base(sqlType) {}
16-
17-
public override object DefaultValue
14+
/// <summary />
15+
public AbstractCharType(SqlType sqlType) : base(sqlType)
1816
{
19-
get { throw new NotSupportedException("not a valid id type"); }
2017
}
2118

19+
public override object DefaultValue => throw new NotSupportedException("not a valid id type");
20+
2221
public override object Get(DbDataReader rs, int index, ISessionImplementor session)
2322
{
2423
string dbValue = Convert.ToString(rs[index]);
@@ -30,15 +29,9 @@ public override object Get(DbDataReader rs, int index, ISessionImplementor sessi
3029
return '\0'; // This line should never be executed
3130
}
3231

33-
public override System.Type PrimitiveClass
34-
{
35-
get { return typeof(char); }
36-
}
32+
public override System.Type PrimitiveClass => typeof(char);
3733

38-
public override System.Type ReturnedClass
39-
{
40-
get { return typeof(char); }
41-
}
34+
public override System.Type ReturnedClass => typeof(char);
4235

4336
public override void Set(DbCommand cmd, object value, int index, ISessionImplementor session)
4437
{

src/NHibernate/Type/AbstractDateTimeType.cs

+4-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace NHibernate.Type
1414
[Serializable]
1515
public abstract partial class AbstractDateTimeType : PrimitiveType, IIdentifierType, ILiteralType, IVersionType
1616
{
17-
private static readonly DateTime BaseDateValue = DateTime.MinValue;
17+
private static readonly object BaseDateValue = DateTime.MinValue;
1818

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

34-
/// <summary>
35-
/// Default constructor.
36-
/// </summary>
37-
protected AbstractDateTimeType() : base(SqlTypeFactory.DateTime)
34+
/// <summary />
35+
protected AbstractDateTimeType() : this(SqlTypeFactory.DateTime)
3836
{
3937
}
4038

41-
/// <summary>
42-
/// Constructor for overriding the default <see cref="SqlType"/>.
43-
/// </summary>
44-
/// <param name="sqlTypeDateTime">The <see cref="SqlType"/> to use.</param>
39+
/// <summary />
4540
protected AbstractDateTimeType(SqlType sqlTypeDateTime) : base(sqlTypeDateTime)
4641
{
4742
}

src/NHibernate/Type/ByteType.cs

+9-24
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ namespace NHibernate.Type
1515
[Serializable]
1616
public partial class ByteType : PrimitiveType, IDiscriminatorType, IVersionType
1717
{
18-
private static readonly byte ZERO = 0;
18+
private static readonly object ZeroObject = (byte) 0;
1919

20-
public ByteType()
21-
: base(SqlTypeFactory.Byte)
20+
/// <summary />
21+
public ByteType() : base(SqlTypeFactory.Byte)
2222
{
2323
}
2424

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

35-
public override System.Type ReturnedClass
36-
{
37-
get { return typeof(byte); }
38-
}
35+
public override System.Type ReturnedClass => typeof(byte);
3936

40-
public override System.Type PrimitiveClass
41-
{
42-
get { return typeof(byte); }
43-
}
37+
public override System.Type PrimitiveClass => typeof(byte);
4438

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

51-
public override string Name
52-
{
53-
get { return "Byte"; }
54-
}
45+
public override string Name => "Byte";
5546

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

8576
public virtual object Seed(ISessionImplementor session)
8677
{
87-
return ZERO;
78+
return ZeroObject;
8879
}
8980

90-
public IComparer Comparator
91-
{
92-
get { return Comparer.DefaultInvariant; }
93-
}
81+
public IComparer Comparator => Comparer.DefaultInvariant;
9482

95-
public override object DefaultValue
96-
{
97-
get { return ZERO; }
98-
}
83+
public override object DefaultValue => ZeroObject;
9984
}
10085
}

src/NHibernate/Type/DateTimeOffSetType.cs

+7-28
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,26 @@ namespace NHibernate.Type
1414
[Serializable]
1515
public partial class DateTimeOffsetType : PrimitiveType, IIdentifierType, ILiteralType, IVersionType
1616
{
17-
static readonly DateTimeOffset BaseDateValue = DateTimeOffset.MinValue;
17+
private static readonly object BaseDateValue = DateTimeOffset.MinValue;
1818

19-
/// <summary>
20-
/// Default constructor.
21-
/// </summary>
2219
public DateTimeOffsetType() : base(SqlTypeFactory.DateTimeOffSet)
2320
{
2421
}
2522

26-
/// <summary>
27-
/// Constructor for specifying a datetimeoffset with a scale. Use <see cref="SqlTypeFactory.GetDateTimeOffset"/>.
28-
/// </summary>
29-
/// <param name="sqlType">The sql type to use for the type.</param>
23+
/// <summary />
3024
public DateTimeOffsetType(DateTimeOffsetSqlType sqlType) : base(sqlType)
3125
{
3226
}
3327

34-
public override string Name
35-
{
36-
get { return "DateTimeOffset"; }
37-
}
28+
public override string Name => "DateTimeOffset";
3829

39-
public override System.Type ReturnedClass
40-
{
41-
get { return typeof (DateTimeOffset); }
42-
}
30+
public override System.Type ReturnedClass => typeof (DateTimeOffset);
4331

44-
public override System.Type PrimitiveClass
45-
{
46-
get { return typeof (DateTimeOffset); }
47-
}
32+
public override System.Type PrimitiveClass => typeof (DateTimeOffset);
4833

49-
public override object DefaultValue
50-
{
51-
get { return BaseDateValue; }
52-
}
34+
public override object DefaultValue => BaseDateValue;
5335

54-
public IComparer Comparator
55-
{
56-
get { return Comparer<DateTimeOffset>.Default; }
57-
}
36+
public IComparer Comparator => Comparer<DateTimeOffset>.Default;
5837

5938
public override void Set(DbCommand st, object value, int index, ISessionImplementor session)
6039
{

src/NHibernate/Type/DateType.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ public class DateType : AbstractDateTimeType, IParameterizedType
2121
// Since v5.0
2222
[Obsolete("Use DateTime.MinValue.")]
2323
public static readonly DateTime BaseDateValue = DateTime.MinValue;
24-
private DateTime customBaseDate = _baseDateValue;
2524

2625
private static readonly DateTime _baseDateValue = DateTime.MinValue;
2726

28-
/// <summary>Default constructor</summary>
27+
private object customBaseDate = _baseDateValue;
28+
29+
/// <summary />
2930
public DateType() : base(SqlTypeFactory.Date)
3031
{
3132
}

src/NHibernate/Type/DecimalType.cs

+8-16
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@ namespace NHibernate.Type
1313
[Serializable]
1414
public class DecimalType : PrimitiveType, IIdentifierType
1515
{
16+
private static readonly object ZeroObject = 0m;
17+
18+
/// <summary />
1619
public DecimalType()
1720
: this(SqlTypeFactory.Decimal)
1821
{
1922
}
2023

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

30-
public override System.Type ReturnedClass
31-
{
32-
get { return typeof(Decimal); }
33-
}
34+
public override System.Type ReturnedClass => typeof(Decimal);
3435

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

40-
public override string Name
41-
{
42-
get { return "Decimal"; }
43-
}
41+
public override string Name => "Decimal";
4442

45-
public override System.Type PrimitiveClass
46-
{
47-
get { return typeof (Decimal); }
48-
}
43+
public override System.Type PrimitiveClass => typeof (Decimal);
4944

50-
public override object DefaultValue
51-
{
52-
get { return 0m; }
53-
}
45+
public override object DefaultValue => ZeroObject;
5446

5547
// Since 5.2
5648
[Obsolete("This method has no more usages and will be removed in a future version.")]

src/NHibernate/Type/DoubleType.cs

+11-18
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ namespace NHibernate.Type
1414
[Serializable]
1515
public class DoubleType : PrimitiveType
1616
{
17-
/// <summary></summary>
17+
private static readonly object ZeroObject = 0D;
18+
19+
/// <summary />
1820
public DoubleType() : base(SqlTypeFactory.Double)
1921
{
2022
}
2123

22-
public DoubleType(SqlType sqlType) : base(sqlType) {}
24+
/// <summary />
25+
public DoubleType(SqlType sqlType) : base(sqlType)
26+
{
27+
}
2328

2429
public override object Get(DbDataReader rs, int index, ISessionImplementor session)
2530
{
@@ -31,21 +36,15 @@ public override object Get(DbDataReader rs, int index, ISessionImplementor sessi
3136
}
3237

3338
/// <summary></summary>
34-
public override System.Type ReturnedClass
35-
{
36-
get { return typeof(double); }
37-
}
39+
public override System.Type ReturnedClass => typeof(double);
3840

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

4446
/// <summary></summary>
45-
public override string Name
46-
{
47-
get { return "Double"; }
48-
}
47+
public override string Name => "Double";
4948

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

57-
public override System.Type PrimitiveClass
58-
{
59-
get { return typeof(double); }
60-
}
56+
public override System.Type PrimitiveClass => typeof(double);
6157

62-
public override object DefaultValue
63-
{
64-
get { return 0D; }
65-
}
58+
public override object DefaultValue => ZeroObject;
6659

6760
public override string ObjectToSQLString(object value, Dialect.Dialect dialect)
6861
{

src/NHibernate/Type/GuidType.cs

+7-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ namespace NHibernate.Type
1313
[Serializable]
1414
public class GuidType : PrimitiveType, IDiscriminatorType
1515
{
16-
/// <summary></summary>
16+
private static readonly object EmptyObject = Guid.Empty;
17+
18+
/// <summary />
1719
public GuidType() : base(SqlTypeFactory.Guid)
1820
{
1921
}
@@ -34,10 +36,7 @@ public override object Get(DbDataReader rs, int index, ISessionImplementor sessi
3436
}
3537

3638
/// <summary></summary>
37-
public override System.Type ReturnedClass
38-
{
39-
get { return typeof(Guid); }
40-
}
39+
public override System.Type ReturnedClass => typeof(Guid);
4140

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

4948
/// <summary></summary>
50-
public override string Name
51-
{
52-
get { return "Guid"; }
53-
}
49+
public override string Name => "Guid";
5450

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

74-
public override System.Type PrimitiveClass
75-
{
76-
get { return typeof(Guid); }
77-
}
70+
public override System.Type PrimitiveClass => typeof(Guid);
7871

79-
public override object DefaultValue
80-
{
81-
get { return Guid.Empty; }
82-
}
72+
public override object DefaultValue => EmptyObject;
8373

8474
public override string ObjectToSQLString(object value, Dialect.Dialect dialect)
8575
{

0 commit comments

Comments
 (0)