Skip to content
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

[stdlib] Deque - part 2 - add operator dunders and remaining traits #3729

Closed
wants to merge 2 commits into from

Conversation

avitkauskas
Copy link
Contributor

This is a second part of Deque implementation adding the operator dunders and all remaining traits.

@avitkauskas avitkauskas requested a review from a team as a code owner November 1, 2024 15:07
Copy link
Collaborator

@JoeLoser JoeLoser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, thank you for continuing this work! I left a few minor comments, but happy to see this moving along. When they're addressed, happy to sync this internally and land it.

Feel free to start adding a changelog entry if you'd like for your deque work, or would you prefer until it's fully finished from your perspective?

stdlib/src/collections/deque.mojo Show resolved Hide resolved
stdlib/src/collections/deque.mojo Show resolved Hide resolved
stdlib/src/collections/deque.mojo Show resolved Hide resolved

q[-1] = 4
assert_equal(q[1], 4)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question It may be good to add a test for "index normalization" that we do in other collections, or explicitly note that we don't do this for deque. What do you think? Concretely, this would be adding a test case for a negative index (not -1), e.g. -2 in this case, or -3, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please clarify this. We do not want to allow indexes out of range in Deque (Python raises IndexError in these cases). I use debug_assert() for that in the code (same as I see it used in the List implementation). Though I do not fully understand how this should work in Mojo, as the documentation says it's noop in production builds, then actually it means undefined behaviour for the indexes out of range in production builds. Not good in my point of view. Actually, if I try this code

    q = Deque(1, 2)
    e = q[-3]
    print(e)

with mojo run file.mojo it gives 5359310824, and if I try mojo run -g err.mojo it gives me 1. Why?
Why does the debug_assert not fail in debug compile with -g?

debug_assert(
   -len(self) <= normalized_idx < len(self),
    "index: ",
    normalized_idx,
    " is out of bounds for `Deque` of size: ",
    len(self),
)

In nightly tests _ = q[-3] does fail, but I cannot test it with assert_raises().

So, I can add a test case with -2 if it helps, but not with -3 as debug_assert() should 'somehow' save me from these out of range cases, but how? So what should be the desired behaviour in stdlib anyway?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, the name debug_assert has nothing to do with "debug mode". To control the behavior of debug_assert, you'd need to specify -D ASSERT=all for example to enable assertions.

Right now, the testing for various debug_assert isn't ideal, but doable - see

# ensure out-of-bounds access calls abort through the assert handler
for example (note the test is using the not --crash in its RUN directive).

More broadly speaking, what our plan is with index normalization for various collections is something we need to figure out, so sticking with what you have is totally fine. Right now, due to the viralness of raises, we don't want to raise for out of bound accesses like in Python. Otherwise, it pushes all downstream callers to be in a potentially-raising context which won't be nice in the current state of Mojo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to test the index out of range (to include it in the next PR part).
I included these lines in the test file:

# REQUIRES: has_not
# RUN: not --crash mojo -D ASSERT=all %s 2>&1

But I got an error:

FAIL: Mojo Standard Library :: collections/test_deque.mojo (1 of 1)
******************** TEST 'Mojo Standard Library :: collections/test_deque.mojo' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
RUN: at line 14: not --crash mojo -D ASSERT=all /Users/alvis/repos/mojo/stdlib/test/collections/test_deque.mojo 2>&1
+ not --crash mojo -D ASSERT=all /Users/alvis/repos/mojo/stdlib/test/collections/test_deque.mojo
--

What else am I missing?

stdlib/test/collections/test_deque.mojo Show resolved Hide resolved
@JoeLoser JoeLoser self-assigned this Nov 4, 2024
@JoeLoser JoeLoser requested a review from ConnorGray November 4, 2024 20:12
@@ -274,6 +506,37 @@ struct Deque[ElementType: CollectionElement](
if self._head == self._tail:
self._realloc(self._capacity << 1)

fn appendleft(inout self, owned value: ElementType):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why it is called appendleft and not appendleft? Mojo style guide points that snake_case should be used :)

Copy link
Contributor Author

@avitkauskas avitkauskas Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how it named in Python: https://docs.python.org/3/library/collections.html#collections.deque
Do we want to follow Python API, or should we follow the Mojo guides?
Rust and C++ call it push_front and push_back, so lots of choices if we do not want to follow Python :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's follow the Python APIs for the time being and we can discuss this in our next team design meeting (CC: @ConnorGray).

@@ -306,6 +569,40 @@ struct Deque[ElementType: CollectionElement](
(src + i).move_pointee_into(self._data + self._tail)
self._tail = self._physical_index(self._tail + 1)

fn extendleft(inout self, owned values: List[ElementType]):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

@avitkauskas
Copy link
Contributor Author

avitkauskas commented Nov 4, 2024

Please give me feedback on my comments to move forward. I cannot make any changes without your further guidance right now. Concerning the changelog entry, I will add it together with the last part that should follow after this PR.

@JoeLoser
Copy link
Collaborator

JoeLoser commented Nov 8, 2024

!sync

@modularbot modularbot added the imported-internally Signals that a given pull request has been imported internally. label Nov 8, 2024
@modularbot
Copy link
Collaborator

✅🟣 This contribution has been merged 🟣✅

Your pull request has been merged to the internal upstream Mojo sources. It will be reflected here in the Mojo repository on the nightly branch during the next Mojo nightly release, typically within the next 24-48 hours.

We use Copybara to merge external contributions, click here to learn more.

@modularbot modularbot added merged-internally Indicates that this pull request has been merged internally merged-externally Merged externally in public mojo repo labels Nov 8, 2024
modularbot pushed a commit that referenced this pull request Nov 9, 2024
…ng traits (#50553)

[External] [stdlib] Deque - part 2 - add operator dunders and remaining
traits

This is a second part of Deque implementation adding the operator
dunders and all remaining traits.

Co-authored-by: Alvydas Vitkauskas <[email protected]>
Closes #3729
MODULAR_ORIG_COMMIT_REV_ID: b57e61d106fc1622eb92192e8133ddfe63fa5666
@modularbot
Copy link
Collaborator

Landed in ab95268! Thank you for your contribution 🎉

@modularbot modularbot closed this Nov 9, 2024
@avitkauskas avitkauskas deleted the deque-2 branch November 9, 2024 10:26
modularbot pushed a commit that referenced this pull request Dec 17, 2024
…ng traits (#50553)

[External] [stdlib] Deque - part 2 - add operator dunders and remaining
traits

This is a second part of Deque implementation adding the operator
dunders and all remaining traits.

Co-authored-by: Alvydas Vitkauskas <[email protected]>
Closes #3729
MODULAR_ORIG_COMMIT_REV_ID: b57e61d106fc1622eb92192e8133ddfe63fa5666
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
imported-internally Signals that a given pull request has been imported internally. merged-externally Merged externally in public mojo repo merged-internally Indicates that this pull request has been merged internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants