我想我发现了该方法中的一个错误Partitioner.Create(int fromInclusive, int toExclusive)
。rangeSize
当范围超过 时,它会计算未提供的参数 的负值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 => -178956971
Range: -178956971 => -357913941
Range: -357913941 => -536870911
Range: -536870911 => -715827881
Range: -715827881 => -894784851
Range: -894784851 => -1073741821
Range: -1073741821 => -1252698791
Range: -1252698791 => -1431655761
Range: -1431655761 => -1610612731
Range: -1610612731 => -1789569701
Range: -1789569701 => -1968526671
Range: -1968526671 => -2147483641
Range: -2147483641 => 2147483647
因此,循环抛出这些范围for (int i = range.Item1; i < range.Item2; i++)将导致除最后一个范围之外的所有范围的零循环,这将有效地循环该Int32类型的整个范围。
有一个特殊情况。下面的分区器计算 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
}
看来减法时发生了整数溢出toExclusive - fromInclusive。
如果这确实是一个错误,在 .NET Framework 的未来版本中修复该错误之前,您建议采用什么解决方法?
动漫人物
喵喵时光机
相关分类