Skip to content

Commit

Permalink
Added ValueChecked. Updated benchamarks
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveDunn committed Mar 17, 2023
1 parent 0c1a6f2 commit 47853a3
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 39 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,16 @@ Very fast! Here's some comparisons of various libraries (and the default `enum`

### `Value`
_note that EnumGenerators isn't here as we use the standard C# enum to get its value_

| Method | Mean | Error | StdDev | Allocated |
|----------------|-----------|------------|------------|-----------|
| StandardEnums | 0.0000 ns | 0.0000 ns | 0.0000 ns | - |
| SmartEnums | 0.3225 ns | 0.0082 ns | 0.0076 ns | - |
| **Intellenums** | **0.1295 ns** | **0.0129 ns** | **0.0121 ns** | **-** |
| StandardEnums | 0.0092 ns | 0.0082 ns | 0.0077 ns | - |
| SmartEnums | 0.3246 ns | 0.0082 ns | 0.0069 ns | - |
| **Intellenums** | **0.3198 ns** | **0.0103 ns** | **0.0096 ns** | **-** |

Note that Intellenums also has a `ValueCheck` property which throws if the
value hasn't been initialised. This takes twice as long. This isn't usually a problem
but if you're in a very tight loop and you're sure everything is initialized, then use
`Value` instead


> NOTE: Intellenum is in pre-release at the moment, so probably isn't production ready and the API might (and probably will change).
Expand Down
37 changes: 32 additions & 5 deletions src/Intellenum/Generators/ClassGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,46 @@ public string BuildClass(VoWorkItem item, TypeDeclarationSyntax tds)
private readonly global::System.Boolean _isInitialized;
private readonly {itemUnderlyingType} _value;
/// <summary>
/// Gets the underlying <see cref=""{itemUnderlyingType}"" /> value if set, otherwise a <see cref=""{nameof(IntellenumValidationException)}"" /> is thrown.
/// </summary>
public {itemUnderlyingType} Value
/// <summary>
/// Gets the underlying <see cref=""{itemUnderlyingType}"" /> value if set, otherwise a <see cref=""{nameof(IntellenumValidationException)}"" /> is thrown.
/// </summary>
public {itemUnderlyingType} ValueChecked
{{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{{
if(!_isInitialized) Throw();
return _value;
}}
}}
/// <summary>
/// Gets the underlying <see cref=""{itemUnderlyingType}"" /> value if set, otherwise default
/// </summary>
public {itemUnderlyingType} Value
{{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{{
// EnsureInitialized();
return _value;
}}
}}
private void Throw()
{{
#if DEBUG
global::System.String message = ""Use of uninitialized Value Object at: "" + _stackTrace ?? """";
#else
global::System.String message = ""Use of uninitialized Value Object."";
#endif
// todo: remove this
throw new global::System.InvalidOperationException(message);
}}
[global::System.Diagnostics.DebuggerStepThroughAttribute]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public {className}()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,46 @@ namespace Whatever
private readonly global::System.Boolean _isInitialized;
private readonly System.Int32 _value;

/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.Int32 Value
/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.Int32 ValueChecked
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
if(!_isInitialized) Throw();
return _value;
}
}

/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise default
/// </summary>
public System.Int32 Value
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
// EnsureInitialized();
return _value;
}
}

private void Throw()
{
#if DEBUG
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
#else
global::System.String message = "Use of uninitialized Value Object.";
#endif

// todo: remove this
throw new global::System.InvalidOperationException(message);

}

[global::System.Diagnostics.DebuggerStepThroughAttribute]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public CustomerType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,46 @@ namespace Whatever
private readonly global::System.Boolean _isInitialized;
private readonly System.Int32 _value;

/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.Int32 Value
/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.Int32 ValueChecked
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
if(!_isInitialized) Throw();
return _value;
}
}

/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise default
/// </summary>
public System.Int32 Value
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
// EnsureInitialized();
return _value;
}
}

private void Throw()
{
#if DEBUG
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
#else
global::System.String message = "Use of uninitialized Value Object.";
#endif

// todo: remove this
throw new global::System.InvalidOperationException(message);

}

[global::System.Diagnostics.DebuggerStepThroughAttribute]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public CustomerType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,46 @@ namespace Whatever
private readonly global::System.Boolean _isInitialized;
private readonly System.Int32 _value;

/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.Int32 Value
/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.Int32 ValueChecked
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
if(!_isInitialized) Throw();
return _value;
}
}

/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise default
/// </summary>
public System.Int32 Value
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
// EnsureInitialized();
return _value;
}
}

private void Throw()
{
#if DEBUG
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
#else
global::System.String message = "Use of uninitialized Value Object.";
#endif

// todo: remove this
throw new global::System.InvalidOperationException(message);

}

[global::System.Diagnostics.DebuggerStepThroughAttribute]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public CustomerType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,46 @@ namespace Whatever
private readonly global::System.Boolean _isInitialized;
private readonly System.Int32 _value;

/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.Int32 Value
/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.Int32 ValueChecked
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
if(!_isInitialized) Throw();
return _value;
}
}

/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise default
/// </summary>
public System.Int32 Value
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
// EnsureInitialized();
return _value;
}
}

private void Throw()
{
#if DEBUG
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
#else
global::System.String message = "Use of uninitialized Value Object.";
#endif

// todo: remove this
throw new global::System.InvalidOperationException(message);

}

[global::System.Diagnostics.DebuggerStepThroughAttribute]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public CustomerType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,46 @@ namespace Whatever
private readonly global::System.Boolean _isInitialized;
private readonly System.Int32 _value;

/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.Int32 Value
/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.Int32 ValueChecked
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
if(!_isInitialized) Throw();
return _value;
}
}

/// <summary>
/// Gets the underlying <see cref="System.Int32" /> value if set, otherwise default
/// </summary>
public System.Int32 Value
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
// EnsureInitialized();
return _value;
}
}

private void Throw()
{
#if DEBUG
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
#else
global::System.String message = "Use of uninitialized Value Object.";
#endif

// todo: remove this
throw new global::System.InvalidOperationException(message);

}

[global::System.Diagnostics.DebuggerStepThroughAttribute]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public CustomerType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,46 @@ namespace Whatever
private readonly global::System.Boolean _isInitialized;
private readonly System.String _value;

/// <summary>
/// Gets the underlying <see cref="System.String" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.String Value
/// <summary>
/// Gets the underlying <see cref="System.String" /> value if set, otherwise a <see cref="IntellenumValidationException" /> is thrown.
/// </summary>
public System.String ValueChecked
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
if(!_isInitialized) Throw();
return _value;
}
}

/// <summary>
/// Gets the underlying <see cref="System.String" /> value if set, otherwise default
/// </summary>
public System.String Value
{
[global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
[global::System.Diagnostics.DebuggerStepThroughAttribute]
get
{
// EnsureInitialized();
return _value;
}
}

private void Throw()
{
#if DEBUG
global::System.String message = "Use of uninitialized Value Object at: " + _stackTrace ?? "";
#else
global::System.String message = "Use of uninitialized Value Object.";
#endif

// todo: remove this
throw new global::System.InvalidOperationException(message);

}

[global::System.Diagnostics.DebuggerStepThroughAttribute]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public CustomerType()
Expand Down

0 comments on commit 47853a3

Please sign in to comment.