-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issue-44801 EnsureCapacity Apis For List Stack Queue #47149
issue-44801 EnsureCapacity Apis For List Stack Queue #47149
Conversation
Tagging subscribers to this area: @eiriktsarpalis Issue DetailsFix #44801
|
Thanks for taking this. Consider marking this as a draft PR while working on it. |
src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Collections/src/System/Collections/Generic/Stack.cs
Outdated
Show resolved
Hide resolved
@@ -31,6 +31,7 @@ public class Stack<T> : IEnumerable<T>, | |||
private int _version; // Used to keep enumerator in sync w/ collection. Do not rename (binary serialization) | |||
|
|||
private const int DefaultCapacity = 4; | |||
private const int MaxArrayLength = 0X7FEFFFFF; // This is the maximum array length value from Array.MaxArrayLength |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems out of place but there is certainly precedent elsewhere, e.g.
const int MaxArrayLength = 0x7FEFFFFF; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can go away once #43301 lands.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed by moving this temp value to nearby usage scope.
For Queue.EnsureCapacity(), I did not use private SetCapacity() as following check can be removed:
Not sure if it deserved to 'inline' most of its implementation into Queue.EnsureCapacity() rather than calling SetCapacity(). |
It is ready for review now. |
It seems like many of the new tests are failing: https://dev.azure.com/dnceng/public/_build/results?buildId=975051&view=ms.vss-test-web.build-test-results-tab&runId=30602816&resultId=117111&paneView=debug Could you take a look? |
Should because of the last commit about version update which was the only one not run test in my local :( will fix later. |
src/libraries/System.Collections/src/System/Collections/Generic/Queue.cs
Outdated
Show resolved
Hide resolved
@eiriktsarpalis Reopen cannot pass those failures, are they related to this PR ? |
Hi @eiriktsarpalis, build passed after last new commit (although not sure if failure is related to it, maybe it just trigger refresh build) |
src/libraries/System.Collections/src/System/Collections/Generic/Queue.cs
Outdated
Show resolved
Hide resolved
…t, Stack, Queue).
1. Avoid incrementing version number twice in one Insert (or Add) method call; 2. Avoid more capacity check for Insert (or Add) method.
…neric/List.cs Fix comment: update xml doc. Co-authored-by: Eirik Tsarpalis <[email protected]>
6de3226
to
253bcdc
Compare
Hey @lateapexearlyspeed, I just pushed a commit that makes a few adjustments to your PR:
Hope it makes sense. |
Hi @eiriktsarpalis I have no question about change, thanks. And would confirm one thing about following code in Queue.cs about overflow:
In future, if GrowFactor is not 200 (for example 400), then it is possible that newcapacity is indeed overflowed after first line calculation but it skipped third line check because int newcapacity value may be larger than original newcapacity after multiple 4, for example take a 8-bit signed number: (0101 1000) << 2 = (0110 0000). |
Could you elaborate?
EDIT: I pushed a commit that removes the |
No issue, just notice that new test failed :) |
BTW could you help explain "different allocation pattern" ? I know it will definitely have chance to give different newcapacity result if using |
Calling
Oh. I guess it worked on my machine 😄 |
Seems like a mono issue. I just checked and the following is valid in mono 6.12: var arr = new int[int.MaxValue];
arr[int.MaxValue - 1] = 42; So we have two options: either remove the tests or apply the |
You can either use the |
Ok, so if I wanted to have a test guaranteed to only run in CoreCLR (assuming we eventually add more entries to the |
Right, but we usually only use the |
Thank you for your contribution! |
Fix #44801
Current status is to make sure there is a PR on progress, will notify when it is ready for review, thanks.