Skip to content

Commit 47853a3

Browse files
committed
Added ValueChecked. Updated benchamarks
1 parent 0c1a6f2 commit 47853a3

8 files changed

+232
-39
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,16 @@ Very fast! Here's some comparisons of various libraries (and the default `enum`
192192

193193
### `Value`
194194
_note that EnumGenerators isn't here as we use the standard C# enum to get its value_
195-
196195
| Method | Mean | Error | StdDev | Allocated |
197196
|----------------|-----------|------------|------------|-----------|
198-
| StandardEnums | 0.0000 ns | 0.0000 ns | 0.0000 ns | - |
199-
| SmartEnums | 0.3225 ns | 0.0082 ns | 0.0076 ns | - |
200-
| **Intellenums** | **0.1295 ns** | **0.0129 ns** | **0.0121 ns** | **-** |
197+
| StandardEnums | 0.0092 ns | 0.0082 ns | 0.0077 ns | - |
198+
| SmartEnums | 0.3246 ns | 0.0082 ns | 0.0069 ns | - |
199+
| **Intellenums** | **0.3198 ns** | **0.0103 ns** | **0.0096 ns** | **-** |
200+
201+
Note that Intellenums also has a `ValueCheck` property which throws if the
202+
value hasn't been initialised. This takes twice as long. This isn't usually a problem
203+
but if you're in a very tight loop and you're sure everything is initialized, then use
204+
`Value` instead
201205

202206

203207
> NOTE: Intellenum is in pre-release at the moment, so probably isn't production ready and the API might (and probably will change).

