| | 1 | | namespace Pozitron.QuerySpecification; |
| | 2 | |
|
| | 3 | | /* |
| | 4 | | public bool IsValid<T>(T entity, Specification<T> specification) |
| | 5 | | { |
| | 6 | | foreach (var likeGroup in specification.LikeExpressions.GroupBy(x => x.Group)) |
| | 7 | | { |
| | 8 | | if (likeGroup.Any(c => c.KeySelectorFunc(entity)?.Like(c.Pattern) ?? false) == false) return false; |
| | 9 | | } |
| | 10 | | return true; |
| | 11 | | } |
| | 12 | | This was the previous implementation.We're trying to avoid allocations of LikeExpressions, GroupBy and LINQ. |
| | 13 | | Instead of GroupBy, we have a single array sorted by group, and we slice it to get the groups. |
| | 14 | | The new implementation preserves the behavior and reduces allocations drastically. |
| | 15 | | For 1000 entities, the allocations are reduced from 651.160 bytes to ZERO bytes. Refer to LikeValidatorBenchmark res |
| | 16 | | */ |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Represents a validator for "like" expressions. |
| | 20 | | /// </summary> |
| | 21 | | [ValidatorDiscovery(Order = 20)] |
| | 22 | | public sealed class LikeValidator : IValidator |
| | 23 | | { |
| | 24 | | /// <summary> |
| | 25 | | /// Gets the singleton instance of the <see cref="LikeValidator"/> class. |
| | 26 | | /// </summary> |
| 2 | 27 | | public static LikeValidator Instance = new(); |
| 4 | 28 | | private LikeValidator() { } |
| | 29 | |
|
| | 30 | | /// <inheritdoc/> |
| | 31 | | public bool IsValid<T>(T entity, Specification<T> specification) |
| | 32 | | { |
| 14 | 33 | | var compiledItems = specification.GetCompiledItems(); |
| 15 | 34 | | if (compiledItems.Length == 0) return true; |
| | 35 | |
|
| 30 | 36 | | var startIndexLikeItems = Array.FindIndex(compiledItems, item => item.Type == ItemType.Like); |
| 14 | 37 | | if (startIndexLikeItems == -1) return true; |
| | 38 | |
|
| | 39 | | // The like items are contiguously placed as a last segment in the array and are already sorted by group. |
| 12 | 40 | | return IsValid(entity, compiledItems.AsSpan()[startIndexLikeItems..compiledItems.Length]); |
| | 41 | | } |
| | 42 | |
|
| | 43 | | private static bool IsValid<T>(T entity, ReadOnlySpan<SpecItem> span) |
| | 44 | | { |
| 12 | 45 | | var groupStart = 0; |
| 52 | 46 | | for (var i = 1; i <= span.Length; i++) |
| | 47 | | { |
| | 48 | | // If we reached the end of the span or the group has changed, we slice and process the group. |
| 18 | 49 | | if (i == span.Length || span[i].Bag != span[groupStart].Bag) |
| | 50 | | { |
| 15 | 51 | | if (IsValidInOrGroup(entity, span[groupStart..i]) is false) |
| | 52 | | { |
| 4 | 53 | | return false; |
| | 54 | | } |
| 11 | 55 | | groupStart = i; |
| | 56 | | } |
| | 57 | | } |
| 8 | 58 | | return true; |
| | 59 | |
|
| | 60 | | static bool IsValidInOrGroup(T entity, ReadOnlySpan<SpecItem> span) |
| | 61 | | { |
| 15 | 62 | | var validOrGroup = false; |
| 51 | 63 | | foreach (var specItem in span) |
| | 64 | | { |
| 16 | 65 | | if (specItem.Reference is not SpecLikeCompiled<T> specLike) continue; |
| | 66 | |
|
| 16 | 67 | | if (specLike.KeySelector(entity)?.Like(specLike.Pattern) ?? false) |
| | 68 | | { |
| 11 | 69 | | validOrGroup = true; |
| 11 | 70 | | break; |
| | 71 | | } |
| | 72 | | } |
| 15 | 73 | | return validOrGroup; |
| | 74 | | } |
| | 75 | | } |
| | 76 | | } |