Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b667109

Browse files
committed
minor optimizations
1 parent cc41b61 commit b667109

File tree

8 files changed

+100
-84
lines changed

8 files changed

+100
-84
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
2+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EdotCover_002EIde_002ECore_002EFilterManagement_002EModel_002ESolutionFilterSettingsManagerMigrateSettings/@EntryIndexedValue">True</s:Boolean>
23
<s:String x:Key="/Default/FilterSettingsManager/CoverageFilterXml/@EntryValue">&lt;data&gt;&lt;IncludeFilters /&gt;&lt;ExcludeFilters /&gt;&lt;/data&gt;</s:String>
34
<s:String x:Key="/Default/FilterSettingsManager/AttributeFilterXml/@EntryValue">&lt;data /&gt;</s:String></wpf:ResourceDictionary>

src/ServiceStack.Redis/Generic/RedisTypedClient.Async.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -106,49 +106,47 @@ async Task IEntityStoreAsync<T>.DeleteAsync(T entity, CancellationToken token)
106106
{
107107
var urnKey = client.UrnKey(entity);
108108
await AsyncClient.RemoveEntryAsync(new[] { urnKey }, token).ConfigureAwait(false);
109-
await client.RemoveTypeIdsAsync(new[] { entity }, token).ConfigureAwait(false);
109+
await client.RemoveTypeIdsByValueAsync(entity, token).ConfigureAwait(false);
110110
}
111111

112112
async Task IEntityStoreAsync<T>.DeleteByIdAsync(object id, CancellationToken token)
113113
{
114114
var urnKey = client.UrnKey<T>(id);
115115

116116
await AsyncClient.RemoveEntryAsync(new[] { urnKey }, token).ConfigureAwait(false);
117-
await client.RemoveTypeIdsAsync<T>(new[] { id.ToString() }, token).ConfigureAwait(false);
117+
await client.RemoveTypeIdsByIdAsync<T>(id.ToString(), token).ConfigureAwait(false);
118118
}
119119

120120
async Task IEntityStoreAsync<T>.DeleteByIdsAsync(IEnumerable ids, CancellationToken token)
121121
{
122122
if (ids == null) return;
123123

124-
var urnKeys = ids.Map(t => client.UrnKey<T>(t));
125-
if (urnKeys.Count > 0)
124+
var idStrings = ids.Cast<object>().Select(x => x.ToString()).ToArray();
125+
var urnKeys = idStrings.Select(t => client.UrnKey<T>(t)).ToArray();
126+
if (urnKeys.Length > 0)
126127
{
127-
await AsyncClient.RemoveEntryAsync(urnKeys.ToArray(), token).ConfigureAwait(false);
128-
await client.RemoveTypeIdsAsync<T>(ids.Map(x => x.ToString()).ToArray(), token).ConfigureAwait(false);
128+
await AsyncClient.RemoveEntryAsync(urnKeys, token).ConfigureAwait(false);
129+
await client.RemoveTypeIdsByIdsAsync<T>(idStrings, token).ConfigureAwait(false);
129130
}
130131
}
131132

132133
async Task IEntityStoreAsync<T>.DeleteAllAsync(CancellationToken token)
133134
{
134-
await DeleteAllAsync(0,RedisConfig.DeleteAllBatchSize, token).ConfigureAwait(false);
135+
await DeleteAllAsync(0,RedisConfig.CommandKeysBatchSize, token).ConfigureAwait(false);
135136
}
136137

137-
private async Task DeleteAllAsync(ulong cursor, int pageSize, CancellationToken token)
138+
private async Task DeleteAllAsync(ulong cursor, int batchSize, CancellationToken token)
138139
{
139-
var callCount = 0;
140-
while (cursor != 0 || callCount == 0)
140+
do
141141
{
142-
var scanResult = await AsyncNative.SScanAsync(this.TypeIdsSetKey, cursor, pageSize, token: token).ConfigureAwait(false);
143-
callCount++;
142+
var scanResult = await AsyncNative.SScanAsync(this.TypeIdsSetKey, cursor, batchSize, token: token).ConfigureAwait(false);
144143
cursor = scanResult.Cursor;
145-
var ids = scanResult.Results.Select(x => Encoding.UTF8.GetString(x)).ToList();
146-
var urnKeys = ids.Map(t => client.UrnKey<T>(t));
147-
if (urnKeys.Count > 0)
144+
var urnKeys = scanResult.Results.Select(x => client.UrnKey<T>(Encoding.UTF8.GetString(x))).ToArray();
145+
if (urnKeys.Length > 0)
148146
{
149-
await AsyncClient.RemoveEntryAsync(urnKeys.ToArray(), token).ConfigureAwait(false);
147+
await AsyncClient.RemoveEntryAsync(urnKeys, token).ConfigureAwait(false);
150148
}
151-
}
149+
} while (cursor != 0);
152150
await AsyncClient.RemoveEntryAsync(new[] { this.TypeIdsSetKey }, token).ConfigureAwait(false);
153151
}
154152

