Optimize filter add
MODIFY
WalletWasabi/Services/WasabiSynchronizer.cs
WalletWasabi/Services/WasabiSynchronizer.cs
@@ -268,14 +268,9 @@ namespace WalletWasabi.Services
if (response.FiltersResponseState == FiltersResponseState.NewFilters)
{
- var filters = response.Filters; // performance
+ var filters = response.Filters.ToArray(); // performance
- foreach (var filter in filters)
- {
- await BitcoinStore.IndexStore.AddNewFilterAsync(filter, toFile: false);
- }
-
- await BitcoinStore.IndexStore.ToFileAsync();
+ await BitcoinStore.IndexStore.AddNewFiltersAsync(filters);
if (filters.Count() == 1)
{
@@ -290,7 +285,7 @@ namespace WalletWasabi.Services
{
// Reorg happened
// 1. Rollback index
- FilterModel reorgedFilter = await BitcoinStore.IndexStore.RemoveLastFilterAsync(toFile: true);
+ FilterModel reorgedFilter = await BitcoinStore.IndexStore.RemoveLastFilterAsync();
Logger.LogInfo<WasabiSynchronizer>($"REORG Invalid Block: {reorgedFilter.BlockHash}");
ignoreRequestInterval = true;

MODIFY
WalletWasabi/Stores/IndexStore.cs
WalletWasabi/Stores/IndexStore.cs
@@ -97,27 +97,37 @@ namespace WalletWasabi.Stores
}
}
- public async Task AddNewFilterAsync(FilterModel filter, bool toFile)
+ public async Task AddNewFiltersAsync(params FilterModel[] filters)
+ {
+ foreach (var filter in filters)
{
try
{
await Semaphore.WaitAsync();
Index.Add(filter);
- if (toFile)
+ }
+ finally
{
- await ToFileNoSemaphoreAsync();
+ Semaphore.Release();
}
+
+ NewFilter?.Invoke(this, filter); // Event always outside the lock.
+ }
+
+ try
+ {
+ await Semaphore.WaitAsync();
+
+ await ToFileNoSemaphoreAsync();
}
finally
{
Semaphore.Release();
}
-
- NewFilter?.Invoke(this, filter);
}
- public async Task<FilterModel> RemoveLastFilterAsync(bool toFile)
+ public async Task<FilterModel> RemoveLastFilterAsync()
{
FilterModel filter = null;
try
@@ -126,11 +136,8 @@ namespace WalletWasabi.Stores
filter = Index.Last();
Index.RemoveLast();
- if (toFile)
- {
await ToFileNoSemaphoreAsync();
}
- }
finally
{
Semaphore.Release();
@@ -149,20 +156,6 @@ namespace WalletWasabi.Stores
});
}
- public async Task ToFileAsync()
- {
- try
- {
- await Semaphore.WaitAsync();
-
- await ToFileNoSemaphoreAsync();
- }
- finally
- {
- Semaphore.Release();
- }
- }
-
public async Task<FilterModel> GetBestKnownFilterAsync()
{
FilterModel ret = null;

Optimize filter add