| | 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 | | public sealed class LikeValidator : IValidator |
| | 22 | | { |
| | 23 | | /// <summary> |
| | 24 | | /// Gets the singleton instance of the <see cref="LikeValidator"/> class. |
| | 25 | | /// </summary> |
| 1 | 26 | | public static LikeValidator Instance = new(); |
| 2 | 27 | | private LikeValidator() { } |
| | 28 | |
|
| | 29 | | /// <inheritdoc/> |
| | 30 | | public bool IsValid<T>(T entity, Specification<T> specification) |
| | 31 | | { |
| 14 | 32 | | var compiledItems = specification.GetCompiledItems(); |
| 15 | 33 | | if (compiledItems.Length == 0) return true; |
| | 34 | |
|
| 30 | 35 | | var startIndexLikeItems = Array.FindIndex(compiledItems, item => item.Type == ItemType.Like); |
| 14 | 36 | | if (startIndexLikeItems == -1) return true; |
| | 37 | |
|
| | 38 | | // The like items are contiguously placed as a last segment in the array and are already sorted by group. |
| 12 | 39 | | return IsValid(entity, compiledItems.AsSpan()[startIndexLikeItems..compiledItems.Length]); |
| | 40 | | } |
| | 41 | |
|
| | 42 | | private static bool IsValid<T>(T entity, ReadOnlySpan<SpecItem> span) |
| | 43 | | { |
| 12 | 44 | | var groupStart = 0; |
| 52 | 45 | | for (var i = 1; i <= span.Length; i++) |
| | 46 | | { |
| | 47 | | // If we reached the end of the span or the group has changed, we slice and process the group. |
| 18 | 48 | | if (i == span.Length || span[i].Bag != span[groupStart].Bag) |
| | 49 | | { |
| 15 | 50 | | if (IsValidInOrGroup(entity, span[groupStart..i]) is false) |
| | 51 | | { |
| 4 | 52 | | return false; |
| | 53 | | } |
| 11 | 54 | | groupStart = i; |
| | 55 | | } |
| | 56 | | } |
| 8 | 57 | | return true; |
| | 58 | |
|
| | 59 | | static bool IsValidInOrGroup(T entity, ReadOnlySpan<SpecItem> span) |
| | 60 | | { |
| 15 | 61 | | var validOrGroup = false; |
| 51 | 62 | | foreach (var specItem in span) |
| | 63 | | { |
| 16 | 64 | | if (specItem.Reference is not SpecLikeCompiled<T> specLike) continue; |
| | 65 | |
|
| 16 | 66 | | if (specLike.KeySelector(entity)?.Like(specLike.Pattern) ?? false) |
| | 67 | | { |
| 11 | 68 | | validOrGroup = true; |
| 11 | 69 | | break; |
| | 70 | | } |
| | 71 | | } |
| 15 | 72 | | return validOrGroup; |
| | 73 | | } |
| | 74 | | } |
| | 75 | | } |