@@ -251,9 +249,9 @@ ValueTask<bool> IRedisTypedClientAsync<T>.RemoveEntryAsync(string[] keys, Cancel
251249

252250
async ValueTask<bool> IRedisTypedClientAsync<T>.RemoveEntryAsync(IHasStringId[] entities, CancellationToken token)
253251
{
254-
var ids = entities.Map(x => x.Id);
255-
var success = await AsyncNative.DelAsync(ids.ToArray(), token).IsSuccessAsync().ConfigureAwait(false);
256-
if (success) await client.RemoveTypeIdsAsync(ids.ToArray(), token).ConfigureAwait(false);
252+
var ids = entities.Select(x => x.Id).ToArray();
253+
var success = await AsyncNative.DelAsync(ids, token).IsSuccessAsync().ConfigureAwait(false);
254+
if (success) await client.RemoveTypeIdsByValuesAsync(ids, token).ConfigureAwait(false);
257255
return success;
258256
}
259257

src/ServiceStack.Redis/Generic/RedisTypedClient.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public bool RemoveEntry(params IHasStringId[] entities)
220220
{
221221
var ids = entities.Map(x => x.Id);
222222
var success = client.Del(ids.ToArray()) == RedisNativeClient.Success;
223-
if (success) client.RemoveTypeIds(ids.ToArray());
223+
if (success) client.RemoveTypeIdsByValues(ids.ToArray());
224224
return success;
225225
}
226226

@@ -443,51 +443,50 @@ public void Delete(T entity)
443443
{
444444
var urnKey = client.UrnKey(entity);
445445
this.RemoveEntry(urnKey);
446-
client.RemoveTypeIds(entity);
446+
client.RemoveTypeIdsByValue(entity);
447447
}
448448

449449
public void DeleteById(object id)
450450
{
451451
var urnKey = client.UrnKey<T>(id);
452452

453453
this.RemoveEntry(urnKey);
454-
client.RemoveTypeIds<T>(id.ToString());
454+
client.RemoveTypeIdsById<T>(id.ToString());
455455
}
456456

457457
public void DeleteByIds(IEnumerable ids)
458458
{
459459
if (ids == null) return;
460460

461-
var urnKeys = ids.Map(t => client.UrnKey<T>(t));
462-
if (urnKeys.Count > 0)
461+
var idStrings = ids.Map(x => x.ToString()).ToArray();
462+
var urnKeys = idStrings.Select(t => client.UrnKey<T>(t)).ToArray();
463+
if (urnKeys.Length > 0)
463464
{
464-
this.RemoveEntry(urnKeys.ToArray());
465-
client.RemoveTypeIds<T>(ids.Map(x => x.ToString()).ToArray());
465+
this.RemoveEntry(urnKeys);
466+
client.RemoveTypeIdsByIds<T>(idStrings);
466467
}
467468
}
468469

469470
private void DeleteAll(ulong cursor, int pageSize)
470471
{
471-
var callCount = 0;
472-
while (cursor != 0 || callCount == 0)
472+
do
473473
{
474474
var scanResult = client.SScan(this.TypeIdsSetKey, cursor, pageSize);
475-
callCount++;
476475
cursor = scanResult.Cursor;
477476
var ids = scanResult.Results.Select(x => Encoding.UTF8.GetString(x)).ToList();
478477
var urnKeys = ids.Map(t => client.UrnKey<T>(t));
479478
if (urnKeys.Count > 0)
480479
{
481480
this.RemoveEntry(urnKeys.ToArray());
482481
}
483-
}
482+
} while (cursor != 0);
484483

485484
this.RemoveEntry(this.TypeIdsSetKey);
486485
}
487486

