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

Improve WASM performance #136

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Examples/WebAssembly/Pages/Home.razor
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
else
{
<p>Signatures remaining: @Xmss.SignaturesRemaining</p>
<p><FluentButton @onclick="DestroyKey">Destroy</FluentButton></p>
<p><FluentButton @onclick="DestroyKey" Disabled="IsCalculating">Destroy</FluentButton></p>

<h2>Public Key</h2>

Expand Down Expand Up @@ -109,6 +109,7 @@ else

async Task CalculatePublicKey()
{
ProgressPercentage = 0;
IsCalculating = true;

CancellationTokenSource.Dispose();
Expand Down
19 changes: 13 additions & 6 deletions Xmss/Xmss.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public Xmss()
static bool TriedRegisterOnce;

/// <summary>
/// Registers the <see cref="Xmss"/> implementation with <see cref="CryptoConfig"/>, such that it can be created
/// by name or <see cref="Oid"/>.
/// Registers the <see cref="Xmss"/> class with <see cref="CryptoConfig"/>, such that its <see cref="Oid"/> is known and it can be created by name.
/// </summary>
/// <seealso cref="Xmss.IdAlgXmssHashsig"/>
/// <seealso cref="CryptoConfig.CreateFromName(string)"/>
Expand Down Expand Up @@ -530,6 +529,7 @@ public async Task CalculatePublicKeyAsync(Action<double>? reportPercentage = nul
var index = 0;
var completed = 0;
var lastReported = 0;
var lastDelay = Stopwatch.GetTimestamp();
using var cancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
Exception? taskException = null;
while (!cancellationTokenSource.IsCancellationRequested && completed < totalTaskCount)
Expand Down Expand Up @@ -581,10 +581,17 @@ void HandleTaskCompletion()
[ExcludeFromCodeCoverage(Justification = "Not testable; WASM only.")]
Task OptionalDelayTask()
{
// WASM is single-threaded; give the UI a chance
return RuntimeInformation.ProcessArchitecture == Architecture.Wasm && Environment.ProcessorCount == 1
? Task.Delay(TimeSpan.FromMilliseconds(1), cancellationToken)
: Task.CompletedTask;
if (RuntimeInformation.ProcessArchitecture == Architecture.Wasm && Environment.ProcessorCount == 1
&& Stopwatch.GetElapsedTime(lastDelay) > TimeSpan.FromMilliseconds(50))
{
// On single threaded WASM we need to keep the UI responsive.
lastDelay = Stopwatch.GetTimestamp();
return Task.Delay(TimeSpan.FromMilliseconds(1), cancellationToken);
}
else
{
return Task.CompletedTask;
}
}

await OptionalDelayTask().ConfigureAwait(false);
Expand Down
Loading