Skip to content

Commit 0f2e88d

Browse files
committed
Refactor imports to use macros for static casts and improve code organization
Signed-off-by: Heinz N. Gies <[email protected]>
1 parent f982788 commit 0f2e88d

File tree

16 files changed

+38
-57
lines changed

16 files changed

+38
-57
lines changed

src/impls/avx2/deser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use arch::{
1212
use crate::{
1313
Deserializer, Result, SillyWrapper,
1414
error::ErrorType,
15+
macros::static_cast_u32,
1516
safer_unchecked::GetSaferUnchecked,
1617
stringparse::{ESCAPE_MAP, handle_unicode_codepoint},
1718
};

src/impls/avx2/stage1.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#![allow(dead_code)]
2-
use crate::{Stage1Parse, static_cast_i32, static_cast_i64, static_cast_u32};
2+
use crate::{
3+
Stage1Parse,
4+
macros::{static_cast_i32, static_cast_i64, static_cast_u32},
5+
};
36
#[cfg(target_arch = "x86")]
47
use std::arch::x86 as arch;
58

src/impls/native/stage1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(clippy::cast_lossless, clippy::cast_sign_loss)]
22

3-
use crate::{Stage1Parse, static_cast_i32};
3+
use crate::{Stage1Parse, macros::static_cast_i32};
44

55
type V128 = [u8; 16];
66

