
How the JVM compares strings on x86 - jcdavis
http://jcdav.is/2016/09/01/How-the-JVM-compares-your-strings/
======
qwertyuiop924
Am I seriously the only person on HN who doesn't know x86 backwards and
forwards? Because it seems from the comments like I'm the only one here that
didn't know this.

This was a good article, though.

~~~
2bitencryption
rule #1 about Hacker News:

ALWAYS make sure you comment to let people know how smart you are. This is
IMPERATIVE.

...even if you have to Google it a bit to brush up, always come back to let
people know that you are knowledgeable.

~~~
jdc
You might say this facetiously, but this is would lead to some very
informative comments, right?

~~~
qwertyuiop924
I disagree. If you never reveal a lack of knowledge, than you'll never learn
anything from the conversation: some of my best experiences on HN were times I
was totally wrong.

------
TazeTSchnitzel
I saw “the x86 instruction you've never heard of” and thought, hey, it's
probably the SSE strcmp instructions. So, the title is clickbait.

Nonetheless, interesting article. I had no idea it could handle UTF-16!

~~~
rcthompson
"Compare strings faster with this one weird x86 instruction!"

~~~
avar
"ARM CPU vendors HATE HIM!"

~~~
protomyth
At some point, someone is going to write the most popular plug-in ever that
rewrites all titles in clear language and does a quick fetch of the page to
answer the questions posed by the title. That's the kind of expert system I'm
looking for.

~~~
willismichael
But then what happens when somebody writes a clickbait title about said expert
system?

~~~
brokenmachine
I HATE MYSELF! Bzz.. whirr click... Destroy all humans!