488487
public void DeleteAll()
489488
{
490-
DeleteAll(0,RedisConfig.DeleteAllBatchSize);
489+
DeleteAll(0,RedisConfig.CommandKeysBatchSize);
491490
}
492491

493492
#endregion

src/ServiceStack.Redis/RedisClient.Async.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,9 @@ internal ValueTask RegisterTypeIdsAsync<T>(IEnumerable<T> values, CancellationTo
599599
}
600600
}
601601

602-
internal async ValueTask RemoveTypeIdsAsync<T>(T[] values, CancellationToken token)
602+
internal ValueTask RemoveTypeIdsByValueAsync<T>(T value, CancellationToken token) =>
603+
RemoveTypeIdsByIdAsync<T>(value.GetId().ToString(), token);
604+
internal async ValueTask RemoveTypeIdsByValuesAsync<T>(T[] values, CancellationToken token)
603605
{
604606
var typeIdsSetKey = GetTypeIdsSetKey<T>();
605607
if (this.Pipeline != null)
@@ -616,7 +618,18 @@ internal async ValueTask RemoveTypeIdsAsync<T>(T[] values, CancellationToken tok
616618
}
617619
}
618620

619-
internal async ValueTask RemoveTypeIdsAsync<T>(string[] ids, CancellationToken token)
621+
internal async ValueTask RemoveTypeIdsByIdAsync<T>(string id, CancellationToken token)
622+
{
623+
var typeIdsSetKey = GetTypeIdsSetKey<T>();
624+
if (this.Pipeline != null)
625+
GetRegisteredTypeIdsWithinPipeline(typeIdsSetKey).Remove(id);
626+
else
627+
{
628+
await AsAsync().RemoveItemFromSetAsync(typeIdsSetKey, id, token).ConfigureAwait(false);
629+
}
630+
}
631+
632+
internal async ValueTask RemoveTypeIdsByIdsAsync<T>(string[] ids, CancellationToken token)
620633
{
621634
var typeIdsSetKey = GetTypeIdsSetKey<T>();
622635
if (this.Pipeline != null)
@@ -637,48 +650,46 @@ async Task IEntityStoreAsync.DeleteAsync<T>(T entity, CancellationToken token)
637650
{
638651
var urnKey = UrnKey(entity);
639652
await AsAsync().RemoveAsync(urnKey, token).ConfigureAwait(false);
640-
await this.RemoveTypeIdsAsync(new[] { entity }, token).ConfigureAwait(false);
653+
await this.RemoveTypeIdsByValueAsync(entity, token).ConfigureAwait(false);
641654
}
642655

643656
async Task IEntityStoreAsync.DeleteByIdAsync<T>(object id, CancellationToken token)
644657
{
645658
var urnKey = UrnKey<T>(id);
646659
await AsAsync().RemoveAsync(urnKey, token).ConfigureAwait(false);
647-
await this.RemoveTypeIdsAsync<T>(new[] { id.ToString() }, token).ConfigureAwait(false);
660+
await this.RemoveTypeIdsByIdAsync<T>(id.ToString(), token).ConfigureAwait(false);
648661
}
649662

650663
async Task IEntityStoreAsync.DeleteByIdsAsync<T>(ICollection ids, CancellationToken token)
651664
{
652665
if (ids == null || ids.Count == 0) return;
653666

654-
var idsList = ids.Cast<object>().ToList();
655-
var urnKeys = idsList.Map(UrnKey<T>);
656-
await AsAsync().RemoveEntryAsync(urnKeys.ToArray(), token).ConfigureAwait(false);
657-
await this.RemoveTypeIdsAsync<T>(idsList.Map(x => x.ToString()).ToArray(), token).ConfigureAwait(false);
667+
var idStrings = ids.Cast<object>().Select(x => x.ToString()).ToArray();
668+
var urnKeys = idStrings.Select(UrnKey<T>).ToArray();
669+
await AsAsync().RemoveEntryAsync(urnKeys, token).ConfigureAwait(false);
670+
await this.RemoveTypeIdsByIdsAsync<T>(idStrings, token).ConfigureAwait(false);
658671
}
659672

660673
async Task IEntityStoreAsync.DeleteAllAsync<T>(CancellationToken token)
661674
{
662-
await DeleteAllAsync<T>(0, RedisConfig.DeleteAllBatchSize, token).ConfigureAwait(false);
675+
await DeleteAllAsync<T>(0, RedisConfig.CommandKeysBatchSize, token).ConfigureAwait(false);
663676
}
664677

665-
private async Task DeleteAllAsync<T>(ulong cursor, int pageSize, CancellationToken token)
678+
private async Task DeleteAllAsync<T>(ulong cursor, int batchSize, CancellationToken token)
666679
{
667680
var typeIdsSetKey = this.GetTypeIdsSetKey<T>();
668-
var callCount = 0;
669-
while (cursor != 0 || callCount == 0)
681+
var asyncClient = AsAsync();
682+
do
670683
{
671-
var scanResult = await NativeAsync.SScanAsync(typeIdsSetKey, cursor, pageSize, token: token).ConfigureAwait(false);
672-
callCount++;
684+
var scanResult = await NativeAsync.SScanAsync(typeIdsSetKey, cursor, batchSize, token: token).ConfigureAwait(false);
673685
cursor = scanResult.Cursor;
674-
var ids = scanResult.Results.Select(x => x.FromUtf8Bytes());
675-
var urnKeys = ids.Map(t => AsAsync().UrnKey<T>(t));
676-
if (urnKeys.Count > 0)
686+
var urnKeys = scanResult.Results.Select(id => UrnKey<T>(id.FromUtf8Bytes())).ToArray();
687+
if (urnKeys.Length > 0)
677688
{
678-
await AsAsync().RemoveEntryAsync(urnKeys.ToArray(), token).ConfigureAwait(false);
689+
await asyncClient.RemoveEntryAsync(urnKeys, token).ConfigureAwait(false);
679690
}
680-
}
681-
await AsAsync().RemoveEntryAsync(new[] { typeIdsSetKey }, token).ConfigureAwait(false);
691+
} while (cursor != 0);
692+
await asyncClient.RemoveEntryAsync(new[] { typeIdsSetKey }, token).ConfigureAwait(false);
682693
}
683694

