Improving memory usage and allocation (#4)

* Improving memory usage and allocation (#3)

* Improving memory usage and allocation (#2)

* Improving memory usage and allocation

No need for an optional to a pointer

* Switching to a pointer directly

Less memory usage and allocations

* Switching to pointer, it needs to be initialized

* Making value_text_ as optional to reduce memory usage

Making value_text_ as optional to reduce memory usage

* Making value_text_ as optional to reduce memory usage

Making value_text_ as optional to reduce memory usage

* Making value_text_ as optional to reduce memory usage

Making value_text_ as optional to reduce memory usage

* Fixing mistake when emty
This commit is contained in:
Teebonne 2022-09-09 22:58:30 +01:00 committed by GitHub
parent fbae8cd6c1
commit 635277d736
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 24 deletions

View File

@ -504,8 +504,8 @@ void cell::error(const std::string &error)
{ {
throw invalid_data_type(); throw invalid_data_type();
} }
d_->value_text_ = error;
d_->value_text_.plain_text(error, false); d_->value_text_.get().plain_text(error, false);
d_->type_ = type::error; d_->type_ = type::error;
} }
@ -717,7 +717,7 @@ XLNT_API rich_text cell::value() const
return workbook().shared_strings(static_cast<std::size_t>(d_->value_numeric_)); return workbook().shared_strings(static_cast<std::size_t>(d_->value_numeric_));
} }
return d_->value_text_; return d_->value_text_.get();
} }
bool cell::has_value() const bool cell::has_value() const
@ -750,7 +750,7 @@ std::string cell::to_string() const
bool cell::has_format() const bool cell::has_format() const
{ {
return d_->format_.is_set(); return (d_->format_ != nullptr);
} }
void cell::format(const class format new_format) void cell::format(const class format new_format)
@ -838,10 +838,10 @@ void cell::value(const std::string &value_string, bool infer_type)
void cell::clear_format() void cell::clear_format()
{ {
if (d_->format_.is_set()) if (d_->format_ != nullptr)
{ {
format().d_->references -= format().d_->references > 0 ? 1 : 0; format().d_->references -= format().d_->references > 0 ? 1 : 0;
d_->format_.clear(); d_->format_ = nullptr;
} }
} }
@ -899,22 +899,22 @@ bool cell::has_style() const
format cell::modifiable_format() format cell::modifiable_format()
{ {
if (!d_->format_.is_set()) if (d_->format_ == nullptr)
{ {
throw invalid_attribute(); throw invalid_attribute();
} }
return xlnt::format(d_->format_.get()); return xlnt::format(d_->format_);
} }
const format cell::format() const const format cell::format() const
{ {
if (!d_->format_.is_set()) if (d_->format_ == nullptr)
{ {
throw invalid_attribute(); throw invalid_attribute();
} }
return xlnt::format(d_->format_.get()); return xlnt::format(d_->format_);
} }
alignment cell::alignment() const alignment cell::alignment() const
@ -956,7 +956,7 @@ bool cell::has_hyperlink() const
bool cell::has_comment() bool cell::has_comment()
{ {
return d_->comment_.is_set(); return (d_->comment_ != nullptr);
} }
void cell::clear_comment() void cell::clear_comment()
@ -964,7 +964,7 @@ void cell::clear_comment()
if (has_comment()) if (has_comment())
{ {
d_->parent_->comments_.erase(reference().to_string()); d_->parent_->comments_.erase(reference().to_string());
d_->comment_.clear(); d_->comment_ = nullptr;
} }
} }
@ -975,7 +975,7 @@ class comment cell::comment()
throw xlnt::exception("cell has no comment"); throw xlnt::exception("cell has no comment");
} }
return *d_->comment_.get(); return *d_->comment_;
} }
void cell::comment(const std::string &text, const std::string &author) void cell::comment(const std::string &text, const std::string &author)
@ -992,12 +992,12 @@ void cell::comment(const class comment &new_comment)
{ {
if (has_comment()) if (has_comment())
{ {
*d_->comment_.get() = new_comment; *d_->comment_ = new_comment;
} }
else else
{ {
d_->parent_->comments_[reference().to_string()] = new_comment; d_->parent_->comments_[reference().to_string()] = new_comment;
d_->comment_.set(&d_->parent_->comments_[reference().to_string()]); d_->comment_ = (&d_->parent_->comments_[reference().to_string()]);
} }
// offset comment 5 pixels down and 5 pixels right of the top right corner of the cell // offset comment 5 pixels down and 5 pixels right of the top right corner of the cell
@ -1005,7 +1005,7 @@ void cell::comment(const class comment &new_comment)
cell_position.first += static_cast<int>(width()) + 5; cell_position.first += static_cast<int>(width()) + 5;
cell_position.second += 5; cell_position.second += 5;
d_->comment_.get()->position(cell_position.first, cell_position.second); d_->comment_->position(cell_position.first, cell_position.second);
worksheet().register_comments_in_manifest(); worksheet().register_comments_in_manifest();
} }

