Skip to content

Commit

Permalink
ci: support windows
Browse files Browse the repository at this point in the history
  • Loading branch information
liuq19 committed Aug 9, 2024
1 parent 59d3e65 commit 65fac57
Show file tree
Hide file tree
Showing 19 changed files with 647 additions and 25 deletions.
19 changes: 16 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu, macos]
os: [ubuntu, macos, windows]
timeout-minutes: 45
steps:
- uses: actions/checkout@v4
Expand All @@ -26,7 +26,6 @@ jobs:
- run: cargo test --features arbitrary_precision
- run: cargo test --features sort_keys
- run: ./scripts/run_examples.sh
- run: ./scripts/sanitize.sh

test-stable-self:
name: Rust stable on self-hosted arm
Expand All @@ -46,7 +45,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu, macos]
os: [ubuntu, macos, windows]
timeout-minutes: 45
steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -84,6 +83,20 @@ jobs:
cargo clippy --all-targets --all-features -- -D warnings
cargo fmt -- --check
sanitize:
runs-on: [self-hosted, X64]
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Sanitize
run: ./scripts/sanitize.sh


# check-spelling:
# name: Check spelling
# runs-on: ubuntu-latest
Expand Down
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ parking_lot = "0.12"
page_size = "0.6"


[dev-dependencies]
[target.'cfg(not(target_env = "msvc"))'.dev-dependencies]
jemallocator = "0.5"

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", features = ["float_roundtrip", "raw_value"] }
simd-json = "0.13"
Expand All @@ -40,8 +42,8 @@ gjson = "0.8"
serde_derive = "1.0"
faststr = "0.2"
encoding_rs = "0.8"
# This config will disable rustc-serialize crate to avoid security warnings in ci
json-benchmark = { git = "https://github.com/serde-rs/json-benchmark", default-features = false, features = ["all-files", "lib-serde"]}
# This schema are used in benches and copied from https://github.com/serde-rs/json-benchmark
schema = { path = "benches/schema" }
paste = "1.0"
serde_bytes = "0.11"
bytes = {version = "1.4", features = ["serde"]}
Expand Down
6 changes: 3 additions & 3 deletions benches/deserialize_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ extern crate criterion;
use std::{fs::File, io::Read, str::from_utf8_unchecked};

use criterion::{criterion_group, BatchSize, Criterion, SamplingMode, Throughput};
use schema::{canada::Canada, citm_catalog::CitmCatalog, twitter::Twitter};

#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;

fn serde_json_parse_struct<'de, T>(data: &'de [u8]) -> serde_json::Result<T>
where
Expand Down Expand Up @@ -136,8 +138,6 @@ macro_rules! bench_file {
};
}

use json_benchmark::{canada::Canada, citm_catalog::CitmCatalog, twitter::Twitter};

