我想我發(fā)現(xiàn)了該方法中的一個錯誤Partitioner.Create(int fromInclusive, int toExclusive)。rangeSize當(dāng)范圍超過 時,它會計算未提供的參數(shù) 的負(fù)值Int32.MaxValue。這是演示該問題的代碼示例:var partitioner = Partitioner.Create(-1, Int32.MaxValue);var partitions = partitioner.GetPartitions(1);foreach (var partition in partitions){? ? while (partition.MoveNext())? ? {? ? ? ? var range = partition.Current;? ? ? ? Console.WriteLine($"Range: {range.Item1,11} => {range.Item2,11}");? ? }}輸出:Range:? ? ? ? ? -1 =>? -178956971Range:? -178956971 =>? -357913941Range:? -357913941 =>? -536870911Range:? -536870911 =>? -715827881Range:? -715827881 =>? -894784851Range:? -894784851 => -1073741821Range: -1073741821 => -1252698791Range: -1252698791 => -1431655761Range: -1431655761 => -1610612731Range: -1610612731 => -1789569701Range: -1789569701 => -1968526671Range: -1968526671 => -2147483641Range: -2147483641 =>? 2147483647因此,循環(huán)拋出這些范圍for (int i = range.Item1; i < range.Item2; i++)將導(dǎo)致除最后一個范圍之外的所有范圍的零循環(huán),這將有效地循環(huán)該Int32類型的整個范圍。有一個特殊情況。下面的分區(qū)器計算 arangeSize為 1。Partitioner.Create(Int32.MinValue, Int32.MaxValue);下面是該方法的源代碼:public static OrderablePartitioner<Tuple<int, int>> Create(? ? int fromInclusive, int toExclusive){? ? // How many chunks do we want to divide the range into?? If this is 1, then the? ? // answer is "one chunk per core".? Generally, though, you'll achieve better? ? // load balancing on a busy system if you make it higher than 1.? ? int coreOversubscriptionRate = 3;? ? if (toExclusive <= fromInclusive) throw new ArgumentOutOfRangeException("toExclusive");? ? int rangeSize = (toExclusive - fromInclusive) /? ? ? ? (PlatformHelper.ProcessorCount * coreOversubscriptionRate);? ? if (rangeSize == 0) rangeSize = 1;? ? return Partitioner.Create(CreateRanges(fromInclusive, toExclusive, rangeSize),? ? ? ? EnumerablePartitionerOptions.NoBuffering); // chunk one range at a time}看來減法時發(fā)生了整數(shù)溢出toExclusive - fromInclusive。如果這確實是一個錯誤,在 .NET Framework 的未來版本中修復(fù)該錯誤之前,您建議采用什么解決方法?
2 回答

動漫人物
TA貢獻(xiàn)1815條經(jīng)驗 獲得超10個贊
看來整數(shù)溢出發(fā)生在除法 toExclusive - fromInclusive 上。
是的,看起來這是一個錯誤。
在 .NET Framework 的未來版本中修復(fù)該問題之前,您建議采用什么解決方法?
我建議將您的輸入轉(zhuǎn)換為long
并調(diào)用該版本。它仍然存在類似的溢出錯誤,但如果您的原始輸入是,int
您絕對不會遇到溢出情況long
。

喵喵時光機(jī)
TA貢獻(xiàn)1846條經(jīng)驗 獲得超7個贊
這是一個錯誤,我已記錄了一個問題,以便在 .NET Core 的未來版本中修復(fù)此問題https://github.com/dotnet/corefx/issues/40201
并且建議的解決方法也是正確的。只需將輸入?yún)?shù)轉(zhuǎn)換為 long 即可。
- 2 回答
- 0 關(guān)注
- 207 瀏覽
添加回答
舉報
0/150
提交
取消