684695
ValueTask<List<string>> IRedisClientAsync.SearchSortedSetAsync(string setId, string start, string end, int? skip, int? take, CancellationToken token)

src/ServiceStack.Redis/RedisClient.cs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,16 @@ internal void RegisterTypeIds<T>(IEnumerable<T> values)
614614
}
615615
}
616616

617-
internal void RemoveTypeIds<T>(params string[] ids)
617+
internal void RemoveTypeIdsById<T>(string id)
618+
{
619+
var typeIdsSetKey = GetTypeIdsSetKey<T>();
620+
if (this.Pipeline != null)
621+
GetRegisteredTypeIdsWithinPipeline(typeIdsSetKey).Remove(id);
622+
else
623+
this.RemoveItemFromSet(typeIdsSetKey, id);
624+
}
625+
626+
internal void RemoveTypeIdsByIds<T>(IEnumerable<string> ids)
618627
{
619628
var typeIdsSetKey = GetTypeIdsSetKey<T>();
620629
if (this.Pipeline != null)
@@ -628,7 +637,9 @@ internal void RemoveTypeIds<T>(params string[] ids)
628637
}
629638
}
630639

631-
internal void RemoveTypeIds<T>(params T[] values)
640+
internal void RemoveTypeIdsByValue<T>(T value) => RemoveTypeIdsById<T>(value.GetId().ToString());
641+
642+
internal void RemoveTypeIdsByValues<T>(IEnumerable<T> values)
632643
{
633644
var typeIdsSetKey = GetTypeIdsSetKey<T>();
634645
if (this.Pipeline != null)
@@ -809,59 +820,52 @@ public void Delete<T>(T entity)
809820
{
810821
var urnKey = UrnKey(entity);
811822
this.Remove(urnKey);
812-
this.RemoveTypeIds(entity);
823+
this.RemoveTypeIdsByValue(entity);
813824
}
814825

815826
public void DeleteById<T>(object id)
816827
{
817828
var urnKey = UrnKey<T>(id);
818829
this.Remove(urnKey);
819-
this.RemoveTypeIds<T>(id.ToString());
830+
this.RemoveTypeIdsById<T>(id.ToString());
820831
}
821832

822833
public void DeleteByIds<T>(ICollection ids)
823834
{
824835
if (ids == null || ids.Count == 0) return;
825836

826837
var idsList = ids.Cast<object>();
827-
var urnKeys = idsList.Map(UrnKey<T>);
828-
this.RemoveEntry(urnKeys.ToArray());
829-
this.RemoveTypeIds<T>(idsList.Map(x => x.ToString()).ToArray());
838+
var urnKeys = idsList.Select(UrnKey<T>).ToArray();
839+
this.RemoveEntry(urnKeys);
840+
this.RemoveTypeIdsByIds<T>(ids.Map(x => x.ToString()).ToArray());
830841
}
831842

832843
public void DeleteAll<T>()
833844
{
834-
DeleteAll<T>(0,RedisConfig.DeleteAllBatchSize);
845+
DeleteAll<T>(0,RedisConfig.CommandKeysBatchSize);
835846
}
836847

837-
private void DeleteAll<T>(ulong cursor, int pageSize)
848+
private void DeleteAll<T>(ulong cursor, int batchSize)
838849
{
839850
var typeIdsSetKey = this.GetTypeIdsSetKey<T>();
840-
var callCount = 0;
841-
while (cursor != 0 || callCount == 0)
851+
do
842852
{
843-
var scanResult = this.SScan(typeIdsSetKey, cursor, pageSize);
844-
callCount++;
853+
var scanResult = this.SScan(typeIdsSetKey, cursor, batchSize);
845854
cursor = scanResult.Cursor;
846-
var ids = scanResult.Results.Select(x => x.FromUtf8Bytes());
847-
var urnKeys = ids.Map(t => this.UrnKey(t));
848-
if (urnKeys.Count > 0)
855+
var urnKeys = scanResult.Results.Select(id => UrnKey<T>(id.FromUtf8Bytes())).ToArray();
856+
if (urnKeys.Length > 0)
849857
{
850-
this.RemoveEntry(urnKeys.ToArray());
858+
this.RemoveEntry(urnKeys);
851859
}
852-
}
860+
} while (cursor != 0);
853861

854862
this.RemoveEntry(typeIdsSetKey);
855863
}
856864