~~~
protomyth
Clearly your programming team needs this manager
[https://www.youtube.com/watch?v=d70OfGfMtIA](https://www.youtube.com/watch?v=d70OfGfMtIA)
if you're thinking of releasing software that goes into "Destroy all humans!"
mode.

------
stcredzero
For awhile, CISC architectures (basically, just "architectures" before RISC
came around) kept accumulating features. I took a VAX assembly language ("VAX
Macro") class, and I remember that there was a string compare command in VAX
Macro that was about as full featured as what you'd expect in some kind of
standard library.

So I'm not surprised to learn that there's something in an Intel architecture
like this: Intel is just showing its CISC roots. Since it's associated with
SSE, there's probably something advantageously parallel about it.

~~~
pcwalton
It's really more about SIMD than about CISC or RISC. The pcmpxstrx family of
instructions aren't like classical CISC string-compare instructions (though
note that those are there too—"repe cmps"). Rather, they take 16-byte
registers and perform operations (find first byte matching X, etc) on the
bytes in them. I imagine that they can take advantage of effectively map-
reduce style techniques for this (which is what you would do in software too
if you were using simpler SIMD instructions).

Note that pcmpxstrx isn't really that fast in many cases compared to just
doing ordinary SIMD operations.

~~~
stcredzero
The string compare operations built into old CISC architectures also weren't
that fast. It's more like they were designed as bullet points for the
salesmen. (Who were talking to execs who remember managing people who wrote
things in assembly.)

------
astrange
Let me spoil the title - it's the SSE4.2 instruction dedicated to comparing C
strings. Also, I had actually heard of it.

~~~
Someone
Not only C strings; this code uses the Pascal string variant:

 _" This monster of an instruction takes in 4 registers of input: the 2
strings themselves as parameters, plus their lengths in %rax and %rdx (‘e’
meaning explicit length - pcmpistri & pcmpistrm instead look for terminating
nulls"_

------
Lorddigital
You ever been hacked ? or You wanna hack someone or anything? You wanna test
the protection strength of your website, computer or network ? Do you wanna
hack into a computer, website or network? Social Media Threats Has your
Facebook, Twitter or Google+ account .He can help get it restored and track
the person who did it in many cases. Computer Spying and Surveillance. Do you
want to install spyware on a cellphone or computer? Do you want to know if you
have spyware on your computer? Bank account transfer to all Banks.......Hack
and upgrade or change university grades Bank accounts hackErase criminal
records hack Facebook, hack Any social media account, hack Android & iPhone
Hack.....Text message interception.. hack email interception, hack Untraceable
IpTwitters, hack email accounts, hack (Grade Changes), restore crashed
Website, hack servers, hack Skype, hack Databases, hack Word Press & Blogs,
hack Individual computers, hack Control devices remotely, hack Burner Numbers,
hack Verified Paypal Accounts. I am a hacker looking for some one who got old
bank account in usa and i wil load it with 4k to 40 k we share 5050 no upfront
fee. I need BOA, USA navy chase, welsfargo, retirement account old bank
account asap no upfront fee 5050......emaIL ME Lorddigital.hacker@gmail.com

------
tyfon
I'm actually wondering why they do not compare the lengths first, anyone know?
To me that would seem like the first obvious optimization, do not run the
character compare at all if the lengths are different.

~~~
gizmo686
If the strings are not equal, they still need to say which string is "smaller"
(eg. comes first alphabetically).

~~~
tyfon
Well, that explains it! :)

------
Lorddigital
Need a hacker? A senior member of icefog hackers.A professional in the areas
mentioned below: 1.facebook,whatsapp,emails,twitter and instagram hack 2-
Hacking computer system 3- Cloning of phones 4- Changing DMV records without
leaving traces 5- Changing school grades without leaving traces 6- Retrieving
hacked social media accounts 7- Credit card hack 8- Clearing criminal records
9- Clearing of bad driving records without leaving traces 10- Website hack 11-
Retrieving hacked social media accounts 12- P.I - Private investigator like
cheating husbands or wives and any other P.I activities and so many other
services. 13- Catch hacker scammers 14- Wire transfer to any bank 15-
Retrieval of hacked accounts (facebook,twitter,instagram,zoosk,match etc..)
16- Bank login 16- Phishing emails. to mention a few..He is at your service if
you need him. Contact .Email- lorddigital.hacker@gmail.com.. BEWARE OF FAKE
HACKERS..TAKE RECOMMENDATIONS AND GO FOR IT

------
stephencanon
Just doing simple SIMD compares is as fast on most microarchitectures, and
faster on Haswell+. You need to be doing something more involved than simple
equality testing for the SSE4.2 instructions to be beneficial.

~~~
NegativeLatency
this does lexicographical comparison, not just equality

~~~
masklinn
It can do more than just string equality too (the first operand can be treated
as a character sequence, a character set, a substring needle or a union of
character ranges)

Though for all that it's still limited to simplistic comparisons of non-
textual strings.

------
faragon
And not just string compare. There are many operations where general purpose
CPUs have problems for giving optimum results when operating with elements of
1 or 2 bytes at once with dependencies between them. E.g. UTF-8 string search
implemented in hardware using the Rabin-Karp algorithm with a simple enough
reversible hash window guarantying O(n) time could outperform current string
search optimal O(n) implementations by e.g. 16x on CPUs with 128 bit / cycle
load capabilities.

------
taeric
I would be interested to see speed comparisons to know how much this
instruction helps. My gut would be that it helps. A lot.

~~~
DannyBee
For short strings, it basically doesn't help. For longer strings, it's a bit
faster, it depends how bad what you were doing before was.

Straight string comparison is not a great application for this in part because
it's to intel's advantage to make string compare loops work fast without it.

But you can be sure it speeds some customer's serious workload up
significantly. That said, it all depends.

It's great for "figure out if there is newline in the next 16 characters".

But again, compared to basic SSE compares, it often falls down for random
string comparison.

See, e.g,
[https://www.strchr.com/strcmp_and_strlen_using_sse_4.2](https://www.strchr.com/strcmp_and_strlen_using_sse_4.2)
which has timings on core i5.

it's of course, likely possible to find processors where it is faster/slower.

~~~
pcwalton
Edit: I'm stupid and this comment was totally wrong; see below.

~~~
DannyBee
FWIW: it actually does. i measured it in clang :)

Why, i have no idea. My expectation would have been the same as yours

(also, it's pcmp* and tzcnt)

------
cperciva
Definitely a clickbait title... dang, can you edit this down to "How the JVM
compares strings" or possibly "Comparing strings using the x86 SSE4.2
pcmpestri instruction"?

~~~
sctb
Thanks, we've updated the title.

