Skip to content

Commit 24a6938

Browse files
committed
优化first代码
1 parent 4ac8601 commit 24a6938

File tree

9 files changed

+116
-84
lines changed

9 files changed

+116
-84
lines changed

src/ShardingCore/Core/Internal/FixedElementCollection.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ public FixedElementCollection(int capacity)
1818
public int VirtualElementCount => _virtualElementCount;
1919
public int ReallyElementCount => _list.Count;
2020

21-
public void Add(TEntity element)
21+
public bool Add(TEntity element)
2222
{
2323
_virtualElementCount++;
2424
if (VirtualElementCount <= _capacity)
2525
{
2626
_list.Add(element);
27+
return true;
2728
}
29+
return false;
2830
}
2931

3032
public TEntity FirstOrDefault()

src/ShardingCore/Extensions/StreamMergeEnumerableExtension.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,44 @@ public static List<TEntity> ToStreamList<TEntity>(this IEnumerable<TEntity> sour
2626
{
2727
return source.ToList();
2828
}
29-
public static FixedElementCollection<TEntity> ToFixedElementStreamList<TEntity>(this IEnumerable<TEntity> source,int capacity)
29+
public static FixedElementCollection<TEntity> ToFixedElementStreamList<TEntity>(this IEnumerable<TEntity> source,int capacity,int maxVirtualElementCount)
3030
{
3131
var fixedElementCollection = new FixedElementCollection<TEntity>(capacity);
32-
var enumerator = source.GetEnumerator();
32+
using var enumerator = source.GetEnumerator();
3333
while (enumerator.MoveNext())
3434
{
3535
fixedElementCollection.Add(enumerator.Current);
36+
if (fixedElementCollection.VirtualElementCount >= maxVirtualElementCount)
37+
{
38+
break;
39+
}
3640
}
3741
return fixedElementCollection;
3842
}
3943

4044

41-
public static async Task<FixedElementCollection<TEntity>> ToFixedElementStreamListAsync<TEntity>(this IAsyncEnumerable<TEntity> source,int capacity,CancellationToken cancellationToken=default)
45+
public static async Task<FixedElementCollection<TEntity>> ToFixedElementStreamListAsync<TEntity>(this IAsyncEnumerable<TEntity> source,int capacity,int maxVirtualElementCount,CancellationToken cancellationToken=default)
4246
{
4347
var fixedElementCollection = new FixedElementCollection<TEntity>(capacity);
4448
#if EFCORE2
4549
var asyncEnumerator = source.GetEnumerator();
4650
while (await asyncEnumerator.MoveNext(cancellationToken))
4751
{
4852
fixedElementCollection.Add(asyncEnumerator.Current);
53+
if (fixedElementCollection.VirtualElementCount >= maxVirtualElementCount)
54+
{
55+
break;
56+
}
4957
}
5058
#endif
5159
#if !EFCORE2
5260
await foreach (var element in source.WithCancellation(cancellationToken))
5361
{
5462
fixedElementCollection.Add(element);
63+
if (fixedElementCollection.VirtualElementCount >= maxVirtualElementCount)
64+
{
65+
break;
66+
}
5567
}
5668
#endif
5769
return fixedElementCollection;

src/ShardingCore/Sharding/Enumerators/StreamMergeAsync/OneAtMostElementStreamMergeAsyncEnumerator.cs

Lines changed: 20 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,86 +8,72 @@ namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync
88
{
99
internal class OneAtMostElementStreamMergeAsyncEnumerator<T> : IStreamMergeAsyncEnumerator<T>
1010
{
11-
private List<T>.Enumerator _enumerator;
12-
private bool skip;
11+
private int _moveIndex = 0;
12+
private T _constantElement;
1313

1414
public OneAtMostElementStreamMergeAsyncEnumerator(IStreamMergeAsyncEnumerator<T> streamMergeAsyncEnumerator)
1515
{
16-
var list = new List<T>();
17-
if (streamMergeAsyncEnumerator.HasElement())
16+
_constantElement=streamMergeAsyncEnumerator.ReallyCurrent;
17+
}
18+
19+
20+
private bool MoveNext0()
21+
{
22+
if (_moveIndex >= 1)
1823
{
19-
list.Add(streamMergeAsyncEnumerator.ReallyCurrent);
24+
return false;
2025
}
2126

22-
_enumerator = list.GetEnumerator();
23-
_enumerator.MoveNext();
24-
skip = true;
27+
_moveIndex++;
28+
return HasElement();
2529
}
26-
2730
#if !EFCORE2&&!EFCORE3&&!EFCORE5
2831
public ValueTask DisposeAsync()
2932
{
30-
_enumerator.Dispose();
3133
return ValueTask.CompletedTask;
3234
}
33-
3435
public ValueTask<bool> MoveNextAsync()
3536
{
36-
var moveNext = _enumerator.MoveNext();
37+
var moveNext = MoveNext0();
3738
return ValueTask.FromResult<bool>(moveNext);
3839
}
3940

4041
public void Dispose()
4142
{
42-
_enumerator.Dispose();
4343
}
4444

4545
#endif
4646
#if EFCORE3 || EFCORE5
4747
public ValueTask DisposeAsync()
4848
{
49-
_enumerator.Dispose();
5049
return new ValueTask();
5150
}
5251

5352
public ValueTask<bool> MoveNextAsync()
5453
{
55-
var moveNext = _enumerator.MoveNext();
54+
var moveNext = MoveNext0();
5655
return new ValueTask<bool>(moveNext);
5756
}
5857

5958
public void Dispose()
6059
{
61-
_enumerator.Dispose();
6260
}
6361

6462
#endif
6563
public bool MoveNext()
6664
{
67-
if (skip)
68-
{
69-
skip = false;
70-
return null != _enumerator.Current;
71-
}
72-
73-
var moveNext = _enumerator.MoveNext();
65+
var moveNext = MoveNext0();
7466
return moveNext;
7567
}
7668

7769
public bool SkipFirst()
7870
{
79-
if (skip)
80-
{
81-
skip = false;
82-
return true;
83-
}
84-
8571
return false;
8672
}
8773

8874
public bool HasElement()
8975
{
90-
return null != _enumerator.Current;
76+
return null != _constantElement;
9177
}
9278

9379

@@ -102,29 +88,23 @@ public void Reset()
10288

10389
public T GetCurrent()
10490
{
105-
if (skip)
91+
if (_moveIndex==0)
10692
return default;
107-
return _enumerator.Current;
93+
return _constantElement;
10894
}
10995

11096
public T GetReallyCurrent()
11197
{
112-
return _enumerator.Current;
98+
return _constantElement;
11399
}
114100
#if EFCORE2
115101
public void Dispose()
116102
{
117-
_enumerator.Dispose();
118103
}
119104

120105
public Task<bool> MoveNext(CancellationToken cancellationToken = new CancellationToken())
121106
{
122-
if (skip)
123-
{
124-
skip = false;
125-
return Task.FromResult(null != _enumerator.Current);
126-
}
127-
var moveNext = _enumerator.MoveNext();
107+
var moveNext = MoveNext0();
128108
return Task.FromResult(moveNext);
129109
}
130110

src/ShardingCore/Sharding/MergeEngines/Executors/Abstractions/AbstractExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ private async Task<LinkedList<TResult>> ExecuteAsync0(bool async,
9090
if (dataSourceSqlExecutorUnit.ConnectionMode == ConnectionModeEnum.CONNECTION_STRICTLY)
9191
{
9292
MergeParallelExecuteResult(result, routeQueryResults.Select(o => o.MergeResult), async);
93-
var dbContexts = routeQueryResults.Select(o => o.DbContext);
93+
var dbContexts = routeQueryResults.Where(o=>o!=null).Select(o => o.DbContext).ToArray();
9494
foreach (var dbContext in dbContexts)
9595
{
9696
await streamMergeContext.DbContextDisposeAsync(dbContext);

0 commit comments

Comments
 (0)