File tree Expand file tree Collapse file tree 2 files changed +70
-18
lines changed
src/Microsoft.ML.Data/Transforms
test/Microsoft.ML.Benchmarks Expand file tree Collapse file tree 2 files changed +70
-18
lines changed Original file line number Diff line number Diff line change @@ -634,26 +634,25 @@ protected override bool MoveNextCore()
634
634
while ( _liveCount < _poolRows && ! _doneConsuming )
635
635
{
636
636
// We are under capacity. Try to get some more.
637
- var hasReadItem = _toConsumeChannel . Reader . TryRead ( out int got ) ;
638
- if ( hasReadItem )
637
+ ValueTask < int > readTask = _toConsumeChannel . Reader . ReadAsync ( ) ;
638
+
639
+ // Note you can't wait synchronously on a ValueTask. So if it
640
+ // hasn't been completed yet, need to call AsTask() to get a Task
641
+ // which can be waited on synchronously.
642
+ int got = readTask . IsCompletedSuccessfully ?
643
+ readTask . Result :
644
+ readTask . AsTask ( ) . GetAwaiter ( ) . GetResult ( ) ;
645
+ if ( got == 0 )
639
646
{
640
- if ( got == 0 )
641
- {
642
- // We've reached the end of the Channel. There's no reason
643
- // to attempt further communication with the producer.
644
- // Check whether something horrible happened.
645
- if ( _producerTaskException != null )
646
- throw Ch . Except ( _producerTaskException , "Shuffle input cursor reader failed with an exception" ) ;
647
- _doneConsuming = true ;
648
- break ;
649
- }
650
- _liveCount += got ;
651
- }
652
- else
653
- {
654
- // Sleeping for one millisecond to stop the thread from spinning while waiting for the producer.
655
- Thread . Sleep ( 1 ) ;
647
+ // We've reached the end of the Channel. There's no reason
648
+ // to attempt further communication with the producer.
649
+ // Check whether something horrible happened.
650
+ if ( _producerTaskException != null )
651
+ throw Ch . Except ( _producerTaskException , "Shuffle input cursor reader failed with an exception" ) ;
652
+ _doneConsuming = true ;
653
+ break ;
656
654
}
655
+ _liveCount += got ;
657
656
}
658
657
if ( _liveCount == 0 )
659
658
return false ;
Original file line number Diff line number Diff line change
1
+ // Licensed to the .NET Foundation under one or more agreements.
2
+ // The .NET Foundation licenses this file to you under the MIT license.
3
+ // See the LICENSE file in the project root for more information.
4
+
5
+ using BenchmarkDotNet . Attributes ;
6
+ using Microsoft . ML . Benchmarks . Harness ;
7
+ using Microsoft . ML . Data ;
8
+
9
+ namespace Microsoft . ML . Benchmarks
10
+ {
11
+ [ CIBenchmark ]
12
+ public class ShuffleRowsBench : BenchmarkBase
13
+ {
14
+ private TrainRow [ ] _rows ;
15
+ private MLContext _context ;
16
+
17
+ [ GlobalSetup ]
18
+ public void Setup ( )
19
+ {
20
+ _rows = new TrainRow [ 10_000 ] ;
21
+ for ( var i = 0 ; i < _rows . Length ; i ++ )
22
+ {
23
+ _rows [ i ] = new TrainRow ( ) { Sample = i . ToString ( ) , Week = i , Label = i / 2 } ;
24
+ }
25
+
26
+ _context = new MLContext ( ) ;
27
+ }
28
+
29
+ [ Benchmark ]
30
+ public void ShuffleRows ( )
31
+ {
32
+ IDataView data = _context . Data . LoadFromEnumerable ( _rows ) ;
33
+
34
+ IDataView shuffledData = _context . Data . ShuffleRows ( data , seed : 0 ) ;
35
+
36
+ foreach ( string sample in shuffledData . GetColumn < string > ( "Sample" ) )
37
+ {
38
+ }
39
+ }
40
+
41
+ private class TrainRow
42
+ {
43
+ [ ColumnName ( "Sample" ) ]
44
+ public string Sample ;
45
+
46
+ [ ColumnName ( "Week" ) ]
47
+ public float Week ;
48
+
49
+ [ ColumnName ( "Label" ) ]
50
+ public float Label ;
51
+ }
52
+ }
53
+ }
You can’t perform that action at this time.
0 commit comments