Skip to content

Commit 5242cea

Browse files
authored
chore: cleanup codes (#33)
1 parent f472ff8 commit 5242cea

File tree

6 files changed

+229
-104
lines changed

6 files changed

+229
-104
lines changed

.github/workflows/CI.yml

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
- uses: actions/setup-node@v5
3636
with:
3737
node-version: 22
38-
cache: 'yarn'
38+
cache: "yarn"
3939
- name: Install dependencies
4040
run: yarn install
4141
- name: Download fixtures
@@ -58,7 +58,7 @@ jobs:
5858
- uses: actions/setup-node@v5
5959
with:
6060
node-version: 22
61-
cache: 'yarn'
61+
cache: "yarn"
6262
- name: Install dependencies
6363
run: yarn install
6464
- name: Download fixtures
@@ -69,6 +69,110 @@ jobs:
6969
run: cargo miri test
7070
env:
7171
MIRIFLAGS: "-Zmiri-disable-isolation"
72+
73+
asan:
74+
name: ASAN - Linux-x86_64 - ${{ matrix.asan.flag }}
75+
runs-on: ubuntu-24.04
76+
strategy:
77+
fail-fast: false
78+
matrix:
79+
asan:
80+
- flag: sanitizer=address
81+
options: detect_leaks=1 detect_stack_use_after_return=1
82+
- flag: sanitizer=memory
83+
options: ""
84+
- flag: sanitizer=safestack
85+
options: ""
86+
87+
steps:
88+
- uses: actions/checkout@v5
89+
90+
- name: Setup node
91+
uses: actions/setup-node@v5
92+
with:
93+
node-version: 22
94+
cache: "yarn"
95+
96+
- name: Install Rust
97+
uses: dtolnay/rust-toolchain@stable
98+
with:
99+
toolchain: nightly
100+
components: rust-src
101+
102+
- name: Install dependencies
103+
run: yarn install --immutable --mode=skip-build
104+
105+
- name: Download fixtures
106+
run: node download-fixtures.js
107+
env:
108+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
109+
110+
- name: Test with ASAN
111+
run: cargo test --tests --target x86_64-unknown-linux-gnu
112+
env:
113+
RUST_TARGET: x86_64-unknown-linux-gnu
114+
RUST_BACKTRACE: 1
115+
RUSTFLAGS: "-Z${{ matrix.asan.flag }}"
116+
ASAN_OPTIONS: ${{ matrix.asan.options }}
117+
CARGO_UNSTABLE_BUILD_STD: std,panic_abort
118+
119+
120+
asan-win32:
121+
name: ASAN - Windows-x86_64
122+
runs-on: windows-latest
123+
124+
steps:
125+
- uses: actions/checkout@v5
126+
127+
- name: Setup node
128+
uses: actions/setup-node@v5
129+
with:
130+
node-version: 22
131+
cache: "yarn"
132+
133+
- name: Install Rust
134+
uses: dtolnay/rust-toolchain@stable
135+
with:
136+
toolchain: nightly
137+
components: rust-src
138+
139+
- name: Install dependencies
140+
run: yarn install --immutable --mode=skip-build
141+
142+
- name: Download fixtures
143+
run: node download-fixtures.js
144+
env:
145+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
146+
147+
- name: Test with ASAN (Windows)
148+
shell: pwsh
149+
run: |
150+
# Set ASAN environment variables for Windows
151+
$env:ASAN_OPTIONS = "windows_hook_rtl_allocators=true:detect_leaks=0:print_stats=1:check_initialization_order=true:strict_string_checks=true"
152+
153+
# Find and set the path to the ASAN runtime DLL
154+
$vsPath = & "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath
155+
$asanDllPath = Get-ChildItem -Path "$vsPath\VC\Tools\MSVC" -Recurse -Filter "clang_rt.asan_dynamic-x86_64.dll" | Select-Object -First 1
156+
if ($asanDllPath) {
157+
$env:PATH = "$($asanDllPath.DirectoryName);$env:PATH"
158+
Write-Host "Found ASAN DLL at: $($asanDllPath.FullName)"
159+
}
160+
cargo test --tests --target x86_64-pc-windows-msvc
161+
env:
162+
RUSTFLAGS: -Zsanitizer=address
163+
RUST_BACKTRACE: 1
164+
CARGO_PROFILE_DEV_OPT_LEVEL: 1
165+
CARGO_UNSTABLE_BUILD_STD: std,panic_abort
166+
167+
- name: Upload ASAN logs (Windows)
168+
if: failure()
169+
uses: actions/upload-artifact@v4
170+
with:
171+
name: windows-asan-logs
172+
path: |
173+
asan.log*
174+
*.asan.log
175+
72176
bench:
73177
strategy:
74178
matrix:
@@ -94,7 +198,7 @@ jobs:
94198
- uses: actions/setup-node@v5
95199
with:
96200
node-version: 22
97-
cache: 'yarn'
201+
cache: "yarn"
98202
- name: Install dependencies
99203
run: yarn install
100204
- name: Download fixtures
@@ -104,4 +208,11 @@ jobs:
104208
- name: Run benchmarks
105209
run: cargo bench
106210
env:
107-
RUSTFLAGS: '-C target-cpu=native'
211+
RUSTFLAGS: "-C target-cpu=native"
212+
213+
done:
214+
runs-on: ubuntu-latest
215+
needs: [test, miri, asan, asan-win32, bench]
216+
steps:
217+
- run: exit 1
218+
if: ${{ always() && (contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')) }}

src/simd/avx2.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -220,25 +220,27 @@ pub unsafe fn format_string(value: &str, dst: &mut [u8]) -> usize {
220220
// Handle remaining bytes
221221
let mut placeholder: [u8; LANES] = [0; LANES];
222222
while nb > 0 {
223-
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
224223
let v = {
225-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
226-
Simd256u::loadu(placeholder.as_ptr())
227-
};
228-
#[cfg(any(target_os = "linux", target_os = "macos"))]
229-
let v = {
230-
if check_cross_page(sptr, LANES) {
231-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
232-
Simd256u::loadu(placeholder.as_ptr())
233-
} else {
234-
#[cfg(any(debug_assertions, miri))]
235-
{
236-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
237-
Simd256u::loadu(placeholder.as_ptr())
238-
}
239-
#[cfg(not(any(debug_assertions, miri)))]
240-
{
241-
Simd256u::loadu(sptr)
224+
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
225+
{
226+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
227+
Simd256u::loadu(placeholder[..].as_ptr())
228+
}
229+
#[cfg(any(target_os = "linux", target_os = "macos"))]
230+
{
231+
if check_cross_page(sptr, LANES) {
232+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
233+
Simd256u::loadu(placeholder[..].as_ptr())
234+
} else {
235+
#[cfg(any(debug_assertions, miri))]
236+
{
237+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
238+
Simd256u::loadu(placeholder[..].as_ptr())
239+
}
240+
#[cfg(not(any(debug_assertions, miri)))]
241+
{
242+
Simd256u::loadu(sptr)
243+
}
242244
}
243245
}
244246
};

src/simd/avx512.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -212,25 +212,27 @@ pub unsafe fn format_string(value: &str, dst: &mut [u8]) -> usize {
212212
// Handle remaining bytes
213213
let mut placeholder: [u8; LANES] = [0; LANES];
214214
while nb > 0 {
215-
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
216215
let v = {
217-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
218-
Simd512u::loadu(placeholder.as_ptr())
219-
};
220-
#[cfg(any(target_os = "linux", target_os = "macos"))]
221-
let v = {
222-
if check_cross_page(sptr, LANES) {
223-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
224-
Simd512u::loadu(placeholder.as_ptr())
225-
} else {
226-
#[cfg(any(debug_assertions, miri))]
227-
{
228-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
229-
Simd512u::loadu(placeholder.as_ptr())
230-
}
231-
#[cfg(not(any(debug_assertions, miri)))]
232-
{
233-
Simd512u::loadu(sptr)
216+
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
217+
{
218+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
219+
Simd512u::loadu(placeholder[..].as_ptr())
220+
}
221+
#[cfg(any(target_os = "linux", target_os = "macos"))]
222+
{
223+
if check_cross_page(sptr, LANES) {
224+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
225+
Simd512u::loadu(placeholder[..].as_ptr())
226+
} else {
227+
#[cfg(any(debug_assertions, miri))]
228+
{
229+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
230+
Simd512u::loadu(placeholder[..].as_ptr())
231+
}
232+
#[cfg(not(any(debug_assertions, miri)))]
233+
{
234+
Simd512u::loadu(sptr)
235+
}
234236
}
235237
}
236238
};

src/simd/neon.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -219,27 +219,29 @@ pub unsafe fn format_string(value: &str, dst: &mut [u8]) -> usize {
219219
}
220220

221221
// Handle remaining bytes
222-
let mut placeholder: [u8; 16] = [0; 16];
222+
let mut placeholder: [u8; LANES] = [0; LANES];
223223
while nb > 0 {
224-
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
225224
let v = {
226-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
227-
Simd128u::loadu(placeholder.as_ptr())
228-
};
229-
#[cfg(any(target_os = "linux", target_os = "macos"))]
230-
let v = {
231-
if check_cross_page(sptr, LANES) {
232-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
233-
Simd128u::loadu(placeholder.as_ptr())
234-
} else {
235-
#[cfg(any(debug_assertions, miri))]
236-
{
237-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
238-
Simd128u::loadu(placeholder.as_ptr())
239-
}
240-
#[cfg(not(any(debug_assertions, miri)))]
241-
{
242-
Simd128u::loadu(sptr)
225+
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
226+
{
227+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
228+
Simd128u::loadu(placeholder[..].as_ptr())
229+
}
230+
#[cfg(any(target_os = "linux", target_os = "macos"))]
231+
{
232+
if check_cross_page(sptr, LANES) {
233+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
234+
Simd128u::loadu(placeholder[..].as_ptr())
235+
} else {
236+
#[cfg(any(debug_assertions, miri))]
237+
{
238+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
239+
Simd128u::loadu(placeholder[..].as_ptr())
240+
}
241+
#[cfg(not(any(debug_assertions, miri)))]
242+
{
243+
Simd128u::loadu(sptr)
244+
}
243245
}
244246
}
245247
};

src/simd/sse2.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -215,27 +215,29 @@ pub unsafe fn format_string(value: &str, dst: &mut [u8]) -> usize {
215215
}
216216

217217
// Handle remaining bytes
218-
let mut placeholder: [u8; 16] = [0; 16];
218+
let mut placeholder: [u8; LANES] = [0; LANES];
219219
while nb > 0 {
220-
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
221220
let v = {
222-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
223-
Simd128u::loadu(placeholder.as_ptr())
224-
};
225-
#[cfg(any(target_os = "linux", target_os = "macos"))]
226-
let v = {
227-
if check_cross_page(sptr, LANES) {
228-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
229-
Simd128u::loadu(placeholder.as_ptr())
230-
} else {
231-
#[cfg(any(debug_assertions, miri))]
232-
{
233-
std::ptr::copy_nonoverlapping(sptr, placeholder.as_mut_ptr(), nb);
234-
Simd128u::loadu(placeholder.as_ptr())
235-
}
236-
#[cfg(not(any(debug_assertions, miri)))]
237-
{
238-
Simd128u::loadu(sptr)
221+
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
222+
{
223+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
224+
Simd128u::loadu(placeholder[..].as_ptr())
225+
}
226+
#[cfg(any(target_os = "linux", target_os = "macos"))]
227+
{
228+
if check_cross_page(sptr, LANES) {
229+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
230+
Simd128u::loadu(placeholder[..].as_ptr())
231+
} else {
232+
#[cfg(any(debug_assertions, miri))]
233+
{
234+
std::ptr::copy_nonoverlapping(sptr, placeholder[..].as_mut_ptr(), nb);
235+
Simd128u::loadu(placeholder[..].as_ptr())
236+
}
237+
#[cfg(not(any(debug_assertions, miri)))]
238+
{
239+
Simd128u::loadu(sptr)
240+
}
239241
}
240242
}
241243
};
@@ -247,7 +249,7 @@ pub unsafe fn format_string(value: &str, dst: &mut [u8]) -> usize {
247249
dptr = dptr.add(nb);
248250
break;
249251
} else {
250-
let cn = mask.trailing_zeros() as usize;
252+
let cn = mask.first_offset();
251253
nb -= cn;
252254
dptr = dptr.add(cn);
253255
sptr = sptr.add(cn);

0 commit comments

Comments
 (0)