Skip to content

Commit 87ae5c6

Browse files
committed
perf: reduce allocations across audio and network paths
1 parent bc5f302 commit 87ae5c6

31 files changed

Lines changed: 2825 additions & 324 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,3 +821,4 @@ $RECYCLE.BIN/
821821
!.vscode/extensions.json
822822
.temp/
823823
config/CrashLogs.json
824+
report.html

VoiceCraft.Client/VoiceCraft.Client.Android/AndroidBackgroundService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ public override void OnDestroy()
4747
{
4848
service.Dispose();
4949
}
50+
51+
Services.Clear();
5052
base.OnDestroy();
5153
}
5254

@@ -100,4 +102,4 @@ private void UpdateNotification()
100102
notification.SetSmallIcon(ResourceConstant.Drawable.Icon);
101103
notificationManager?.Notify(NotificationId, notification.Build());
102104
}
103-
}
105+
}

VoiceCraft.Client/VoiceCraft.Client.Android/NativeBackgroundService.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ public async Task StartServiceAsync<T>(Func<T, Action<string>, Action<string>, T
2828

2929
var backgroundTask = new BackgroundTask(instance);
3030
backgroundTask.OnCompleted += BackgroundTaskOnCompleted;
31-
AndroidBackgroundService.Services.TryAdd(backgroundType, backgroundTask);
31+
if (!AndroidBackgroundService.Services.TryAdd(backgroundType, backgroundTask))
32+
{
33+
backgroundTask.OnCompleted -= BackgroundTaskOnCompleted;
34+
backgroundTask.Dispose();
35+
throw new InvalidOperationException();
36+
}
37+
3238
try
3339
{
3440
await StartBackgroundService();
@@ -60,6 +66,10 @@ public void Dispose()
6066
var context = Application.Context;
6167
var intent = new Intent(context, typeof(AndroidBackgroundService));
6268
context.StopService(intent);
69+
foreach (var service in AndroidBackgroundService.Services.Values)
70+
service.Dispose();
71+
72+
AndroidBackgroundService.Services.Clear();
6373
GC.SuppressFinalize(this);
6474
}
6575

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include <stdarg.h>
2+
3+
#define OPUS_UNIMPLEMENTED -5
4+
5+
void __stack_chk_fail(void)
6+
{
7+
__builtin_trap();
8+
}
9+
10+
int opus_ms_decoder_ctl(void *st, int request, ...)
11+
{
12+
(void)st;
13+
(void)request;
14+
return OPUS_UNIMPLEMENTED;
15+
}
16+
17+
int opus_ms_encoder_ctl(void *st, int request, ...)
18+
{
19+
(void)st;
20+
(void)request;
21+
return OPUS_UNIMPLEMENTED;
22+
}

VoiceCraft.Client/VoiceCraft.Client.Browser/NativeBackgroundService.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@ public Task StartServiceAsync<T>(Func<T, Action<string>, Action<string>, Task> s
2020

2121
var backgroundTask = new BackgroundTask(instance);
2222
backgroundTask.OnCompleted += BackgroundTaskOnCompleted;
23+
if (!Services.TryAdd(backgroundType, backgroundTask))
24+
{
25+
backgroundTask.OnCompleted -= BackgroundTaskOnCompleted;
26+
backgroundTask.Dispose();
27+
throw new InvalidOperationException();
28+
}
29+
2330
try
2431
{
25-
Services.TryAdd(backgroundType, backgroundTask);
2632
backgroundTask.Start(() => startAction.Invoke(instance, _ => { }, _ => { }));
2733
}
2834
catch
@@ -48,6 +54,7 @@ public void Dispose()
4854
foreach (var service in Services.Values)
4955
service.Dispose();
5056

57+
Services.Clear();
5158
GC.SuppressFinalize(this);
5259
}
5360

VoiceCraft.Client/VoiceCraft.Client.Browser/VoiceCraft.Client.Browser.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<Nullable>enable</Nullable>
77
<WasmAllowUndefinedSymbols>true</WasmAllowUndefinedSymbols>
88
<WasmBuildNative>true</WasmBuildNative>
9+
<EmccVerbose>false</EmccVerbose>
10+
<!-- .NET 10.0.8's wasm-opt rejects a flag emitted during -O2 linking. -->
11+
<EmccLinkOptimizationFlag Condition="'$(Configuration)' == 'Release'">-O1</EmccLinkOptimizationFlag>
912
</PropertyGroup>
1013

1114
<ItemGroup>
@@ -18,6 +21,7 @@
1821
</ItemGroup>
1922

2023
<ItemGroup>
24+
<NativeFileReference Include="Native\wasm_stubs.c"/>
2125
<NativeFileReference Include="opus.a"/>
2226
</ItemGroup>
2327
</Project>

VoiceCraft.Client/VoiceCraft.Client.Linux/NativeBackgroundService.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ public Task StartServiceAsync<T>(Func<T, Action<string>, Action<string>, Task> s
2222

2323
var backgroundTask = new BackgroundTask(instance);
2424
backgroundTask.OnCompleted += BackgroundTaskOnCompleted;
25+
if (!Services.TryAdd(backgroundType, backgroundTask))
26+
{
27+
backgroundTask.OnCompleted -= BackgroundTaskOnCompleted;
28+
backgroundTask.Dispose();
29+
throw new InvalidOperationException();
30+
}
31+
2532
try
2633
{
27-
Services.TryAdd(backgroundType, backgroundTask);
2834
backgroundTask.Start(() => startAction.Invoke(instance, _ => { }, _ => { }));
2935
}
3036
catch
@@ -51,6 +57,8 @@ public void Dispose()
5157
{
5258
service.Dispose();
5359
}
60+
61+
Services.Clear();
5462
GC.SuppressFinalize(this);
5563
}
5664

VoiceCraft.Client/VoiceCraft.Client.MacOS/NativeBackgroundService.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ public Task StartServiceAsync<T>(Func<T, Action<string>, Action<string>, Task> s
2222

2323
var backgroundTask = new BackgroundTask(instance);
2424
backgroundTask.OnCompleted += BackgroundTaskOnCompleted;
25+
if (!Services.TryAdd(backgroundType, backgroundTask))
26+
{
27+
backgroundTask.OnCompleted -= BackgroundTaskOnCompleted;
28+
backgroundTask.Dispose();
29+
throw new InvalidOperationException();
30+
}
31+
2532
try
2633
{
27-
Services.TryAdd(backgroundType, backgroundTask);
2834
backgroundTask.Start(() => startAction.Invoke(instance, _ => { }, _ => { }));
2935
}
3036
catch
@@ -51,6 +57,8 @@ public void Dispose()
5157
{
5258
service.Dispose();
5359
}
60+
61+
Services.Clear();
5462
GC.SuppressFinalize(this);
5563
}
5664

VoiceCraft.Client/VoiceCraft.Client.Windows/NativeBackgroundService.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,15 @@ public Task StartServiceAsync<T>(Func<T, Action<string>, Action<string>, Task> s
2222

2323
var backgroundTask = new BackgroundTask(instance);
2424
backgroundTask.OnCompleted += BackgroundTaskOnCompleted;
25+
if (!Services.TryAdd(backgroundType, backgroundTask))
26+
{
27+
backgroundTask.OnCompleted -= BackgroundTaskOnCompleted;
28+
backgroundTask.Dispose();
29+
throw new InvalidOperationException();
30+
}
31+
2532
try
2633
{
27-
Services.TryAdd(backgroundType, backgroundTask);
2834
backgroundTask.Start(() => startAction.Invoke(instance, _ => { }, _ => { }));
2935
}
3036
catch
@@ -51,6 +57,8 @@ public void Dispose()
5157
{
5258
service.Dispose();
5359
}
60+
61+
Services.Clear();
5462
GC.SuppressFinalize(this);
5563
}
5664

VoiceCraft.Client/VoiceCraft.Client/Services/DiscordRpcService.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@ namespace VoiceCraft.Client.Services;
77

88
public class DiscordRpcService : IDisposable
99
{
10-
private readonly RichPresence _richPresence = new()
11-
{
12-
Buttons =
13-
[
14-
new Button
15-
{
16-
Label = Constants.GithubButton,
17-
Url = Constants.GithubButtonUrl
18-
}
19-
],
20-
Assets = new Assets
21-
{
22-
LargeImageKey = Constants.LargeImageKey,
23-
LargeImageText = Constants.LargeImageText
24-
}
25-
};
26-
10+
private readonly RichPresence? _richPresence;
2711
private readonly DiscordRpcClient? _rpcClient;
2812

2913
public DiscordRpcService()
3014
{
3115
if (OperatingSystem.IsBrowser() || OperatingSystem.IsAndroid() || OperatingSystem.IsIOS()) return;
3216

17+
_richPresence = new RichPresence
18+
{
19+
Buttons =
20+
[
21+
new Button
22+
{
23+
Label = Constants.GithubButton,
24+
Url = Constants.GithubButtonUrl
25+
}
26+
],
27+
Assets = new Assets
28+
{
29+
LargeImageKey = Constants.LargeImageKey,
30+
LargeImageText = Constants.LargeImageText
31+
}
32+
};
3333
_rpcClient = new DiscordRpcClient(Constants.ApplicationId);
3434

3535
#if DEBUG
@@ -58,7 +58,8 @@ public void Dispose()
5858

5959
public void SetState(string state)
6060
{
61+
if (_rpcClient == null || _richPresence == null) return;
6162
_richPresence.State = state;
62-
_rpcClient?.SetPresence(_richPresence);
63+
_rpcClient.SetPresence(_richPresence);
6364
}
64-
}
65+
}

0 commit comments

Comments
 (0)