View File

@ -36,7 +36,9 @@ cell_impl::cell_impl()
row_(1), row_(1),
is_merged_(false), is_merged_(false),
phonetics_visible_(false), phonetics_visible_(false),
value_numeric_(0) value_numeric_(0),
format_(),
comment_()
{ {
} }

View File

@ -58,17 +58,17 @@ struct cell_impl
bool is_merged_; bool is_merged_;
bool phonetics_visible_; bool phonetics_visible_;
rich_text value_text_; optional<rich_text> value_text_;
double value_numeric_; double value_numeric_;
optional<std::string> formula_; optional<std::string> formula_;
optional<hyperlink_impl> hyperlink_; optional<hyperlink_impl> hyperlink_;
optional<format_impl *> format_; format_impl *format_;
optional<comment *> comment_; comment *comment_;
bool is_garbage_collectible() const bool is_garbage_collectible() const
{ {
return !(type_ != cell_type::empty || is_merged_ || phonetics_visible_ || formula_.is_set() || format_.is_set() || hyperlink_.is_set()); return !(type_ != cell_type::empty || is_merged_ || phonetics_visible_ || formula_.is_set() || format_ != nullptr || hyperlink_.is_set());
} }
}; };
@ -84,8 +84,8 @@ inline bool operator==(const cell_impl &lhs, const cell_impl &rhs)
&& float_equals(lhs.value_numeric_, rhs.value_numeric_) && float_equals(lhs.value_numeric_, rhs.value_numeric_)
&& lhs.formula_ == rhs.formula_ && lhs.formula_ == rhs.formula_
&& lhs.hyperlink_ == rhs.hyperlink_ && lhs.hyperlink_ == rhs.hyperlink_
&& (lhs.format_.is_set() == rhs.format_.is_set() && (!lhs.format_.is_set() || *lhs.format_.get() == *rhs.format_.get())) && ((lhs.format_ != nullptr) == (rhs.format_ != nullptr) && ((lhs.format_ == nullptr) || *lhs.format_ == *rhs.format_))
&& (lhs.comment_.is_set() == rhs.comment_.is_set() && (!lhs.comment_.is_set() || *lhs.comment_.get() == *rhs.comment_.get())); && ((lhs.comment_ != nullptr) == (rhs.comment_ != nullptr) && ((lhs.comment_ == nullptr) || *lhs.comment_ == *rhs.comment_));
} }
} // namespace detail } // namespace detail

View File

@ -892,7 +892,8 @@ void xlsx_consumer::read_worksheet_sheetdata()
break; break;
} }
case cell::type::error: { case cell::type::error: {
ws_cell_impl->value_text_.plain_text(cell.value, false); ws_cell_impl->value_text_ = std::move(cell.value);
ws_cell_impl->value_text_.get().plain_text(cell.value, false);
break; break;
} }
} }