瀏覽代碼

[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;
 
 				try {
+					// Avoid cache thrashing of locals array
+					var local = locals [index];
+
 					if (seedFunc == null) {
 						if (!enumerator.MoveNext ())
 							return;
-						locals[index] = (U)(object)enumerator.Current;
+						local = (U)(object)enumerator.Current;
 					}
 
 					while (enumerator.MoveNext ()) {
 						if (implementerToken.IsCancellationRequested)
 							break;
 						token.ThrowIfCancellationRequested ();
-						locals[index] = localCall (locals[index], enumerator.Current);
+						local = localCall (local, enumerator.Current);
 					}
+
+					locals [index] = local;
 				} finally {
 					enumerator.Dispose ();
 				}