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] Delegate string comparisons to StringRef #2620

Closed
wants to merge 5 commits into from

Conversation

siitron
Copy link

@siitron siitron commented May 11, 2024

This is a follow-up to #2409.

String comparisons for StringRef are implemented. StringRef make use of memory.memcmp for all of its 6 comparisons now, hopefully this change is ok.

String's and StringLiteral's comparisons (__eq__, __ne__, __lt__, __le__, __gt__, & __ge__) are delegated to StringRef. As a result:

  1. String comparisons uses its _strref_dangerous-method. And,
  2. StringLiteral's local _memcmp-function is removed (which was used "rather than memory.memcpy to avoid #31139 and #25100").

The base logic for all 18 comparisons are implemented in StringRef's __ne__ and __lt__ methods. All other comparisons uses some variation/negation of either __ne__ or __lt__.

…ring and StringLiteral are delegated to StringRef. Tests for StringRef comparisons added.

Signed-off-by: Simon Hellsten <simonhellsten@hotmail.com>
@siitron siitron changed the title [stdlib] String comparisons delegated to StringRef [stdlib] String comparisons delegated to StringRef May 11, 2024
@siitron siitron changed the title [stdlib] String comparisons delegated to StringRef [stdlib] Delegating string comparisons to StringRef May 11, 2024
@siitron siitron changed the title [stdlib] Delegating string comparisons to StringRef [stdlib] Delegate string comparisons to StringRef May 11, 2024
@siitron
Copy link
Author

siitron commented May 11, 2024

Ubuntu tests got stuck for 6 hours (but the macos ones passed). I'm closing + reopening this to rerun the tests.

@siitron siitron closed this May 11, 2024
@siitron siitron reopened this May 11, 2024
@JoeLoser
Copy link
Collaborator

Ubuntu tests got stuck for 6 hours (but the macos ones passed). I'm closing + reopening this to rerun the tests.

Still looks like they're hanging for you. I just posted about this on Discord, but I don't think the hang is due to your change?

@siitron siitron marked this pull request as ready for review May 13, 2024 20:36
@siitron siitron requested a review from a team as a code owner May 13, 2024 20:36
@laszlokindrat laszlokindrat self-assigned this May 13, 2024
@laszlokindrat laszlokindrat added the imported-internally Signals that a given pull request has been imported internally. label May 16, 2024
Copy link
Contributor

@laszlokindrat laszlokindrat left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for fixing this!

@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 the merged-internally Indicates that this pull request has been merged internally label May 17, 2024
@laszlokindrat
Copy link
Contributor

FYI I had to modify this internally to avoid the memcmp recursion. It's the same problem that necessitated the _memcmp workaround, and I did a similar workaround as well.

@modularbot modularbot added the merged-externally Merged externally in public mojo repo label May 17, 2024
@modularbot
Copy link
Collaborator

Landed in 0fc4272! Thank you for your contribution 🎉

modularbot added a commit that referenced this pull request May 17, 2024
[External] [stdlib] Delegate string comparisons to `StringRef`

This is a follow-up to #2409.

String comparisons for `StringRef` are implemented. `StringRef` make use
of `memory.memcmp` for all of its 6 comparisons now, hopefully this
change is ok.

`String`'s and `StringLiteral`'s comparisons (`__eq__`, `__ne__`,
`__lt__`, `__le__`, `__gt__`, & `__ge__`) are delegated to `StringRef`.
As a result:
1. `String` comparisons uses its `_strref_dangerous`-method. And,
2. `StringLiteral`'s local `_memcmp`-function is removed (which was used
"rather than memory.memcpy to avoid #31139 and #25100").

The base logic for all 18 comparisons are implemented in `StringRef`'s
`__ne__` and `__lt__` methods. All other comparisons uses some
variation/negation of either `__ne__` or `__lt__`.

ORIGINAL_AUTHOR=Simon Hellsten
<56205346+siitron@users.noreply.github.com>
PUBLIC_PR_LINK=#2620

---------

Co-authored-by: Simon Hellsten <56205346+siitron@users.noreply.github.com>
Closes #2620
MODULAR_ORIG_COMMIT_REV_ID: ed03ee44f06caed7cad77c9c909e83d3c2c2d3cc
@modularbot modularbot closed this May 17, 2024
@siitron
Copy link
Author

siitron commented May 17, 2024

FYI I had to modify this internally to avoid the memcmp recursion. It's the same problem that necessitated the _memcmp workaround, and I did a similar workaround as well.

@laszlokindrat Great that you got it to work! But, does this mean that String now have to use a non-vectorized version of memcmp when doing comparisons?

@laszlokindrat
Copy link
Contributor

@laszlokindrat Great that you got it to work! But, does this mean that String now have to use a non-vectorized version of memcmp when doing comparisons?

Yes, unfortunately, although my understanding is that the code before your patch had a similar workaround. If you are interested, you can give it a try: the recursion probably comes from the constraint or some assert or print from the memcmp implemention. In theory, you might be able to refactor to work around this.

@siitron
Copy link
Author

siitron commented May 17, 2024

Yes, unfortunately, although my understanding is that the code before your patch had a similar workaround. If you are interested, you can give it a try: the recursion probably comes from the constraint or some assert or print from the memcmp implemention. In theory, you might be able to refactor to work around this.

Yes, the workaround is similar to what StringLiteral had before, but String can use the regular memcmp. I could maybe try and see if anything could be done about the recursion but idk, I don't even know how to trigger it. It's at least nice to have it all in one place :)

@siitron
Copy link
Author

siitron commented May 18, 2024

Weird, the tests seem to pass on my fork: siitron#1 🤔

I'm creating a new PR if you want to take a look: #2740

msaelices pushed a commit to msaelices/mojo that referenced this pull request May 18, 2024
[External] [stdlib] Delegate string comparisons to `StringRef`

This is a follow-up to modularml#2409.

String comparisons for `StringRef` are implemented. `StringRef` make use
of `memory.memcmp` for all of its 6 comparisons now, hopefully this
change is ok.

`String`'s and `StringLiteral`'s comparisons (`__eq__`, `__ne__`,
`__lt__`, `__le__`, `__gt__`, & `__ge__`) are delegated to `StringRef`.
As a result:
1. `String` comparisons uses its `_strref_dangerous`-method. And,
2. `StringLiteral`'s local `_memcmp`-function is removed (which was used
"rather than memory.memcpy to avoid #31139 and #25100").

The base logic for all 18 comparisons are implemented in `StringRef`'s
`__ne__` and `__lt__` methods. All other comparisons uses some
variation/negation of either `__ne__` or `__lt__`.

ORIGINAL_AUTHOR=Simon Hellsten
<56205346+siitron@users.noreply.github.com>
PUBLIC_PR_LINK=modularml#2620

---------

Co-authored-by: Simon Hellsten <56205346+siitron@users.noreply.github.com>
Closes modularml#2620
MODULAR_ORIG_COMMIT_REV_ID: ed03ee44f06caed7cad77c9c909e83d3c2c2d3cc
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.

None yet

4 participants