You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This has been raised in course of haskell/text#365 (comment)
New text UTF8 engine is based on a function isValidUtf8 :: ByteString -> Bool, implemented in C. This is a valuable routine for other packages, but it looks strange to export it from text: the signature does not mention Text at all.
How do we feel about moving this function to bytestring proper? Caveat is that the implementation comes with a chunky C/C++ source.
It would not be in good style. The good style is to parse with a hypothetical parseUtf8 ∷ ByteString → Maybe Text and downgrade to Bool as a special case. In what a use case would one prefer to keep their data in a ByteString after the knowledge that it is Text has been expensively obtained? Is isValidUtf8 much cheaper than a hypothetical parseUtf8?
A bit of background, because this was mostly discussed elsewhere.
The crucial piece of performant UTF8-backed Text is an access to a fast validation routine. Even while Data.Text.Encoding.decodeUtf8 :: ByteString -> Text can just copy a valid UTF8 buffer, it must ensure that the buffer is valid first. This proves to be a non-trivial job. Currently text HEAD relies on simdutf for this. The problem is that simdutf is huge (~500K) and, more importantly, is C++. GHC does not really have a great story of linking to C++ modules, and text is a boot library to be built both in stage 0 and stage 1 compiler, both by make and by hadrian, etc. This also, strictly speaking, adds C++ compiler to a toolchain, required to build GHC. @kozross has been working for several months by now on a pure C replacement for simdutf, and I hope that we'll be able to switch to his library to resolve complications above.
@sjakobi It is supported, but only as the fallback. I can restore it easily enough. As far as the 'happy path' goes, it is blocked only on the fallback. I can restore it there as well, but I would argue it's not as pressing a problem.
Look at the isValidUtf8 API, I wonder whether perhaps rather than returning a Bool result it'd be more useful to return the length of the longest leading valid UTF8 prefix of the string.
When reading ByteStrings from various streaming sources, the last few bytes of the string may be an incomplete UTF8 sequence, and "topping it up" may make it valid again.
Said, there can also be strings that are invalid for reasons other than unexpected truncation, so if this is really the direction, one might return a negative length (say -1) for invalid strings, and a positive length for strings whose tail may yet be fixable.
Of course there could also be more than one interface here. Thoughts???
@vdukhovni The biggest consumer of this is text, which does replacement of invalid sequences anyway. Therefore, an extended API doesn't help much, which is why I didn't provide it.
Activity
kindaro commentedon Sep 5, 2021
It would not be in good style. The good style is to parse with a hypothetical
parseUtf8 ∷ ByteString → Maybe Textand downgrade toBoolas a special case. In what a use case would one prefer to keep their data in aByteStringafter the knowledge that it isTexthas been expensively obtained? IsisValidUtf8much cheaper than a hypotheticalparseUtf8?Bodigrim commentedon Sep 5, 2021
There is already
Data.Text.Encoding.decodeUtf8', similar to yourparseUtf8.The idea is that there are other packages than
text, which can benefit from fast utf8 validation. For example,text-shortandutf8-string.sjakobi commentedon Sep 11, 2021
isValidUtf8 :: ByteString -> Boolsounds like a reasonable addition to me. 👍Bodigrim commentedon Sep 11, 2021
CC @kozross
kozross commentedon Sep 11, 2021
That would make sense to me. In fact, it'd make it easier to port my work I think.
sjakobi commentedon Sep 13, 2021
It would be nice if this feature could be included in the upcoming 0.11.2.0 release.
@kozross, do I understand you correctly that you could provide an implementation? If so, roughly when could provide a PR?
kozross commentedon Sep 13, 2021
@sjakobi I've requested a work day to get this wired in. If this gets approved, tomorrow.
Bodigrim commentedon Sep 13, 2021
A bit of background, because this was mostly discussed elsewhere.
The crucial piece of performant UTF8-backed
Textis an access to a fast validation routine. Even whileData.Text.Encoding.decodeUtf8 :: ByteString -> Textcan just copy a valid UTF8 buffer, it must ensure that the buffer is valid first. This proves to be a non-trivial job. CurrentlytextHEAD relies onsimdutffor this. The problem is thatsimdutfis huge (~500K) and, more importantly, is C++. GHC does not really have a great story of linking to C++ modules, andtextis a boot library to be built both in stage 0 and stage 1 compiler, both by make and by hadrian, etc. This also, strictly speaking, adds C++ compiler to a toolchain, required to build GHC. @kozross has been working for several months by now on a pure C replacement forsimdutf, and I hope that we'll be able to switch to his library to resolve complications above.hasufell commentedon Oct 30, 2021
You can't build current GHCs without a valid C++ compiler, btw.
Bodigrim commentedon Nov 3, 2021
Reopening, since ideally we should have ARM64 support + happy path for ASCII text restored.
sjakobi commentedon Nov 3, 2021
Just to be clear:
isValidUtf8? Is the function entirely disabled on this architecture?kozross commentedon Nov 3, 2021
@sjakobi It is supported, but only as the fallback. I can restore it easily enough. As far as the 'happy path' goes, it is blocked only on the fallback. I can restore it there as well, but I would argue it's not as pressing a problem.
vdukhovni commentedon Nov 14, 2021
Look at the
isValidUtf8API, I wonder whether perhaps rather than returning aBoolresult it'd be more useful to return the length of the longest leading valid UTF8 prefix of the string.When reading ByteStrings from various streaming sources, the last few bytes of the string may be an incomplete UTF8 sequence, and "topping it up" may make it valid again.
Said, there can also be strings that are invalid for reasons other than unexpected truncation, so if this is really the direction, one might return a negative length (say -1) for invalid strings, and a positive length for strings whose tail may yet be fixable.
Of course there could also be more than one interface here. Thoughts???
kozross commentedon Nov 14, 2021
@vdukhovni The biggest consumer of this is
text, which does replacement of invalid sequences anyway. Therefore, an extended API doesn't help much, which is why I didn't provide it.Bodigrim commentedon Nov 14, 2021
@vdukhovni Assuming that the stream is incomplete, but not malformed a caller can detect the last complete element in O(1), see https://github.com/haskell/text/blob/7a492ecff429748386dbde7da0db45a0bfb8dcda/src/Data/Text/Encoding.hs#L208-L221