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

Optimize TypeExtensions.HexStr #1278

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

paulomorgado
Copy link
Contributor

The current implementation creates length * 2 + 1 strings without separator and length * 3 strings with separator.

The proposed implementation use Create<TState>(Int32, TState, SpanAction<Char,TState>), where possible, to allocate only the final string and an intermediary byte[] where not possible.

Copy link
Member

@sipsorcery sipsorcery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The HexStr method is only used fo deug/diagnostic purposes so clarity is more relevant than optimisation. Still, happy to if the "separator is { } s" expression can be simplified.

@@ -122,21 +122,60 @@ public static string HexStr(this byte[] buffer, char? separator = null)

public static string HexStr(this byte[] buffer, int length, char? separator = null)
{
string rv = string.Empty;
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
if (separator is { } s)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(separator != null) is a lot clearer here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants