You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fury.apache.org by "urlyy (via GitHub)" <gi...@apache.org> on 2024/07/29 17:21:02 UTC
[PR] feat(rust): support simd approach in converting utf16 to utf8 [fury]
urlyy opened a new pull request, #1778:
URL: https://github.com/apache/fury/pull/1778
## What does this PR do?
For the conversion from UTF-16 to UTF-8, a SIMD method based on AVX/SSE/NEON instruction sets was added on the basis of #1730 , and benchmarks were written.
referencing
- https://github.com/simdutf/simdutf/blob/master/src/westmere/sse_convert_utf16_to_utf8.cpp
- https://github.com/simdutf/simdutf/blob/master/src/haswell/avx2_convert_utf16_to_utf8.cpp
- https://github.com/simdutf/simdutf/blob/5c1a86887010cd2b4d648049c4d73de81a026341/src/arm64/arm_convert_utf16_to_utf8.cpp
- https://github.com/simdutf/simdutf/blob/master/src/tables/utf16_to_utf8_tables.h
Notice:
- I use two precomputing table , as same as what have done in `simdutf`. But it takes 1600 lines.
- I copied two utf8 encoded text file into rust project for benchmark.
- `util.rs` might need to be merged with `string_util.rs`
## Related issues
- #1547
- #1730
## Does this PR introduce any user-facing change?
- [x] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
dataset from https://github.com/lemire/unicode_lipsum/tree/main/wikipedia_mars
Both SIMD and non-SIMD approach are faster than using `String::from_utf16(bytes)`.In my win11 x86 machine benchmark , SIMD approach seems to be approximately only a little faster than normal approach , that is out of my expectation. AVX seems better than SSE because AVX handle 256bit at one time but SSE onlyt handle 128 bits at one time. When handling with surrogate pair, algorithm will use fall_back (normal, without SIMD) way, in this case simd approach might be worse than normal way.

--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support simd approach in converting utf16 to utf8 [fury]
Posted by "kitty-eu-org (via GitHub)" <gi...@apache.org>.
kitty-eu-org commented on PR #1778:
URL: https://github.com/apache/fury/pull/1778#issuecomment-2258041718
@urlyy I'm optimizing simd code for SSE2 and AVX
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support simd approach in converting utf16 to utf8 [fury]
Posted by "urlyy (via GitHub)" <gi...@apache.org>.
urlyy commented on PR #1778:
URL: https://github.com/apache/fury/pull/1778#issuecomment-2258055379
> @urlyy neon I referred to part of your implementation, thank you
I'm new to SIMD, and my implementation is referenced from `simdutf`. I'd love to see further optimizations for this part of the code and learn something new from you🤓
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support simd approach in converting utf16 to utf8 [fury]
Posted by "urlyy (via GitHub)" <gi...@apache.org>.
urlyy commented on code in PR #1778:
URL: https://github.com/apache/fury/pull/1778#discussion_r1696289058
##########
rust/fury/benches/simd_utf16_to_utf8.rs:
##########
@@ -0,0 +1,100 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#[cfg(target_arch = "aarch64")]
+use std::arch::is_aarch64_feature_detected;
+use std::{env, fs};
+
+use criterion::{black_box, criterion_group, criterion_main, Criterion};
+use fury::{utf16_to_utf8, utf16_to_utf8_simd};
+
+#[cfg(target_endian = "big")]
+const IS_LITTLE_ENDIAN_LOCAL: bool = false;
+
+#[cfg(target_endian = "little")]
+const IS_LITTLE_ENDIAN_LOCAL: bool = true;
+
+fn criterion_benchmark(c: &mut Criterion) {
+ let current_dir = env::current_dir()
+ .expect("Failed to get current directory")
+ .join("benches");
+ let path1 = current_dir.join("chinese.utf8.txt");
Review Comment:
OK, I'll replace it with some randomly generated string
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support simd approach in converting utf16 to utf8 [fury]
Posted by "kitty-eu-org (via GitHub)" <gi...@apache.org>.
kitty-eu-org commented on PR #1778:
URL: https://github.com/apache/fury/pull/1778#issuecomment-2258027181
@urlyy I wrote a rust version, the neon performance improvement is about twice as good, we can optimize it together
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support simd approach in converting utf16 to utf8 [fury]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on PR #1778:
URL: https://github.com/apache/fury/pull/1778#issuecomment-2257408851
This is too huge, do we have a better way to implement it @theweipeng @kitty-eu-org
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support simd approach in converting utf16 to utf8 [fury]
Posted by "urlyy (via GitHub)" <gi...@apache.org>.
urlyy commented on PR #1778:
URL: https://github.com/apache/fury/pull/1778#issuecomment-2257487497
> This is too huge, do we have a better way to implement it @theweipeng @kitty-eu-org
I also think about it. Actually I have seen the CPP version implementation: https://github.com/apache/fury/pull/1732/files , but the SIMD API seems only used at swap endian and `checking` whether all utf16 of a chunk are all can be converted to 1 bytes utf8, and then just use `loop` to convert the utf16 bytes in a chunk one by one. I don't think it has a good use of SIMD.
```
// cpp version
if (_mm256_testz_si256(mask1, mask1)) {
// All values < 0x80, 1 byte per character
for (int j = 0; j < 16; ++j) {
*output++ = static_cast<char>(utf16[i + j]);
}
}
```
rust version
```
// check chunk's all u16 less than 0x80
if _mm256_testz_si256(chunk, *M_FF80) != 0 {
let utf8_packed = _mm_packus_epi16(
_mm256_castsi256_si128(chunk),
_mm256_extractf128_si256(chunk, 1),
);
_mm_storeu_si128(ptr_8.add(offset_8) as *mut __m128i, utf8_packed);
offset_8 += CHUNK_UTF16_USAGE;
offset_16 += CHUNK_UTF16_USAGE;
continue;
}
```
But it is easy to shorten the code . Just remove some functions. And I just write a short-version like below. Benchmark data is also as follows.
```rust
while offset_16 + CHUNK_UTF16_USAGE <= len_16 {
let mut chunk = _mm256_loadu_si256(ptr_16.add(offset_16) as *const __m256i);
chunk = if is_little_endian == super::super::IS_LITTLE_ENDIAN_LOCAL {
chunk
} else {
_mm256_shuffle_epi8(chunk, *ENDIAN_SWAP_MASK)
};
let mask1 = _mm256_cmpgt_epi16(chunk, *limit1);
// check chunk's all u16 less than 0x80 ,1 utf16 -> 1utf8
if _mm256_testz_si256(mask1, mask1) != 0 {
let utf8_packed = _mm_packus_epi16(
_mm256_castsi256_si128(chunk),
_mm256_extractf128_si256(chunk, 1),
);
_mm_storeu_si128(ptr_8.add(offset_8) as *mut __m128i, utf8_packed);
offset_8 += CHUNK_UTF16_USAGE;
offset_16 += CHUNK_UTF16_USAGE;
continue;
}
// when has some utf16 can convert to 2/3/4 utf8 bytes
let res = call_fallback(
ptr_16,
ptr_8,
&mut offset_16,
&mut offset_8,
len_16,
is_little_endian,
);
if let Some(err_msg) = res {
return Err(err_msg);
}
}
```

--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support simd approach in converting utf16 to utf8 [fury]
Posted by "urlyy (via GitHub)" <gi...@apache.org>.
urlyy closed pull request #1778: feat(rust): support simd approach in converting utf16 to utf8
URL: https://github.com/apache/fury/pull/1778
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support simd approach in converting utf16 to utf8 [fury]
Posted by "chaokunyang (via GitHub)" <gi...@apache.org>.
chaokunyang commented on code in PR #1778:
URL: https://github.com/apache/fury/pull/1778#discussion_r1696247553
##########
rust/fury/benches/simd_utf16_to_utf8.rs:
##########
@@ -0,0 +1,100 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#[cfg(target_arch = "aarch64")]
+use std::arch::is_aarch64_feature_detected;
+use std::{env, fs};
+
+use criterion::{black_box, criterion_group, criterion_main, Criterion};
+use fury::{utf16_to_utf8, utf16_to_utf8_simd};
+
+#[cfg(target_endian = "big")]
+const IS_LITTLE_ENDIAN_LOCAL: bool = false;
+
+#[cfg(target_endian = "little")]
+const IS_LITTLE_ENDIAN_LOCAL: bool = true;
+
+fn criterion_benchmark(c: &mut Criterion) {
+ let current_dir = env::current_dir()
+ .expect("Failed to get current directory")
+ .join("benches");
+ let path1 = current_dir.join("chinese.utf8.txt");
Review Comment:
Seems it's only used here, could we use a smaller test data? I think two or three string line of literal is enough for benchmark
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org
Re: [PR] feat(rust): support simd approach in converting utf16 to utf8 [fury]
Posted by "kitty-eu-org (via GitHub)" <gi...@apache.org>.
kitty-eu-org commented on PR #1778:
URL: https://github.com/apache/fury/pull/1778#issuecomment-2258046497
@urlyy neon I referred to part of your implementation, thank you
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fury.apache.org
For additional commands, e-mail: commits-help@fury.apache.org