summaryrefslogtreecommitdiff
path: root/src/os
diff options
context:
space:
mode:
authorMichael Lutz <michi@icosahedron.de>2019-04-02 21:30:53 +0200
committerMichael Lutz <michi@icosahedron.de>2019-04-09 22:45:15 +0200
commit329bb526134aca214a914f25006c805de78ec851 (patch)
tree00763488d458fee57ce8ba94e32ae53899367d21 /src/os
parent9325d63d8e55758a953f535c5f90534ab9bc01e3 (diff)
downloadopenttd-329bb526134aca214a914f25006c805de78ec851.tar.xz
Codechange: Store text layout runs directly as values in a std::vector instead of heap allocated.
This reduces memory allocations and heap fragmentation.
Diffstat (limited to 'src/os')
-rw-r--r--src/os/macosx/string_osx.cpp7
-rw-r--r--src/os/windows/string_uniscribe.cpp23
2 files changed, 20 insertions, 10 deletions
diff --git a/src/os/macosx/string_osx.cpp b/src/os/macosx/string_osx.cpp
index f26421e36..55eac42f9 100644
--- a/src/os/macosx/string_osx.cpp
+++ b/src/os/macosx/string_osx.cpp
@@ -53,6 +53,7 @@ public:
public:
CoreTextVisualRun(CTRunRef run, Font *font, const CoreTextParagraphLayoutFactory::CharType *buff);
+ CoreTextVisualRun(CoreTextVisualRun &&other) = default;
const GlyphID *GetGlyphs() const override { return &this->glyphs[0]; }
const float *GetPositions() const override { return &this->positions[0]; }
@@ -65,7 +66,7 @@ public:
};
/** A single line worth of VisualRuns. */
- class CoreTextLine : public AutoDeleteSmallVector<CoreTextVisualRun *>, public ParagraphLayouter::Line {
+ class CoreTextLine : public std::vector<CoreTextVisualRun>, public ParagraphLayouter::Line {
public:
CoreTextLine(CTLineRef line, const FontMap &fontMapping, const CoreTextParagraphLayoutFactory::CharType *buff)
{
@@ -78,7 +79,7 @@ public:
auto map = fontMapping.begin();
while (map < fontMapping.end() - 1 && map->first <= chars.location) map++;
- this->push_back(new CoreTextVisualRun(run, map->second, buff));
+ this->emplace_back(run, map->second, buff);
}
CFRelease(line);
}
@@ -86,7 +87,7 @@ public:
int GetLeading() const override;
int GetWidth() const override;
int CountRuns() const override { return this->size(); }
- const VisualRun *GetVisualRun(int run) const override { return this->at(run); }
+ const VisualRun &GetVisualRun(int run) const override { return this->at(run); }
int GetInternalCharLength(WChar c) const override
{
diff --git a/src/os/windows/string_uniscribe.cpp b/src/os/windows/string_uniscribe.cpp
index a5d03b948..f47d9236a 100644
--- a/src/os/windows/string_uniscribe.cpp
+++ b/src/os/windows/string_uniscribe.cpp
@@ -90,6 +90,7 @@ public:
public:
UniscribeVisualRun(const UniscribeRun &range, int x);
+ UniscribeVisualRun(UniscribeVisualRun &&other) noexcept;
~UniscribeVisualRun() override
{
free(this->glyph_to_char);
@@ -106,12 +107,12 @@ public:
};
/** A single line worth of VisualRuns. */
- class UniscribeLine : public AutoDeleteSmallVector<UniscribeVisualRun *>, public ParagraphLayouter::Line {
+ class UniscribeLine : public std::vector<UniscribeVisualRun>, public ParagraphLayouter::Line {
public:
int GetLeading() const override;
int GetWidth() const override;
int CountRuns() const override { return (uint)this->size(); }
- const VisualRun *GetVisualRun(int run) const override { return this->at(run); }
+ const VisualRun &GetVisualRun(int run) const override { return this->at(run); }
int GetInternalCharLength(WChar c) const override
{
@@ -424,7 +425,7 @@ static std::vector<SCRIPT_ITEM> UniscribeItemizeString(UniscribeParagraphLayoutF
if (!UniscribeShapeRun(this->text_buffer, run)) return NULL;
}
- line->push_back(new UniscribeVisualRun(run, cur_pos));
+ line->emplace_back(run, cur_pos);
cur_pos += run.total_advance;
}
@@ -448,8 +449,8 @@ static std::vector<SCRIPT_ITEM> UniscribeItemizeString(UniscribeParagraphLayoutF
int UniscribeParagraphLayout::UniscribeLine::GetLeading() const
{
int leading = 0;
- for (const UniscribeVisualRun *run : *this) {
- leading = max(leading, run->GetLeading());
+ for (const auto &run : *this) {
+ leading = max(leading, run.GetLeading());
}
return leading;
@@ -462,8 +463,8 @@ int UniscribeParagraphLayout::UniscribeLine::GetLeading() const
int UniscribeParagraphLayout::UniscribeLine::GetWidth() const
{
int length = 0;
- for (const UniscribeVisualRun *run : *this) {
- length += run->GetAdvance();
+ for (const auto &run : *this) {
+ length += run.GetAdvance();
}
return length;
@@ -484,6 +485,14 @@ UniscribeParagraphLayout::UniscribeVisualRun::UniscribeVisualRun(const Uniscribe
this->positions[this->num_glyphs * 2] = advance + x;
}
+UniscribeParagraphLayout::UniscribeVisualRun::UniscribeVisualRun(UniscribeVisualRun&& other) noexcept
+ : glyphs(std::move(other.glyphs)), positions(std::move(other.positions)), char_to_glyph(std::move(other.char_to_glyph)),
+ start_pos(other.start_pos), total_advance(other.total_advance), num_glyphs(other.num_glyphs), font(other.font)
+{
+ this->glyph_to_char = other.glyph_to_char;
+ other.glyph_to_char = NULL;
+}
+
const int *UniscribeParagraphLayout::UniscribeVisualRun::GetGlyphToCharMap() const
{
if (this->glyph_to_char == NULL) {