Implement new `compareByteArrays#` primop
authorHerbert Valerio Riedel <hvr@gnu.org>
Mon, 16 Oct 2017 19:01:57 +0000 (21:01 +0200)
committerHerbert Valerio Riedel <hvr@gnu.org>
Mon, 16 Oct 2017 19:09:06 +0000 (21:09 +0200)
commite3ba26f8b49700b41ff4672f3f7f6a4e453acdcc
tree26880006ca2615143f18349dcf892aa25e3fe29b
parentadd85cc2a3ec0bda810dca2a35264308ffaab069
Implement new `compareByteArrays#` primop

The new primop

    compareByteArrays# :: ByteArray# -> Int# {- offset -}
                       -> ByteArray# -> Int# {- offset -}
                       -> Int# {- length -}
                       -> Int#

allows to compare the subrange of the first `ByteArray#` to
the (same-length) subrange of the second `ByteArray#` and returns a
value less than, equal to, or greater than zero if the range is found,
respectively, to be byte-wise lexicographically less than, to match,
or be greater than the second range.

Under the hood, the new primop is implemented in terms of the standard
ISO C `memcmp(3)` function. It is currently an out-of-line primop but
work is underway to optimise this into an inline primop for a future
follow-up Differential (see D4091).

This primop has applications in packages like `text`, `text-short`,
`bytestring`, `text-containers`, `primitive`, etc.  which currently
have to incur the overhead of an ordinary FFI call to directly or
indirectly invoke `memcmp(3)` as well has having to deal with some
`unsafePerformIO`-variant.

While at it, this also improves the documentation for the existing
`copyByteArray#` primitive which has a non-trivial type-signature
that significantly benefits from a more explicit description of its
arguments.

Reviewed By: bgamari

Differential Revision: https://phabricator.haskell.org/D4090
compiler/prelude/primops.txt.pp
includes/stg/MiscClosures.h
rts/PrimOps.cmm
rts/RtsSymbols.c
testsuite/tests/codeGen/should_run/all.T
testsuite/tests/codeGen/should_run/compareByteArrays.hs [new file with mode: 0644]
testsuite/tests/codeGen/should_run/compareByteArrays.stdout [new file with mode: 0644]