bench_file!(
json: twitter,
structure: Twitter
Expand Down
3 changes: 2 additions & 1 deletion benches/deserialize_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ use std::{fs::File, io::Read, str::from_utf8_unchecked};

use criterion::{criterion_group, BatchSize, Criterion, SamplingMode, Throughput};

#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;

fn simdjson_to_borrowed_value(data: &mut [u8]) {
let _ = simd_json::to_borrowed_value(data).unwrap();
Expand Down
8 changes: 8 additions & 0 deletions benches/schema/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "schema"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = { version = "1.0", features = ["derive"] }

4 changes: 4 additions & 0 deletions benches/schema/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# JSON Schema

Schema used in benches, copied from `https://github.com/serde-rs/json-benchmark`.
23 changes: 23 additions & 0 deletions benches/schema/licences/LICENSE-json-benchmark
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
41 changes: 41 additions & 0 deletions benches/schema/src/canada.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::collections::BTreeMap as Map;

use serde::{Deserialize, Serialize};

use crate::enum_str;

pub type Canada = FeatureCollection;

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FeatureCollection {
#[serde(rename = "type")]
pub obj_type: ObjType,
pub features: Vec<Feature>,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Feature {
#[serde(rename = "type")]
pub obj_type: ObjType,
pub properties: Map<String, String>,
pub geometry: Geometry,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Geometry {
#[serde(rename = "type")]
pub obj_type: ObjType,
pub coordinates: Vec<Vec<(Latitude, Longitude)>>,
}

pub type Latitude = f32;
pub type Longitude = f32;

enum_str!(ObjType {
FeatureCollection("FeatureCollection"),
Feature("Feature"),
Polygon("Polygon"),
});
73 changes: 73 additions & 0 deletions benches/schema/src/citm_catalog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use std::collections::BTreeMap as Map;

use serde::{Deserialize, Serialize};

use crate::{empty, prim_str::PrimStr};

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct CitmCatalog {
pub area_names: Map<IdStr, String>,
pub audience_sub_category_names: Map<IdStr, String>,
pub block_names: Map<IdStr, String>,
pub events: Map<IdStr, Event>,
pub performances: Vec<Performance>,
pub seat_category_names: Map<IdStr, String>,
pub sub_topic_names: Map<IdStr, String>,
pub subject_names: Map<IdStr, String>,
pub topic_names: Map<IdStr, String>,
pub topic_sub_topics: Map<IdStr, Vec<Id>>,
pub venue_names: Map<String, String>,
}

pub type Id = u32;
pub type IdStr = PrimStr<u32>;

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Event {
pub description: (),
pub id: Id,
pub logo: Option<String>,
pub name: String,
pub sub_topic_ids: Vec<Id>,
pub subject_code: (),
pub subtitle: (),
pub topic_ids: Vec<Id>,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Performance {
pub event_id: Id,
pub id: Id,
pub logo: Option<String>,
pub name: (),
pub prices: Vec<Price>,
pub seat_categories: Vec<SeatCategory>,
pub seat_map_image: (),
pub start: u64,
pub venue_code: String,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Price {
pub amount: u32,
pub audience_sub_category_id: Id,
pub seat_category_id: Id,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct SeatCategory {
pub areas: Vec<Area>,
pub seat_category_id: Id,
}

#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct Area {
pub area_id: Id,
pub block_ids: empty::Array,
}
85 changes: 85 additions & 0 deletions benches/schema/src/color.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use std::{fmt, mem::MaybeUninit, ptr, slice, str};

use serde::{
de::{self, Deserialize, Deserializer, Unexpected},
ser::{Serialize, Serializer},
};

#[derive(Clone, Copy)]
pub struct Color(u32);

const HEX_LUT: &[u8] = b"\
000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F\
202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F\
404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F\
606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F\
808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F\
A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF\
C0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF\
E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF";

impl Color {
fn as_str(self, buf: &mut MaybeUninit<[u8; 6]>) -> &str {
let buf_len = 6;
let buf_ptr = buf.as_mut_ptr() as *mut u8;
let lut_ptr = HEX_LUT.as_ptr();

let r = ((self.0 & 0xFF0000) >> 15) as isize;
let g = ((self.0 & 0x00FF00) >> 7) as isize;
let b = ((self.0 & 0x0000FF) << 1) as isize;

unsafe {
ptr::copy_nonoverlapping(lut_ptr.offset(r), buf_ptr, 2);
ptr::copy_nonoverlapping(lut_ptr.offset(g), buf_ptr.offset(2), 2);
ptr::copy_nonoverlapping(lut_ptr.offset(b), buf_ptr.offset(4), 2);

str::from_utf8(slice::from_raw_parts(buf_ptr, buf_len)).unwrap()
}
}
}

impl Serialize for Color {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut buf = MaybeUninit::uninit();
serializer.serialize_str(self.as_str(&mut buf))
}
}

impl<'de> Deserialize<'de> for Color {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct Visitor;

impl<'de> de::Visitor<'de> for Visitor {
type Value = Color;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("color string")
}

fn visit_str<E>(self, value: &str) -> Result<Color, E>
where
E: de::Error,
{
match u32::from_str_radix(value, 16) {
Ok(hex) => Ok(Color(hex)),
Err(_) => Err(E::invalid_value(Unexpected::Str(value), &self)),
}
}
}

deserializer.deserialize_str(Visitor)
}
}

#[test]
fn test_color() {
let mut buf = MaybeUninit::uninit();
let string = Color(0xA0A0A0).as_str(&mut buf);
assert_eq!(string, "A0A0A0");
}
41 changes: 41 additions & 0 deletions benches/schema/src/empty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::fmt;

use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

#[derive(Clone, Copy)]
pub struct Array;

impl Serialize for Array {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
[(); 0].serialize(serializer)
}
}

impl<'de> Deserialize<'de> for Array {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct Visitor;

impl<'de> de::Visitor<'de> for Visitor {
type Value = Array;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("empty array")
}

fn visit_seq<V>(self, _: V) -> Result<Array, V::Error>
where
V: de::SeqAccess<'de>,
{
Ok(Array)
}
}

deserializer.deserialize_tuple(0, Visitor)
}
}
Loading

0 comments on commit 65fac57

Please sign in to comment.