[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework

Strange code in Queue class in .NET framework

cody

3/2/2008 4:54:00 PM

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?
6 Answers

Erik Funkenbusch

3/2/2008 5:31:00 PM

0

On Sun, 02 Mar 2008 17:54:03 +0100, cody wrote:

> 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.

Jeroen Mostert

3/2/2008 5:35:00 PM

0

Erik Funkenbusch wrote:
> On Sun, 02 Mar 2008 17:54:03 +0100, cody wrote:
>
>> 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.

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.

--
J.

Rory Becker

3/2/2008 7:12:00 PM

0

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


Jeroen Mostert

3/2/2008 7:40:00 PM

0

Rory Becker wrote:
>> 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.
>
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.

--
J.

cody

3/3/2008 12:27:00 AM

0

Thank you, sounds very logical for me.


Jeroen Mostert wrote:
> Rory Becker wrote:
>>> 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.
>>
> 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.
>

keremskusmezer@gmail.com

3/5/2008 9:57:00 AM

0

On Mar 2, 6:54 pm, cody <deutron...@gmx.de> wrote:
> 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/netmass...