Skip to content

Commit

Permalink
yyjson 0.5.1, build misc
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Jun 23, 2022
1 parent edde952 commit f4b3518
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/manylinux_2_28.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
- run: cargo fetch
- run: maturin build --no-sdist --release --strip --cargo-extra-args="--features=unstable-simd,yyjson" --compatibility manylinux_2_28 --interpreter python${{ matrix.python.version }}
env:
CC: "clang"
CFLAGS: "-O2 -fno-plt -flto=thin"
LDFLAGS: "-O2 -flto=thin -fuse-ld=lld -Wl,--as-needed"
RUSTFLAGS: "-C linker=clang -C link-arg=-fuse-ld=lld"
- run: python3 -m pip install --user target/wheels/orjson*.whl
- run: python3 -m pip install --user -r test/requirements.txt -r integration/requirements.txt
Expand Down
13 changes: 2 additions & 11 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
// SPDX-License-Identifier: (Apache-2.0 OR MIT)

#[cfg(feature = "yyjson")]
extern crate cc;

#[allow(dead_code)]
#[cfg(feature = "yyjson")]
fn build_yyjson() {
// note -march is not automatically propagated by -C target-cpu
cc::Build::new()
.file("include/yyjson.c")
.include("include")
.file("include/yyjson/yyjson.c")
.include("include/yyjson")
.define("YYJSON_DISABLE_WRITER", "1")
.define("YYJSON_DISABLE_NON_STANDARD", "1")
.compiler("clang")
.static_flag(true)
.use_plt(false)
.opt_level_str("2")
.flag("-flto=thin")
.compile("yyjson");
}

Expand Down
8 changes: 8 additions & 0 deletions ci/azure-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ steps:
displayName: test dependencies
- bash: PATH=$HOME/.cargo/bin:$PATH cargo fetch
- bash: PATH=$HOME/.cargo/bin:$PATH maturin build --no-sdist --release --strip --cargo-extra-args="--features=unstable-simd,yyjson" --interpreter $(interpreter)
env:
CC: "clang"
CFLAGS: "-O2 -fno-plt -flto=thin"
LDFLAGS: "-O2 -flto=thin -fuse-ld=lld -Wl,--as-needed"
displayName: build
- bash: pip install target/wheels/orjson*.whl
displayName: install
Expand All @@ -29,6 +33,10 @@ steps:
- bash: rustup target add aarch64-apple-darwin
displayName: rustup target
- bash: PATH=$HOME/.cargo/bin:$PATH PYO3_CROSS_LIB_DIR=$(python -c "import sysconfig;print(sysconfig.get_config_var('LIBDIR'))") maturin build --no-sdist --release --strip --cargo-extra-args="--features=unstable-simd" --interpreter $(interpreter) --universal2
env:
CC: "clang"
CFLAGS: "-O2 -fno-plt -flto=thin"
LDFLAGS: "-O2 -flto=thin -fuse-ld=lld --as-needed,-z,relro,-z,now"
displayName: build universal2
- bash: pip install --force-reinstall target/wheels/orjson*universal2.whl
displayName: install universal2
Expand Down
12 changes: 12 additions & 0 deletions ci/drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ steps:
from_secret: twine_username
MATURIN_PASSWORD:
from_secret: twine_password
CC: "clang"
CFLAGS: "-O2 -fno-plt -flto=thin"
LDFLAGS: "-O2 -flto=thin -fuse-ld=lld -Wl,--as-needed"
RUSTFLAGS: "-C linker=clang -C link-arg=-fuse-ld=lld"
commands:
- yum update -y && yum install -y clang lld
Expand Down Expand Up @@ -40,6 +43,9 @@ steps:
from_secret: twine_username
MATURIN_PASSWORD:
from_secret: twine_password
CC: "clang"
CFLAGS: "-O2 -fno-plt -flto=thin"
LDFLAGS: "-O2 -flto=thin -fuse-ld=lld -Wl,--as-needed"
RUSTFLAGS: "-C linker=clang -C link-arg=-fuse-ld=lld"
commands:
- yum update -y && yum install -y clang lld
Expand Down Expand Up @@ -67,6 +73,9 @@ steps:
from_secret: twine_username
MATURIN_PASSWORD:
from_secret: twine_password
CC: "clang"
CFLAGS: "-O2 -fno-plt -flto=thin"
LDFLAGS: "-O2 -flto=thin -fuse-ld=lld -Wl,--as-needed"
RUSTFLAGS: "-C linker=clang -C link-arg=-fuse-ld=lld"
commands:
- yum update -y && yum install -y clang lld
Expand Down Expand Up @@ -94,6 +103,9 @@ steps:
from_secret: twine_username
MATURIN_PASSWORD:
from_secret: twine_password
CC: "clang"
CFLAGS: "-O2 -fno-plt -flto=thin"
LDFLAGS: "-O2 -flto=thin -fuse-ld=lld -Wl,--as-needed"
RUSTFLAGS: "-C linker=clang -C link-arg=-fuse-ld=lld"
commands:
- yum update -y && yum install -y clang lld
Expand Down
52 changes: 19 additions & 33 deletions include/yyjson.c → include/yyjson/yyjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ static_inline void u128_mul_add(u64 a, u64 b, u64 c, u64 *hi, u64 *lo) {
u64 h, l, t;
u128_mul(a, b, &h, &l);
t = l + c;
h += ((t < l) | (t < c));
h += (u64)(((t < l) | (t < c)));
*hi = h;
*lo = t;
#endif
Expand Down Expand Up @@ -1558,11 +1558,11 @@ static_inline void *pointer_read_arr(const char **ptr,
cur++;
idx = idx * 10 + add;
}
if (cur == hdr) return NULL;
if (cur == hdr || idx >= (u64)USIZE_MAX) return NULL;
*ptr = cur;
return mut
? (void *)yyjson_mut_arr_get(m_arr, idx)
: (void *)yyjson_arr_get(i_arr, idx);
? (void *)yyjson_mut_arr_get(m_arr, (usize)idx)
: (void *)yyjson_arr_get(i_arr, (usize)idx);
}

