소스 검색

[PLinq] Avoid cache thrashing of locals array when looping over enumerator

Ludovic Henry 11 년 전
부모
커밋
44214ee4de
1개의 변경된 파일7개의 추가작업 그리고 2개의 파일을 삭제
  1. 7 2
      mcs/class/System.Core/System.Linq.Parallel/ParallelExecuter.cs

+ 7 - 2
mcs/class/System.Core/System.Linq.Parallel/ParallelExecuter.cs

@@ -230,18 +230,23 @@ namespace System.Linq.Parallel
 				var implementerToken = options.ImplementerToken;
 				var implementerToken = options.ImplementerToken;
 
 
 				try {
 				try {
+					// Avoid cache thrashing of locals array
+					var local = locals [index];
+
 					if (seedFunc == null) {
 					if (seedFunc == null) {
 						if (!enumerator.MoveNext ())
 						if (!enumerator.MoveNext ())
 							return;
 							return;
-						locals[index] = (U)(object)enumerator.Current;
+						local = (U)(object)enumerator.Current;
 					}
 					}
 
 
 					while (enumerator.MoveNext ()) {
 					while (enumerator.MoveNext ()) {
 						if (implementerToken.IsCancellationRequested)
 						if (implementerToken.IsCancellationRequested)
 							break;
 							break;
 						token.ThrowIfCancellationRequested ();
 						token.ThrowIfCancellationRequested ();
-						locals[index] = localCall (locals[index], enumerator.Current);
+						local = localCall (local, enumerator.Current);
 					}
 					}
+
+					locals [index] = local;
 				} finally {
 				} finally {
 					enumerator.Dispose ();
 					enumerator.Dispose ();
 				}
 				}