Skip to content

Commit f702008

Browse files
authored
Performance improvement for csv-report (#136)
* Remove multiple clones * Use xlsxwriter instead of simple-excel-writer * Use slice instead of vec * Update CHANGELOG.md
1 parent ca53616 commit f702008

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
All notable changes to this project will be documented in this file.
33
This project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## [0.19.7] - 2021-03-09
6+
### Changed
7+
- Performance improvement for `rbt csv-report`.
8+
59
## [0.19.6] - 2021-03-09
610
### Changed
7-
- Fixed a JSON syntax error in `rbt vcf-report`
11+
- Fixed a JSON syntax error in `rbt vcf-report`.
812

913
## [0.19.5] - 2021-03-05
1014
### Changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ regex = "1.3"
4040
tera = "1"
4141
jsonm = "0.1.4"
4242
chrono = "0.4"
43-
simple_excel_writer="0.1.4"
43+
xlsxwriter = "0.3.2"
4444
lazy_static = "1.4"
4545
anyhow = "1"
4646
thiserror = "1"

src/csv/report.rs

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ use itertools::Itertools;
77
use lz_str::compress_to_utf16;
88
use serde_derive::Serialize;
99
use serde_json::json;
10-
use simple_excel_writer::*;
1110
use std::collections::{HashMap, HashSet};
11+
use std::convert::TryInto;
1212
use std::fs;
1313
use std::io::Write;
1414
use std::path::Path;
1515
use std::str::FromStr;
1616
use tera::{Context, Tera};
17+
use xlsxwriter::*;
1718

1819
type LookupTable = HashMap<String, HashMap<String, Vec<(String, usize, usize)>>>;
1920

@@ -85,11 +86,11 @@ pub(crate) fn csv_report(
8586
for title in &titles {
8687
match is_numeric.get(title) {
8788
Some(true) => {
88-
let plot = num_plot(table.clone(), title.to_string());
89+
let plot = num_plot(&table, title.to_string());
8990
num_plot_data.insert(title, plot);
9091
}
9192
Some(false) => {
92-
let plot = nominal_plot(table.clone(), title.to_string());
93+
let plot = nominal_plot(&table, title.to_string());
9394
plot_data.insert(title, plot);
9495
}
9596
_ => unreachable!(),
@@ -118,28 +119,22 @@ pub(crate) fn csv_report(
118119
(_, _) => {}
119120
}
120121

121-
let mut wb = Workbook::create(&(output_path.to_owned() + "/report.xlsx"));
122-
let mut sheet = wb.create_sheet("Report");
123-
for _ in 1..titles.len() {
124-
sheet.add_column(Column { width: 50.0 });
122+
let wb = Workbook::new(&(output_path.to_owned() + "/report.xlsx"));
123+
let mut sheet = wb.add_worksheet(Some("Report"))?;
124+
for (i, title) in titles.iter().enumerate() {
125+
sheet.write_string(0, i.try_into()?, title, None)?;
125126
}
126127

127-
wb.write_sheet(&mut sheet, |sheet_writer| {
128-
let sw = sheet_writer;
129-
let mut title_row = Row::new();
130-
for title in titles.clone() {
131-
title_row.add_cell(title);
132-
}
133-
sw.append_row(title_row)?;
134-
for row in table.clone() {
135-
let mut excel_row = Row::new();
136-
for title in titles.clone() {
137-
excel_row.add_cell(row.get(title).unwrap().as_str());
138-
}
139-
sw.append_row(excel_row)?;
128+
for (i, row) in table.iter().enumerate() {
129+
for (c, title) in titles.iter().enumerate() {
130+
sheet.write_string(
131+
(i + 1).try_into()?,
132+
c.try_into()?,
133+
row.get(*title).unwrap(),
134+
None,
135+
)?;
140136
}
141-
Ok(())
142-
})?;
137+
}
143138

144139
wb.close()?;
145140

@@ -341,7 +336,7 @@ pub(crate) fn csv_report(
341336
Ok(())
342337
}
343338

344-
fn num_plot(table: Vec<HashMap<String, String>>, column: String) -> Vec<BinnedPlotRecord> {
339+
fn num_plot(table: &[HashMap<String, String>], column: String) -> Vec<BinnedPlotRecord> {
345340
let mut values = Vec::new();
346341
let mut nan = 0;
347342
for row in table {
@@ -390,7 +385,7 @@ fn num_plot(table: Vec<HashMap<String, String>>, column: String) -> Vec<BinnedPl
390385
plot_data
391386
}
392387

393-
fn nominal_plot(table: Vec<HashMap<String, String>>, column: String) -> Vec<PlotRecord> {
388+
fn nominal_plot(table: &[HashMap<String, String>], column: String) -> Vec<PlotRecord> {
394389
let mut values = Vec::new();
395390
for row in table {
396391
let val = row.get(&column).unwrap();

0 commit comments

Comments
 (0)