Skip to content

Optimize Encoders.Hex.EncodeData(ReadOnlySpan<byte> data) - #1332

Closed
Carti-it wants to merge 2 commits into
MetacoSA:masterfrom
Carti-it:hex-optimize
Closed

Carti-it wants to merge 2 commits into
MetacoSA:masterfrom
Carti-it:hex-optimize

Conversation

@Carti-it

@Carti-it Carti-it commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

By default

public virtual string EncodeData(ReadOnlySpan<byte> data)
{
	return this.EncodeData(data.ToArray());
}

is called. However, this allocates an array so it beats any improvement that ReadOnlySpan<byte> provides.

This PR adds:

#if NET8_0_OR_GREATER
		public override string EncodeData(ReadOnlySpan<byte> data)
		{
			// TODO: Convert.ToHexStringLower (.NET 9+) once available.
			return Convert.ToHexString(data).ToLowerInvariant();
		}
#endif

to avoid copying span data.

@NicolasDorier

Copy link
Copy Markdown
Collaborator

I believe we should start shipping NBitcoin version for .NET10 and just use the ToHexStringLower there.
Can you do this? Also, you will need to update the CI so that we replace .NET8.0 tests with .NET10.0.

@Carti-it

Carti-it commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

I believe we should start shipping NBitcoin version for .NET10 and just use the ToHexStringLower there.
Can you do this? Also, you will need to update the CI so that we replace .NET8.0 tests with .NET10.0.

Yes, I have already done it locally. I will try to publish it today or tomorrow.

@Carti-it

Carti-it commented Sep 7, 2026

Copy link
Copy Markdown
Contributor Author

I opened #1333

@Carti-it Carti-it closed this Sep 7, 2026
@Carti-it
Carti-it deleted the hex-optimize branch September 7, 2026 08:15
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