From 5cc24fda398972c2a2d8fe2acb6538b862932bd1 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 26 Aug 2017 10:49:48 -0500 Subject: [PATCH 1/2] PERF: optimize read_cell --- source/cell/cell_reference.cpp | 8 ++-- source/cell/index_types.cpp | 6 +-- source/detail/serialization/xlsx_consumer.cpp | 38 +++++++++++-------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/source/cell/cell_reference.cpp b/source/cell/cell_reference.cpp index 4a966a97..15736fdd 100644 --- a/source/cell/cell_reference.cpp +++ b/source/cell/cell_reference.cpp @@ -22,7 +22,7 @@ // @license: http://www.opensource.org/licenses/mit-license.php // @author: see AUTHORS file -#include +#include #include #include @@ -122,9 +122,9 @@ std::pair cell_reference::split_reference( for (auto character : reference_string) { - char upper = std::toupper(character, std::locale::classic()); + char upper = std::toupper(character); - if (std::isalpha(character, std::locale::classic())) + if (std::isalpha(character)) { if (column_part) { @@ -155,7 +155,7 @@ std::pair cell_reference::split_reference( { column_part = false; } - else if (!std::isdigit(character, std::locale::classic())) + else if (!std::isdigit(character)) { throw invalid_cell_reference(reference_string); } diff --git a/source/cell/index_types.cpp b/source/cell/index_types.cpp index 16b42138..c330421c 100644 --- a/source/cell/index_types.cpp +++ b/source/cell/index_types.cpp @@ -21,7 +21,7 @@ // // @license: http://www.opensource.org/licenses/mit-license.php // @author: see AUTHORS file -#include +#include #include #include @@ -41,12 +41,12 @@ column_t::index_t column_t::column_index_from_string(const std::string &column_s for (int i = static_cast(column_string.length()) - 1; i >= 0; i--) { - if (!std::isalpha(column_string[static_cast(i)], std::locale::classic())) + if (!std::isalpha(column_string[static_cast(i)])) { throw invalid_column_index(); } - auto char_index = std::toupper(column_string[static_cast(i)], std::locale::classic()) - 'A'; + auto char_index = std::toupper(column_string[static_cast(i)]) - 'A'; column_index += static_cast((char_index + 1) * place); place *= 26; diff --git a/source/detail/serialization/xlsx_consumer.cpp b/source/detail/serialization/xlsx_consumer.cpp index 1c451e9f..b3bcf716 100644 --- a/source/detail/serialization/xlsx_consumer.cpp +++ b/source/detail/serialization/xlsx_consumer.cpp @@ -149,6 +149,12 @@ void xlsx_consumer::open(std::istream &source) populate_workbook(true); } +// caching frequently used names +static const auto sheetData_el = qn("spreadsheetml", "sheetData"); +static const auto row_el = qn("spreadsheetml", "row"); +static const auto cell_el = qn("spreadsheetml", "c"); +static const auto val_el = qn("spreadsheetml", "v"); + cell xlsx_consumer::read_cell() { if (!has_cell()) @@ -158,10 +164,11 @@ cell xlsx_consumer::read_cell() auto ws = worksheet(current_worksheet_); - if (in_element(qn("spreadsheetml", "sheetData"))) + if (in_element(sheetData_el)) { - expect_start_element(qn("spreadsheetml", "row"), xml::content::complex); // CT_Row - auto row_index = parser().attribute("r"); + expect_start_element(row_el, xml::content::complex); // CT_Row + // auto row_index = parser().attribute("r"); + auto row_index = static_cast(std::stoul(parser().attribute("r"))); if (parser().attribute_present("ht")) { @@ -177,19 +184,18 @@ cell xlsx_consumer::read_cell() { ws.row_properties(row_index).hidden = true; } - skip_attributes({ qn("x14ac", "dyDescent") }); skip_attributes({ "customFormat", "s", "customFont", "outlineLevel", "collapsed", "thickTop", "thickBot", "ph", "spans" }); } - if (!in_element(qn("spreadsheetml", "row"))) + if (!in_element(row_el)) { return cell(nullptr); - } + } - expect_start_element(qn("spreadsheetml", "c"), xml::content::complex); + expect_start_element(cell_el, xml::content::complex); auto cell = streaming_ ? xlnt::cell(streaming_cell_.get()) : ws.cell(cell_reference(parser().attribute("r"))); @@ -211,11 +217,11 @@ cell xlsx_consumer::read_cell() auto has_shared_formula = false; auto formula_value_string = std::string(); - while (in_element(qn("spreadsheetml", "c"))) + while (in_element(cell_el)) { auto current_element = expect_start_element(xml::content::mixed); - if (current_element == qn("spreadsheetml", "v")) // s:ST_Xstring + if (current_element == val_el) // s:ST_Xstring { has_value = true; value_string = read_text(); @@ -248,7 +254,7 @@ cell xlsx_consumer::read_cell() expect_end_element(current_element); } - expect_end_element(qn("spreadsheetml", "c")); + expect_end_element(cell_el); if (has_formula && !has_shared_formula) { @@ -291,13 +297,13 @@ cell xlsx_consumer::read_cell() cell.format(target_.format(format_id)); } - if (!in_element(qn("spreadsheetml", "row"))) + if (!in_element(row_el)) { - expect_end_element(qn("spreadsheetml", "row")); + expect_end_element(row_el); - if (!in_element(qn("spreadsheetml", "sheetData"))) + if (!in_element(sheetData_el)) { - expect_end_element(qn("spreadsheetml", "sheetData")); + expect_end_element(sheetData_el); } } @@ -1024,8 +1030,8 @@ xml::parser &xlsx_consumer::parser() bool xlsx_consumer::has_cell() { - return in_element(qn("spreadsheetml", "row")) - || in_element(qn("spreadsheetml", "sheetData")); + return in_element(row_el) + || in_element(sheetData_el); } std::vector xlsx_consumer::read_relationships(const path &part) From e5d6a26e1738f9b43a4a4e8a12b9ac438a2a5550 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 26 Aug 2017 11:04:23 -0500 Subject: [PATCH 2/2] fixup formatting --- source/detail/serialization/xlsx_consumer.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/source/detail/serialization/xlsx_consumer.cpp b/source/detail/serialization/xlsx_consumer.cpp index b3bcf716..eb7cc7bb 100644 --- a/source/detail/serialization/xlsx_consumer.cpp +++ b/source/detail/serialization/xlsx_consumer.cpp @@ -167,8 +167,7 @@ cell xlsx_consumer::read_cell() if (in_element(sheetData_el)) { expect_start_element(row_el, xml::content::complex); // CT_Row - // auto row_index = parser().attribute("r"); - auto row_index = static_cast(std::stoul(parser().attribute("r"))); + auto row_index = static_cast(std::stoul(parser().attribute("r"))); if (parser().attribute_present("ht")) { @@ -299,7 +298,7 @@ cell xlsx_consumer::read_cell() if (!in_element(row_el)) { - expect_end_element(row_el); + expect_end_element(row_el); if (!in_element(sheetData_el)) {