| | 1 | | using System.Buffers; |
| | 2 | |
|
| | 3 | | namespace Pozitron.QuerySpecification; |
| | 4 | |
|
| | 5 | | /* |
| | 6 | | public IQueryable<T> Evaluate<T>(IQueryable<T> source, Specification<T> specification) where T : class |
| | 7 | | { |
| | 8 | | foreach (var likeGroup in specification.LikeExpressions.GroupBy(x => x.Group)) |
| | 9 | | { |
| | 10 | | source = source.Like(likeGroup); |
| | 11 | | } |
| | 12 | | return source; |
| | 13 | | } |
| | 14 | | This was the previous implementation. We're trying to avoid allocations of LikeExpressions and GroupBy. |
| | 15 | | The new implementation preserves the behavior and has zero allocations. |
| | 16 | | Instead of GroupBy, we have a single array, sorted by group, and we slice it to get the groups. |
| | 17 | | */ |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Evaluates a specification to apply "like" expressions for filtering. |
| | 21 | | /// </summary> |
| | 22 | | public sealed class LikeEvaluator : IEvaluator |
| | 23 | | { |
| 2 | 24 | | private LikeEvaluator() { } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Gets the singleton instance of the <see cref="LikeEvaluator"/> class. |
| | 28 | | /// </summary> |
| 1 | 29 | | public static LikeEvaluator Instance = new(); |
| | 30 | |
|
| | 31 | | /// <inheritdoc/> |
| | 32 | | public IQueryable<T> Evaluate<T>(IQueryable<T> source, Specification<T> specification) where T : class |
| | 33 | | { |
| 53 | 34 | | var likeCount = GetLikeCount(specification); |
| 95 | 35 | | if (likeCount == 0) return source; |
| | 36 | |
|
| 11 | 37 | | if (likeCount == 1) |
| | 38 | | { |
| | 39 | | // Specs with a single Like are the most common. We can optimize for this case to avoid all the additional o |
| 2 | 40 | | var items = specification.Items; |
| 6 | 41 | | for (var i = 0; i < items.Length; i++) |
| | 42 | | { |
| 3 | 43 | | if (items[i].Type == ItemType.Like) |
| | 44 | | { |
| 2 | 45 | | return source.ApplyLikesAsOrGroup(items.Slice(i, 1)); |
| | 46 | | } |
| | 47 | | } |
| | 48 | | } |
| | 49 | |
|
| 9 | 50 | | SpecItem[] array = ArrayPool<SpecItem>.Shared.Rent(likeCount); |
| | 51 | |
|
| | 52 | | try |
| | 53 | | { |
| 9 | 54 | | var span = array.AsSpan()[..likeCount]; |
| 9 | 55 | | FillSorted(specification, span); |
| 9 | 56 | | source = ApplyLike(source, span); |
| 9 | 57 | | } |
| | 58 | | finally |
| | 59 | | { |
| 9 | 60 | | ArrayPool<SpecItem>.Shared.Return(array); |
| 9 | 61 | | } |
| | 62 | |
|
| 9 | 63 | | return source; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | private static IQueryable<T> ApplyLike<T>(IQueryable<T> source, ReadOnlySpan<SpecItem> span) where T : class |
| | 67 | | { |
| 9 | 68 | | var groupStart = 0; |
| 74 | 69 | | for (var i = 1; i <= span.Length; i++) |
| | 70 | | { |
| | 71 | | // If we reached the end of the span or the group has changed, we slice and process the group. |
| 28 | 72 | | if (i == span.Length || span[i].Bag != span[groupStart].Bag) |
| | 73 | | { |
| 19 | 74 | | source = source.ApplyLikesAsOrGroup(span[groupStart..i]); |
| 19 | 75 | | groupStart = i; |
| | 76 | | } |
| | 77 | | } |
| 9 | 78 | | return source; |
| | 79 | | } |
| | 80 | |
|
| | 81 | | private static int GetLikeCount<T>(Specification<T> specification) |
| | 82 | | { |
| 53 | 83 | | var count = 0; |
| 566 | 84 | | foreach (var item in specification.Items) |
| | 85 | | { |
| 230 | 86 | | if (item.Type == ItemType.Like) |
| 30 | 87 | | count++; |
| | 88 | | } |
| 53 | 89 | | return count; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | private static void FillSorted<T>(Specification<T> specification, Span<SpecItem> span) |
| | 93 | | { |
| 9 | 94 | | var i = 0; |
| 254 | 95 | | foreach (var item in specification.Items) |
| | 96 | | { |
| 118 | 97 | | if (item.Type == ItemType.Like) |
| | 98 | | { |
| | 99 | | // Find the correct insertion point |
| 28 | 100 | | var j = i; |
| 29 | 101 | | while (j > 0 && span[j - 1].Bag > item.Bag) |
| | 102 | | { |
| 1 | 103 | | span[j] = span[j - 1]; |
| 1 | 104 | | j--; |
| | 105 | | } |
| | 106 | |
|
| | 107 | | // Insert the current item in the sorted position |
| 28 | 108 | | span[j] = item; |
| 28 | 109 | | i++; |
| | 110 | | } |
| | 111 | | } |
| 9 | 112 | | } |
| | 113 | | } |