Strange code in Queue class in .NET framework

  • Thread starter Thread starter cody
  • Start date Start date
C

cody

Hi folks, I had a look into the source code of the Enqueue(T item)
method of the generic Queue class in the .NET framework.


public void Enqueue(T item)
{
if (this._size == this._array.Length)
{
int capacity = (int) ((this._array.Length * 200L) / 100L);
..


isn't the last line the same as "int capacity = _array.Length * 2" ?
Did I miss something?
 
Hi folks, I had a look into the source code of the Enqueue(T item)
method of the generic Queue class in the .NET framework.


public void Enqueue(T item)
{
if (this._size == this._array.Length)
{
int capacity = (int) ((this._array.Length * 200L) / 100L);
..


isn't the last line the same as "int capacity = _array.Length * 2" ?
Did I miss something?

One possible reason for this might be as a sort of clever "high bandpass
filter". Meaning that if _array.Length * 200 > the amount an int can hold,
it will throw an exception, thus limiting the allocation size to 1/200th of
the size of an int.
 
Erik said:
One possible reason for this might be as a sort of clever "high bandpass
filter". Meaning that if _array.Length * 200 > the amount an int can hold,
it will throw an exception, thus limiting the allocation size to 1/200th of
the size of an int.

Except that it multiplies by 200L, so the length is converted to an Int64
before being multiplied, and no overflow is possible.

You have to keep in mind that this is not the source, really, it's
decompiled IL. The actual source may have looked more sensible.
 
Hello Jeroen,
You have to keep in mind that this is not the source, really, it's
decompiled IL. The actual source may have looked more sensible.

What makes you say that?

The OP hasn't said he's using reflector and MS have released some of their
source.
 
Rory said:
What makes you say that?

The OP hasn't said he's using reflector and MS have released some of
their source.
Yes, this is an assumption on my part, based on the fact that Reflector on
my end gives the exact same source.

I've just looked up the actual source and sufficive to say, it looks different.

I can't be bothered to puzzle through the license to see whether I can
reproduce the code here without a horde of lawyers breathing down my neck
demanding boilerplate, but the summary is that this piece of code grows the
queue by a factor. MS chose to represent the grow factor as a percentage,
which explains the multiplication by 200 rather than 2. This factor is a
constant (it didn't use to be in the non-generic Queue) which is why the
compiler expanded it inline, and the meaning becomes obscured.
 
Thank you, sounds very logical for me.


Jeroen said:
Yes, this is an assumption on my part, based on the fact that Reflector
on my end gives the exact same source.

I've just looked up the actual source and sufficive to say, it looks
different.

I can't be bothered to puzzle through the license to see whether I can
reproduce the code here without a horde of lawyers breathing down my
neck demanding boilerplate, but the summary is that this piece of code
grows the queue by a factor. MS chose to represent the grow factor as a
percentage, which explains the multiplication by 200 rather than 2. This
factor is a constant (it didn't use to be in the non-generic Queue)
which is why the compiler expanded it inline, and the meaning becomes
obscured.
 
Hi folks, I had a look into the source code of the Enqueue(T item)
method of the generic Queue class in the .NET framework.

public void Enqueue(T item)
{
     if (this._size == this._array.Length)
     {
         int capacity = (int) ((this._array.Length * 200L) / 100L);
         ..

isn't the last line the same as "int capacity = _array.Length * 2" ?
Did I miss something?

You can get whole the source code using netmassdownloader. Just grab
it from http://www.codeplex.com/netmassdownloader
 
Back
Top