857-
public RedisClient CloneClient()
858-
{
859-
return new RedisClient(Host, Port, Password, Db)
860-
{
861-
SendTimeout = SendTimeout,
862-
ReceiveTimeout = ReceiveTimeout
863-
};
864-
}
865+
public RedisClient CloneClient() => new(Host, Port, Password, Db) {
866+
SendTimeout = SendTimeout,
867+
ReceiveTimeout = ReceiveTimeout
868+
};
865869

866870
/// <summary>
867871
/// Returns key with automatic object id detection in provided value with <typeparam name="T">generic type</typeparam>.
@@ -899,8 +903,7 @@ public string UrnKey(Type type, object id)
899903

900904
#region LUA EVAL
901905

902-
static readonly ConcurrentDictionary<string, string> CachedLuaSha1Map =
903-
new ConcurrentDictionary<string, string>();
906+
static readonly ConcurrentDictionary<string, string> CachedLuaSha1Map = new();
904907

905908
public T ExecCachedLua<T>(string scriptBody, Func<string, T> scriptSha1)
906909
{

src/ServiceStack.Redis/RedisConfig.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ public class RedisConfig
7373
public static int BufferPoolMaxSize = 500000;
7474

7575
/// <summary>
76-
/// The DeleteAll Batch Size is the number of keys returned each SSCAN when using DeleteAll on the RedisTypedClient.
76+
/// Batch size of keys to include in a single Redis Command (e.g. DEL k1 k2...)
7777
/// </summary>
78-
public static int DeleteAllBatchSize = 1000;
78+
public static int CommandKeysBatchSize = 10000;
7979

8080
/// <summary>
8181
/// Whether Connections to Master hosts should be verified they're still master instances (default true)
@@ -141,6 +141,7 @@ public static void Reset()
141141
DefaultMaxPoolSize = null;
142142
BackOffMultiplier = 10;
143143
BufferPoolMaxSize = 500000;
144+
CommandKeysBatchSize = 10000;
144145
VerifyMasterConnections = true;
145146
RetryReconnectOnFailedMasters = true;
146147
HostLookupTimeoutMs = 200;

0 commit comments

Comments
 (0)