mojo icon indicating copy to clipboard operation
mojo copied to clipboard

[stdlib] Delegate string comparisons to `StringRef`

Open siitron opened this issue 9 months ago • 2 comments

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

siitron avatar May 11 '24 16:05 siitron

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

siitron avatar May 11 '24 23:05 siitron

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?

JoeLoser avatar May 11 '24 23:05 JoeLoser

✅🟣 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 avatar May 17 '24 02:05 modularbot

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 avatar May 17 '24 02:05 laszlokindrat

Landed in 0fc42722ea09c2fed625a0fd94b767d455bfc493! Thank you for your contribution 🎉

modularbot avatar May 17 '24 05:05 modularbot

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?

siitron avatar May 17 '24 07:05 siitron

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

laszlokindrat avatar May 17 '24 20:05 laszlokindrat

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 avatar May 17 '24 22:05 siitron

Weird, the tests seem to pass on my fork: https://github.com/siitron/mojo/pull/1 🤔

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

siitron avatar May 18 '24 13:05 siitron