src/Intellenum/Generators/ClassGenerator.cs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,46 @@ public string BuildClass(VoWorkItem item, TypeDeclarationSyntax tds)
3030
private readonly global::System.Boolean _isInitialized;
3131
private readonly {itemUnderlyingType} _value;
3232
33-
/// <summary>
34-
/// Gets the underlying <see cref=""{itemUnderlyingType}"" /> value if set, otherwise a <see cref=""{nameof(IntellenumValidationException)}"" /> is thrown.
35-
/// </summary>
36-
public {itemUnderlyingType} Value
33+
/// <summary>
34+
/// Gets the underlying <see cref=""{itemUnderlyingType}"" /> value if set, otherwise a <see cref=""{nameof(IntellenumValidationException)}"" /> is thrown.
35+
/// </summary>
36+
public {itemUnderlyingType} ValueChecked
37+
{{
38+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
39+
[global::System.Diagnostics.DebuggerStepThroughAttribute]
40+
get
41+
{{
42+
if(!_isInitialized) Throw();
43+
return _value;
44+
}}
45+
}}
46+
47+
/// <summary>
48+
/// Gets the underlying <see cref=""{itemUnderlyingType}"" /> value if set, otherwise default
49+
/// </summary>
50+
public {itemUnderlyingType} Value
3751
{{
52+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
3853
[global::System.Diagnostics.DebuggerStepThroughAttribute]
3954
get
4055
{{
41-
// EnsureInitialized();
4256
return _value;
4357
}}
4458
}}
4559
60+
private void Throw()
61+
{{
62+
#if DEBUG
63+
global::System.String message = ""Use of uninitialized Value Object at: "" + _stackTrace ?? """";
64+
#else
65+
global::System.String message = ""Use of uninitialized Value Object."";
66+
#endif
67+
68+
// todo: remove this
69+
throw new global::System.InvalidOperationException(message);
70+
71+
}}
72+
4673
[global::System.Diagnostics.DebuggerStepThroughAttribute]
4774
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
4875
public {className}()

tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_attributes.verified.txt

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,46 @@ namespace Whatever
4747
private readonly global::System.Boolean _isInitialized;
4848
private readonly System.Int32 _value;
4949

50-
/// <summary>
51-
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52-
/// </summary>
53-
public System.Int32 Value
50+
/// <summary>
51+
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52+
/// </summary>
53+
public System.Int32 ValueChecked
54+
{
55+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
56+
[global::System.Diagnostics.DebuggerStepThroughAttribute]
57+
get
58+
{
59+
if(!_isInitialized) Throw();
60+
return _value;
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise default
66+
/// </summary>
67+
public System.Int32 Value
5468
{
69+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
5570
[global::System.Diagnostics.DebuggerStepThroughAttribute]
5671
get
5772
{
58-
// EnsureInitialized();
5973
return _value;
6074
}
6175
}
6276

77+
private void Throw()
78+
{
79+
#if DEBUG
80+
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
81+
#else
82+
global::System.String message = "Use of uninitialized Value Object.";
83+
#endif
84+
85+
// todo: remove this
86+
throw new global::System.InvalidOperationException(message);
87+
88+
}
89+
6390
[global::System.Diagnostics.DebuggerStepThroughAttribute]
6491
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
6592
public CustomerType()

tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_Instance_method.verified.txt

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,46 @@ namespace Whatever
4747
private readonly global::System.Boolean _isInitialized;
4848
private readonly System.Int32 _value;
4949

50-
/// <summary>
51-
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52-
/// </summary>
53-
public System.Int32 Value
50+
/// <summary>
51+
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52+
/// </summary>
53+
public System.Int32 ValueChecked
54+
{
55+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
56+
[global::System.Diagnostics.DebuggerStepThroughAttribute]
57+
get
58+
{
59+
if(!_isInitialized) Throw();
60+
return _value;
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise default
66+
/// </summary>
67+
public System.Int32 Value
5468
{
69+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
5570
[global::System.Diagnostics.DebuggerStepThroughAttribute]
5671
get
5772
{
58-
// EnsureInitialized();
5973
return _value;
6074
}
6175
}
6276

77+
private void Throw()
78+
{
79+
#if DEBUG
80+
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
81+
#else
82+
global::System.String message = "Use of uninitialized Value Object.";
83+
#endif
84+
85+
// todo: remove this
86+
throw new global::System.InvalidOperationException(message);
87+
88+
}
89+
6390
[global::System.Diagnostics.DebuggerStepThroughAttribute]
6491
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
6592
public CustomerType()

tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_a_mixture_of_mechanisms.verified.txt

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,46 @@ namespace Whatever
4747
private readonly global::System.Boolean _isInitialized;
4848
private readonly System.Int32 _value;
4949

50-
/// <summary>
51-
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52-
/// </summary>
53-
public System.Int32 Value
50+
/// <summary>
51+
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52+
/// </summary>
53+
public System.Int32 ValueChecked
54+
{
55+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
56+
[global::System.Diagnostics.DebuggerStepThroughAttribute]
57+
get
58+
{
59+
if(!_isInitialized) Throw();
60+
return _value;
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise default
66+
/// </summary>
67+
public System.Int32 Value
5468
{
69+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
5570
[global::System.Diagnostics.DebuggerStepThroughAttribute]
5671
get
5772
{
58-
// EnsureInitialized();
5973
return _value;
6074
}
6175
}
6276

77+
private void Throw()
78+
{
79+
#if DEBUG
80+
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
81+
#else
82+
global::System.String message = "Use of uninitialized Value Object.";
83+
#endif
84+
85+
// todo: remove this
86+
throw new global::System.InvalidOperationException(message);
87+
88+
}
89+
6390
[global::System.Diagnostics.DebuggerStepThroughAttribute]
6491
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
6592
public CustomerType()

tests/ScratchSnapshotTests/snapshots/snap-v7.0/Explicit_instance_tests.Explicit_instances_using_new.verified.txt

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,46 @@ namespace Whatever
4747
private readonly global::System.Boolean _isInitialized;
4848
private readonly System.Int32 _value;
4949

50-
/// <summary>
51-
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52-
/// </summary>
53-
public System.Int32 Value
50+
/// <summary>
51+
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52+
/// </summary>
53+
public System.Int32 ValueChecked
54+
{
55+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
56+
[global::System.Diagnostics.DebuggerStepThroughAttribute]
57+
get
58+
{
59+
if(!_isInitialized) Throw();
60+
return _value;
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise default
66+
/// </summary>
67+
public System.Int32 Value
5468
{
69+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
5570
[global::System.Diagnostics.DebuggerStepThroughAttribute]
5671
get
5772
{
58-
// EnsureInitialized();
5973
return _value;
6074
}
6175
}
6276

77+
private void Throw()
78+
{
79+
#if DEBUG
80+
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
81+
#else
82+
global::System.String message = "Use of uninitialized Value Object.";
83+
#endif
84+
85+
// todo: remove this
86+
throw new global::System.InvalidOperationException(message);
87+
88+
}
89+
6390
[global::System.Diagnostics.DebuggerStepThroughAttribute]
6491
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
6592
public CustomerType()

tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.int_created_successfully.verified.txt

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,46 @@ namespace Whatever
4747
private readonly global::System.Boolean _isInitialized;
4848
private readonly System.Int32 _value;
4949

50-
/// <summary>
51-
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52-
/// </summary>
53-
public System.Int32 Value
50+
/// <summary>
51+
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52+
/// </summary>
53+
public System.Int32 ValueChecked
54+
{
55+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
56+
[global::System.Diagnostics.DebuggerStepThroughAttribute]
57+
get
58+
{
59+
if(!_isInitialized) Throw();
60+
return _value;
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise default
66+
/// </summary>
67+
public System.Int32 Value
5468
{
69+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
5570
[global::System.Diagnostics.DebuggerStepThroughAttribute]
5671
get
5772
{
58-
// EnsureInitialized();
5973
return _value;
6074
}
6175
}
6276

77+
private void Throw()
78+
{
79+
#if DEBUG
80+
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
81+
#else
82+
global::System.String message = "Use of uninitialized Value Object.";
83+
#endif
84+
85+
// todo: remove this
86+
throw new global::System.InvalidOperationException(message);
87+
88+
}
89+
6390
[global::System.Diagnostics.DebuggerStepThroughAttribute]
6491
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
6592
public CustomerType()

tests/ScratchSnapshotTests/snapshots/snap-v7.0/GeneralTests.string_created_successfully.verified.txt

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,46 @@ namespace Whatever
4747
private readonly global::System.Boolean _isInitialized;
4848
private readonly System.String _value;
4949

50-
/// <summary>
51-
/// Gets the underlying <see cref="System.String" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52-
/// </summary>
53-
public System.String Value
50+
/// <summary>
51+
/// Gets the underlying <see cref="System.String" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
52+
/// </summary>
53+
public System.String ValueChecked
54+
{
55+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
56+
[global::System.Diagnostics.DebuggerStepThroughAttribute]
57+
get
58+
{
59+
if(!_isInitialized) Throw();
60+
return _value;
61+
}
62+
}
63+
64+
/// <summary>
65+
/// Gets the underlying <see cref="System.String" /> value if set, otherwise default
66+
/// </summary>
67+
public System.String Value
5468
{
69+
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
5570
[global::System.Diagnostics.DebuggerStepThroughAttribute]
5671
get
5772
{
58-
// EnsureInitialized();
5973
return _value;
6074
}
6175
}
6276

77+
private void Throw()
78+
{
79+
#if DEBUG
80+
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
81+
#else
82+
global::System.String message = "Use of uninitialized Value Object.";
83+
#endif
84+
85+
// todo: remove this
86+
throw new global::System.InvalidOperationException(message);
87+
88+
}
89+
6390
[global::System.Diagnostics.DebuggerStepThroughAttribute]
6491
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
6592
public CustomerType()

0 commit comments

Comments
 (0)