/**
Expand Down Expand Up @@ -3402,23 +3402,16 @@ static_inline bool read_number(u8 **ptr,

/*
Read integral part, same as the following code.
For more explanation, see the comments under label `skip_ascii_begin`.
for (int i = 1; i <= 18; i++) {
num = cur[i] - '0';
if (num <= 9) sig = num + sig * 10;
else goto digi_sepr_i;
}
*/
#if YYJSON_IS_REAL_GCC
#define expr_intg(i) \
if (likely((num = (u64)(cur[i] - (u8)'0')) <= 9)) sig = num + sig * 10; \
else { __asm volatile("":"=m"(cur[i])::); goto digi_sepr_##i; }
#else
#define expr_intg(i) \
if (likely((num = (u64)(cur[i] - (u8)'0')) <= 9)) sig = num + sig * 10; \
else { goto digi_sepr_##i; }
#endif
repeat_in_1_18(expr_intg);
#undef expr_intg

Expand Down Expand Up @@ -3446,19 +3439,11 @@ static_inline bool read_number(u8 **ptr,


/* read fraction part */
#if YYJSON_IS_REAL_GCC
#define expr_frac(i) \
digi_frac_##i: \
if (likely((num = (u64)(cur[i + 1] - (u8)'0')) <= 9)) \
sig = num + sig * 10; \
else { __asm volatile("":"=m"(cur[i + 1])::); goto digi_stop_##i; }
#else
#define expr_frac(i) \
digi_frac_##i: \
if (likely((num = (u64)(cur[i + 1] - (u8)'0')) <= 9)) \
sig = num + sig * 10; \
else { goto digi_stop_##i; }
#endif
repeat_in_1_18(expr_frac)
#undef expr_frac

Expand Down Expand Up @@ -6020,8 +6005,8 @@ static_inline u64 round_to_odd(u64 hi, u64 lo, u64 cp) {
The output significand is shortest decimal but may have trailing zeros.
This function use the Schubfach algorithm:
Raffaello Giulietti, The Schubfach way to render doubles (4th version), 2021.
https://drive.google.com/file/d/1IEeATSVnEE6TkrHlCYNY2GjaraBjOT4f
Raffaello Giulietti, The Schubfach way to render doubles (5th version), 2022.
https://drive.google.com/file/d/1gp5xv4CAa78SVgCeWfGqqI4FfYYYuNFb
https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-November/083536.html
https://github.com/openjdk/jdk/pull/3402 (Java implementation)
https://github.com/abolz/Drachennest (C++ implementation)
Expand Down Expand Up @@ -6211,6 +6196,7 @@ static_noinline u8 *write_f64_raw(u8 *buf, u64 raw, yyjson_write_flag flg) {
/* write with scientific notation */
/* such as 1.234e56 */
u8 *end = write_u64_len_15_to_17_trim(buf + 1, sig_dec);
end -= (end == buf + 2); /* remove '.0', e.g. 2.0e34 -> 2e134 */
exp_dec += sig_len - 1;
hdr[0] = hdr[1];
hdr[1] = '.';
Expand Down Expand Up @@ -7306,8 +7292,8 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root,
val_begin:
val_type = unsafe_yyjson_get_type(val);
if (val_type == YYJSON_TYPE_STR) {
is_key = ((u8)ctn_obj & (u8)~ctn_len);
no_indent = ((u8)ctn_obj & (u8)ctn_len);
is_key = (bool)((u8)ctn_obj & (u8)~ctn_len);
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
str_len = unsafe_yyjson_get_len(val);
str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
check_str_len(str_len);
Expand All @@ -7320,7 +7306,7 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root,
goto val_end;
}
if (val_type == YYJSON_TYPE_NUM) {
no_indent = ((u8)ctn_obj & (u8)ctn_len);
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
incr_len(32 + (no_indent ? 0 : level * 4));
cur = write_indent(cur, no_indent ? 0 : level);
cur = write_number(cur, val, flg);
Expand All @@ -7331,7 +7317,7 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root,
}
if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) ==
(YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) {
no_indent = ((u8)ctn_obj & (u8)ctn_len);
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
ctn_len_tmp = unsafe_yyjson_get_len(val);
ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ);
if (unlikely(ctn_len_tmp == 0)) {
Expand All @@ -7358,15 +7344,15 @@ static_inline u8 *yyjson_write_pretty(const yyjson_val *root,
}
}
if (val_type == YYJSON_TYPE_BOOL) {
no_indent = ((u8)ctn_obj & (u8)ctn_len);
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
incr_len(16 + (no_indent ? 0 : level * 4));
cur = write_indent(cur, no_indent ? 0 : level);
cur = write_bool(cur, unsafe_yyjson_get_bool(val));
cur += 2;
goto val_end;
}
if (val_type == YYJSON_TYPE_NULL) {
no_indent = ((u8)ctn_obj & (u8)ctn_len);
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
incr_len(16 + (no_indent ? 0 : level * 4));
cur = write_indent(cur, no_indent ? 0 : level);
cur = write_null(cur);
Expand Down Expand Up @@ -7794,8 +7780,8 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root,
val_begin:
val_type = unsafe_yyjson_get_type(val);
if (val_type == YYJSON_TYPE_STR) {
is_key = ((u8)ctn_obj & (u8)~ctn_len);
no_indent = ((u8)ctn_obj & (u8)ctn_len);
is_key = (bool)((u8)ctn_obj & (u8)~ctn_len);
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
str_len = unsafe_yyjson_get_len(val);
str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
check_str_len(str_len);
Expand All @@ -7808,7 +7794,7 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root,
goto val_end;
}
if (val_type == YYJSON_TYPE_NUM) {
no_indent = ((u8)ctn_obj & (u8)ctn_len);
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
incr_len(32 + (no_indent ? 0 : level * 4));
cur = write_indent(cur, no_indent ? 0 : level);
cur = write_number(cur, (yyjson_val *)val, flg);
Expand All @@ -7819,7 +7805,7 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root,
}
if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) ==
(YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) {
no_indent = ((u8)ctn_obj & (u8)ctn_len);
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
ctn_len_tmp = unsafe_yyjson_get_len(val);
ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ);
if (unlikely(ctn_len_tmp == 0)) {
Expand Down Expand Up @@ -7848,15 +7834,15 @@ static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root,
}
}
if (val_type == YYJSON_TYPE_BOOL) {
no_indent = ((u8)ctn_obj & (u8)ctn_len);
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
incr_len(16 + (no_indent ? 0 : level * 4));
cur = write_indent(cur, no_indent ? 0 : level);
cur = write_bool(cur, unsafe_yyjson_get_bool(val));
cur += 2;
goto val_end;
}
if (val_type == YYJSON_TYPE_NULL) {
no_indent = ((u8)ctn_obj & (u8)ctn_len);
no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
incr_len(16 + (no_indent ? 0 : level * 4));
cur = write_indent(cur, no_indent ? 0 : level);
cur = write_null(cur);
Expand Down
8 changes: 4 additions & 4 deletions include/yyjson.h → include/yyjson/yyjson.h
Original file line number Diff line number Diff line change
Expand Up @@ -409,13 +409,13 @@ extern "C" {
#define YYJSON_VERSION_MINOR 5

/** The patch version of yyjson. */
#define YYJSON_VERSION_PATCH 0
#define YYJSON_VERSION_PATCH 1

/** The version of yyjson in hex: (major << 16) | (minor << 8) | (patch). */
#define YYJSON_VERSION_HEX 0x000500
#define YYJSON_VERSION_HEX 0x000501

/** The version string of yyjson. */
#define YYJSON_VERSION_STRING "0.5.0"
#define YYJSON_VERSION_STRING "0.5.1"

/** The version of yyjson in hex, same as `YYJSON_VERSION_HEX`. */
yyjson_api uint32_t yyjson_version(void);
Expand Down Expand Up @@ -779,7 +779,7 @@ yyjson_api_inline yyjson_doc *yyjson_read(const char *dat,
@endcode
@see yyjson_alc_pool_init()
*/
yyjson_api size_t yyjson_read_max_memory_usage(size_t len,
yyjson_api_inline size_t yyjson_read_max_memory_usage(size_t len,
yyjson_read_flag flg) {
/*
1. The max value count is (json_size / 2 + 1),
Expand Down
3 changes: 3 additions & 0 deletions script/develop
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

rm -f target/wheels/*

export CC="clang"
export CFLAGS="-O2 -fno-plt -flto=thin"
export LDFLAGS="-O2 -flto=thin -fuse-ld=lld -Wl,--as-needed"
export RUSTFLAGS="-C linker=clang -C link-arg=-fuse-ld=lld"

maturin build --no-sdist --compatibility off -i python3 --release "$@"
Expand Down
2 changes: 1 addition & 1 deletion script/generate-yyjson
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -eou pipefail
_repo="$(dirname "$(dirname "${BASH_SOURCE[0]}")")"

bindgen \
"${_repo}/include/yyjson.h" \
"${_repo}/include/yyjson/yyjson.h" \
--size_t-is-usize \
--disable-header-comment \
--no-derive-debug \
Expand Down
4 changes: 2 additions & 2 deletions src/deserialize/yyjson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn unsafe_yyjson_get_next(val: *mut yyjson_val) -> *mut yyjson_val {
}
}

fn yyjson_arr_iter_next(iter: *mut yyjson_arr_iter) -> *mut yyjson_val {
fn yyjson_arr_iter_next(iter: &mut yyjson_arr_iter) -> *mut yyjson_val {
unsafe {
let val = (*iter).cur;
(*iter).cur = unsafe_yyjson_get_next(val);
Expand All @@ -74,7 +74,7 @@ fn yyjson_arr_iter_next(iter: *mut yyjson_arr_iter) -> *mut yyjson_val {
}
}

fn yyjson_obj_iter_next(iter: *mut yyjson_obj_iter) -> *mut yyjson_val {
fn yyjson_obj_iter_next(iter: &mut yyjson_obj_iter) -> *mut yyjson_val {
unsafe {
let key = (*iter).cur;
(*iter).cur = unsafe_yyjson_get_next(key.add(1));
Expand Down

0 comments on commit f4b3518

Please sign in to comment.