Skip to content

Commit

Permalink
Merge branch 'ryu'
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcnamara committed Jun 14, 2024
2 parents 9239794 + 819ecdd commit a587aa4
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 10 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/rust_ryu_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Rust - test ryu feature

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Run the tests for the ryu feature set
run: cargo test --features ryu --test '*'
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ js-sys = {version = "0.3.64", optional = true}
wasm-bindgen = {version = "0.2.87", optional = true}
serde = {version = "1.0.193", features = ["derive"], optional = true}
rust_xlsxwriter_derive = {version = "0.2.0", optional = true}
ryu = {version = "1.0.18", optional = true}

[dev-dependencies]
pretty_assertions = "1.3.0"
Expand Down Expand Up @@ -47,6 +48,12 @@ serde = ["dep:serde", "dep:rust_xlsxwriter_derive"]
# `wasm`: Enable wasm/Javascript compilation.
wasm = ["js-sys", "wasm-bindgen"]

# `ryu`: Adds a dependency on `ryu`. This speeds up writing numeric worksheet
# cells for large data files. It gives a performance boost above 300,000 numeric
# cells and can be up to 30% faster than the default number formatting for
# 5,000,000 numeric cells.
ryu = ["dep:ryu"]

# `test-resave`: Developer only testing feature.
test-resave = []

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ frequently.
- `wasm`: Adds a dependency on `js-sys` and `wasm-bindgen` to allow compilation
for wasm/JavaScript targets.

- `ryu`: Adds a dependency on `ryu`. This speeds up writing numeric
worksheet cells for large data files. It gives a performance boost above
300,000 numeric cells and can be up to 30% faster than the default number
formatting for 5,000,000 numeric cells.

## Release notes

Recent changes:
Expand Down
12 changes: 9 additions & 3 deletions examples/app_perf_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
//! Simple performance test for rust_xlsxwriter.
use rust_xlsxwriter::{Workbook, XlsxError};
use std::env;

fn main() -> Result<(), XlsxError> {
let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();
let args: Vec<String> = env::args().collect();

let row_max = 4000;
let col_max = 50;
let row_max = match args.get(1) {
Some(arg) => arg.parse::<u32>().unwrap_or(4_000),
None => 4_000,
};

let mut workbook = Workbook::new();
let worksheet = workbook.add_worksheet();

for row in 0..row_max {
for col in 0..col_max {
Expand Down
12 changes: 9 additions & 3 deletions examples/app_perf_test2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
//! Simple performance test for rust_xlsxwriter.
use rust_xlsxwriter::{Workbook, XlsxError};
use std::env;

fn main() -> Result<(), XlsxError> {
let args: Vec<String> = env::args().collect();

let mut workbook = Workbook::new();

let col_max = 50;
let row_max = match args.get(1) {
Some(arg) => arg.parse::<u32>().unwrap_or(4_000),
None => 4_000,
};

for _ in 0..4 {
let worksheet = workbook.add_worksheet();

let row_max = 4000;
let col_max = 50;

for row in 0..row_max {
for col in 0..col_max {
worksheet.write_string(row, col, "Foo")?;
Expand Down
12 changes: 9 additions & 3 deletions examples/app_perf_test3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
//! Simple performance test for rust_xlsxwriter.
use rust_xlsxwriter::{Workbook, XlsxError};
use std::env;

fn main() -> Result<(), XlsxError> {
let args: Vec<String> = env::args().collect();

let mut workbook = Workbook::new();

let col_max = 50;
let row_max = match args.get(1) {
Some(arg) => arg.parse::<u32>().unwrap_or(4_000),
None => 4_000,
};

for _ in 0..4 {
let worksheet = workbook.add_worksheet();

let row_max = 4000;
let col_max = 50;

for row in 0..row_max {
for col in 0..col_max {
worksheet.write_number(row, col, 12345.0)?;
Expand Down
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@
//! easier to write.
//! - `wasm`: Adds a dependency on `js-sys` and `wasm-bindgen` to allow
//! compilation for wasm/JavaScript targets.
//!
//! - `ryu`: Adds a dependency on `ryu`. This speeds up writing numeric
//! worksheet cells for large data files. It gives a performance boost above
//! 300,000 numeric cells and can be up to 30% faster than the default number
//! formatting for 5,000,000 numeric cells.
//!
mod app;
mod content_types;
Expand Down
9 changes: 9 additions & 0 deletions src/worksheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13668,6 +13668,15 @@ impl Worksheet {
fn write_number_cell(&mut self, row: RowNum, col: ColNum, number: f64, xf_index: u32) {
let col_name = Self::col_to_name(&mut self.col_names, col);

// Use the optional ryu crate to format f64 cell number data as a
// string. Note, the the slightly faster `format_finite()` buffer
// function is safe to use here since nan/inf numbers are filtered out
// at the `store_number()` level and written as strings.
#[cfg(feature = "ryu")]
let mut buffer = ryu::Buffer::new();
#[cfg(feature = "ryu")]
let number = buffer.format_finite(number);

if xf_index > 0 {
write!(
&mut self.writer.xmlfile,
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ fn compare_xlsx_files(
exp_xml_string = DIGITS.replace_all(&exp_xml_string, "").to_string();
}

// The option ryu crate adds a trailing ".0" at the end of integer
// values stored as float, so that 1234 in Excel/std lib becomes 1234.0.
// The following fixes that in the generated file to allow comparison.
#[cfg(feature = "ryu")]
if filename.starts_with("xl/worksheets/sheet") {
got_xml_string = got_xml_string.replace(".0</v>", "</v>");
}

// Convert the xml strings to vectors for easier comparison.
let mut exp_xml_vec;
let mut got_xml_vec;
Expand Down

0 comments on commit a587aa4

Please sign in to comment.