src/impls/neon/stage1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Stage1Parse, static_cast_i32};
1+
use crate::Stage1Parse;
22
use std::arch::aarch64::{
33
int8x16_t, int32x4_t, uint8x16_t, vaddq_s32, vandq_u8, vceqq_u8, vcleq_u8, vdupq_n_s8,
44
vgetq_lane_u64, vld1q_u8, vmovq_n_u8, vpaddq_u8, vqtbl1q_u8, vreinterpretq_u8_s8,

src/impls/portable/stage1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::simd::{ToBitMask, prelude::*};
22

3-
use crate::{Stage1Parse, static_cast_i32};
3+
use crate::{Stage1Parse, macros::static_cast_i32};
44
#[derive(Debug)]
55
pub(crate) struct SimdInput {
66
v: u8x64,

src/impls/sse42/stage1.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::{Stage1Parse, static_cast_i32, static_cast_u32};
1+
use crate::{
2+
Stage1Parse,
3+
macros::{static_cast_i32, static_cast_i64, static_cast_u32},
4+
};
25
#[cfg(target_arch = "x86")]
36
use std::arch::x86 as arch;
47

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ mod numberparse;
4444
mod safer_unchecked;
4545
mod stringparse;
4646

47+
use macros::static_cast_u64;
4748
use safer_unchecked::GetSaferUnchecked;
4849
use stage2::StackState;
4950
use tape::Value;
@@ -54,9 +55,9 @@ mod impls;
5455
pub mod cow;
5556

5657
/// The maximum padding size required by any SIMD implementation
57-
pub const SIMDJSON_PADDING: usize = 32; // take upper limit mem::size_of::<__m256i>()
58+
pub(crate) const SIMDJSON_PADDING: usize = 32; // take upper limit mem::size_of::<__m256i>()
5859
/// It's 64 for all (Is this correct?)
59-
pub const SIMDINPUT_LENGTH: usize = 64;
60+
pub(crate) const SIMDINPUT_LENGTH: usize = 64;
6061

6162
mod stage2;
6263
/// simd-json JSON-DOM value

src/macros.rs

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,48 +1191,16 @@ macro_rules! json_unexpected {
11911191
() => {};
11921192
}
11931193

1194-
/// possible compiler hint that a branch is likely
1195-
#[cfg(feature = "hints")]
1196-
#[macro_export]
1197-
macro_rules! likely {
1198-
($e:expr) => {
1199-
::std::intrinsics::likely($e)
1200-
};
1201-
}
1202-
12031194
/// possible compiler hint that a branch is unlikely
12041195
#[cfg(feature = "hints")]
1205-
#[macro_export]
12061196
macro_rules! unlikely {
12071197
($e:expr) => {{ ::std::intrinsics::unlikely($e) }};
12081198
}
12091199

1210-
/// possible compiler hint that a branch is likely
1211-
///
1212-
/// Technique borrowed from here: <https://github.com/rust-lang/hashbrown/pull/209>
1213-
#[cfg(not(feature = "hints"))]
1214-
#[macro_export]
1215-
macro_rules! likely {
1216-
($e:expr_2021) => {{
1217-
#[inline]
1218-
#[cold]
1219-
fn cold() {}
1220-
1221-
let cond = $e;
1222-
1223-
if !cond {
1224-
cold();
1225-
}
1226-
1227-
cond
1228-
}};
1229-
}
1230-
12311200
/// possible compiler hint that a branch is unlikely
12321201
///
12331202
/// Technique borrowed from here: <https://github.com/rust-lang/hashbrown/pull/209>
12341203
#[cfg(not(feature = "hints"))]
1235-
#[macro_export]
12361204
macro_rules! unlikely {
12371205
($e:expr_2021) => {{
12381206
#[inline]
@@ -1249,61 +1217,60 @@ macro_rules! unlikely {
12491217
}};
12501218
}
12511219

1252-
/// static cast to an i8
1253-
#[macro_export]
1254-
macro_rules! static_cast_i8 {
1255-
($v:expr_2021) => {
1256-
::std::transmute::<_, i8>($v)
1257-
};
1258-
}
1220+
pub(crate) use unlikely;
12591221

12601222
/// static cast to an i32
1261-
#[macro_export]
1223+
#[allow(unused_macros)]
12621224
macro_rules! static_cast_i32 {
12631225
($v:expr_2021) => {
12641226
::std::mem::transmute::<u32, i32>($v)
12651227
};
12661228
}
1229+
#[allow(unused_imports)]
1230+
pub(crate) use static_cast_i32;
12671231

12681232
/// static cast to an u32
1269-
#[macro_export]
1233+
#[allow(unused_macros)]
12701234
macro_rules! static_cast_u32 {
12711235
($v:expr_2021) => {
12721236
// #[allow(clippy::missing_transmute_annotations)]
12731237
::std::mem::transmute::<i32, u32>($v)
12741238
};
12751239
}
1240+
#[allow(unused_imports)]
1241+
pub(crate) use static_cast_u32;
12761242

12771243
/// static cast to an i64
1278-
#[macro_export]
12791244
macro_rules! static_cast_i64 {
12801245
($v:expr_2021) => {
12811246
::std::mem::transmute::<u64, i64>($v)
12821247
};
12831248
}
1249+
pub(crate) use static_cast_i64;
12841250

12851251
/// static cast to an i64
1286-
#[macro_export]
1252+
#[cfg(all(feature = "approx-number-parsing", feature = "i128"))]
12871253
macro_rules! static_cast_i128 {
12881254
($v:expr_2021) => {
12891255
::std::mem::transmute::<_, i128>($v)
12901256
};
12911257
}
1258+
#[cfg(all(feature = "approx-number-parsing", feature = "i128"))]
1259+
pub(crate) use static_cast_i128;
12921260

12931261
/// static cast to an u64
1294-
#[macro_export]
12951262
macro_rules! static_cast_u64 {
12961263
($v:expr_2021) => {
12971264
::std::mem::transmute::<i64, u64>($v)
12981265
};
12991266
}
1267+
pub(crate) use static_cast_u64;
13001268

13011269
/// Custom `try!` macro that does no `From` conversions
13021270
///
13031271
/// FROM serde-json
13041272
/// We only use our own error type; no need for From conversions provided by the
13051273
/// standard library's try! macro. This reduces lines of LLVM IR by 4%.
1306-
#[macro_export]
13071274
macro_rules! stry {
13081275
($e:expr_2021) => {
13091276
match $e {
@@ -1312,6 +1279,8 @@ macro_rules! stry {
13121279
}
13131280
};
13141281
}
1282+
#[allow(unused_imports)]
1283+
pub(crate) use stry;
13151284

13161285
#[cfg(test)]
13171286
mod test {

src/numberparse/approx.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::{
44
};
55
use crate::StaticNode;
66
use crate::charutils::is_structural_or_whitespace;
7+
use crate::macros::{static_cast_i64, unlikely};
78
use crate::safer_unchecked::GetSaferUnchecked;
89
use crate::{Deserializer, ErrorType, Result};
910

src/numberparse/correct.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::{is_made_of_eight_digits_fast, parse_eight_digits_unrolled};
99
use crate::StaticNode;
1010
use crate::charutils::is_structural_or_whitespace;
1111
use crate::error::Error;
12+
use crate::macros::{static_cast_i64, unlikely};
1213
use crate::safer_unchecked::GetSaferUnchecked;
1314
use crate::{Deserializer, ErrorType, Result};
1415

0 commit comments

Comments
 (0)