Skip to content

Conversation

@Youssef1313
Copy link
Member

@Youssef1313 Youssef1313 commented May 12, 2020

Based on the information provided in #36228 (comment)
special-casing the length of zero is good to avoid allocating a new empty array.

@stephentoub, Can you confirm if this is correct in that context?

@ghost
Copy link

ghost commented May 12, 2020

Tagging subscribers to this area: @eiriktsarpalis
Notify danmosemsft if you want to be subscribed.

@stephentoub
Copy link
Member

stephentoub commented May 12, 2020

Thanks. Are you seeing a lot of places where these specific collection types are constructed with 0 as an argument? Generally if you're going to pass 0, you just wouldn't use this ctor (you'd use the parameterless one), and while it's possible the value was constructed with math that could result in a value of 0, from what I've seen of usage, that's very rare. The downside to this change is it's adding more code/expense for the much more common case where the capacity isn't 0.

@Youssef1313
Copy link
Member Author

Thanks. Are you seeing a lot of places where these specific collection types are constructed with 0 as an argument? Generally if you're going to pass 0, you just wouldn't use this ctor (you'd use the parameterless one), and while it's possible the value was constructed with math that could result in a value of 0, from what I've seen of usage, that's very rare. The downside to this change is it's adding more code/expense for the much more common case where the capacity isn't 0.

Internally in the runtime, I didn't notice a usage with the value of 0.
But for consumers, the value may come from an expression that evaluates to 0 at runtime.

@stephentoub
Copy link
Member

But for consumers, the value may come from an expression that evaluates to 0 at runtime.

Sure, but how common is that? Can you point to some real examples (e.g. in other projects on GitHub) where that would happen?

@Youssef1313
Copy link
Member Author

But for consumers, the value may come from an expression that evaluates to 0 at runtime.

Sure, but how common is that? Can you point to some real examples (e.g. in other projects on GitHub) where that would happen?

Found a few usages of that constructor in Roslyn here, but couldn't figure if the passed value could be a zero or it will be always a non-zero.

@stephentoub
Copy link
Member

Those won't be 0. Those paths are only hit if the tree isn't empty, which means the depth being computed will be greater than zero.

@Youssef1313
Copy link
Member Author

Those won't be 0. Those paths are only hit if the tree isn't empty, which means the depth being computed will be greater than zero.

I've tried to search more, but didn't find anything. I'm going to close it as this case seem to be very rare.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

3 participants