Use LocalVector<Glyph> to reduce reallocation.

This commit is contained in:
Yyf2333
2025-01-23 15:04:44 +08:00
committed by Yufeng Ying
parent 19bb18716e
commit 21fc5cce5c
2 changed files with 17 additions and 17 deletions

View File

@@ -4941,11 +4941,11 @@ bool TextServerAdvanced::_shaped_text_resize_object(const RID &p_shaped, const V
if (sd->orientation == ORIENTATION_HORIZONTAL) {
sd->objects[key].rect.position.x = sd->width;
sd->width += sd->objects[key].rect.size.x;
sd->glyphs.write[i].advance = sd->objects[key].rect.size.x;
sd->glyphs[i].advance = sd->objects[key].rect.size.x;
} else {
sd->objects[key].rect.position.y = sd->width;
sd->width += sd->objects[key].rect.size.y;
sd->glyphs.write[i].advance = sd->objects[key].rect.size.y;
sd->glyphs[i].advance = sd->objects[key].rect.size.y;
}
} else {
if (gl.font_rid.is_valid()) {
@@ -5331,12 +5331,12 @@ double TextServerAdvanced::_shaped_text_fit_to_width(const RID &p_shaped, double
// Trim spaces.
while ((start_pos < end_pos) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[start_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[start_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
justification_width -= sd->glyphs[start_pos].advance * sd->glyphs[start_pos].repeat;
sd->glyphs.write[start_pos].advance = 0;
sd->glyphs[start_pos].advance = 0;
start_pos += sd->glyphs[start_pos].count;
}
while ((start_pos < end_pos) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN) && ((sd->glyphs[end_pos].flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_HARD) == GRAPHEME_IS_BREAK_HARD || (sd->glyphs[end_pos].flags & GRAPHEME_IS_BREAK_SOFT) == GRAPHEME_IS_BREAK_SOFT)) {
justification_width -= sd->glyphs[end_pos].advance * sd->glyphs[end_pos].repeat;
sd->glyphs.write[end_pos].advance = 0;
sd->glyphs[end_pos].advance = 0;
end_pos -= sd->glyphs[end_pos].count;
}
} else {
@@ -5369,7 +5369,7 @@ double TextServerAdvanced::_shaped_text_fit_to_width(const RID &p_shaped, double
if ((elongation_count > 0) && p_jst_flags.has_flag(JUSTIFICATION_KASHIDA)) {
double delta_width_per_kashida = (p_width - justification_width) / elongation_count;
for (int i = start_pos; i <= end_pos; i++) {
Glyph &gl = sd->glyphs.write[i];
Glyph &gl = sd->glyphs[i];
if (gl.count > 0) {
if (((gl.flags & GRAPHEME_IS_ELONGATION) == GRAPHEME_IS_ELONGATION) && (gl.advance > 0)) {
if ((i > 0) && ((sd->glyphs[i - 1].flags & GRAPHEME_IS_ELONGATION) != GRAPHEME_IS_ELONGATION)) {
@@ -5391,7 +5391,7 @@ double TextServerAdvanced::_shaped_text_fit_to_width(const RID &p_shaped, double
double delta_width_per_space = (p_width - justification_width) / space_count;
double adv_remain = 0;
for (int i = start_pos; i <= end_pos; i++) {
Glyph &gl = sd->glyphs.write[i];
Glyph &gl = sd->glyphs[i];
if (gl.count > 0) {
if ((gl.flags & GRAPHEME_IS_SOFT_HYPHEN) != GRAPHEME_IS_SOFT_HYPHEN && (gl.flags & GRAPHEME_IS_SPACE) == GRAPHEME_IS_SPACE && (gl.flags & GRAPHEME_IS_PUNCTUATION) != GRAPHEME_IS_PUNCTUATION) {
double old_adv = gl.advance;
@@ -5459,7 +5459,7 @@ double TextServerAdvanced::_shaped_text_tab_align(const RID &p_shaped, const Pac
delta = -1;
}
Glyph *gl = sd->glyphs.ptrw();
Glyph *gl = sd->glyphs.ptr();
for (int i = start; i != end; i += delta) {
if ((gl[i].flags & GRAPHEME_IS_TAB) == GRAPHEME_IS_TAB) {
@@ -5668,7 +5668,7 @@ void TextServerAdvanced::_shaped_text_overrun_trim_to_width(const RID &p_shaped_
bool enforce_ellipsis = p_trim_flags.has_flag(OVERRUN_ENFORCE_ELLIPSIS);
bool justification_aware = p_trim_flags.has_flag(OVERRUN_JUSTIFICATION_AWARE);
Glyph *sd_glyphs = sd->glyphs.ptrw();
Glyph *sd_glyphs = sd->glyphs.ptr();
if ((p_trim_flags & OVERRUN_TRIM) == OVERRUN_NO_TRIM || sd_glyphs == nullptr || p_width <= 0 || !(sd->width > p_width || enforce_ellipsis)) {
sd->overrun_trim_data.trim_pos = -1;
@@ -6039,17 +6039,17 @@ bool TextServerAdvanced::_shaped_text_update_breaks(const RID &p_shaped) {
sd->break_ops_valid = true;
}
Vector<Glyph> glyphs_new;
LocalVector<Glyph> glyphs_new;
bool rewrite = false;
int sd_shift = 0;
int sd_size = sd->glyphs.size();
Glyph *sd_glyphs = sd->glyphs.ptrw();
Glyph *sd_glyphs = sd->glyphs.ptr();
Glyph *sd_glyphs_new = nullptr;
if (sd->break_inserts > 0) {
glyphs_new.resize(sd->glyphs.size() + sd->break_inserts);
sd_glyphs_new = glyphs_new.ptrw();
sd_glyphs_new = glyphs_new.ptr();
rewrite = true;
} else {
sd_glyphs_new = sd_glyphs;
@@ -6165,7 +6165,7 @@ bool TextServerAdvanced::_shaped_text_update_breaks(const RID &p_shaped) {
}
if (sd->break_inserts > 0) {
sd->glyphs = glyphs_new;
sd->glyphs = std::move(glyphs_new);
}
sd->line_breaks_valid = true;
@@ -6311,7 +6311,7 @@ bool TextServerAdvanced::_shaped_text_update_justification_ops(const RID &p_shap
sd->sort_valid = false;
sd->glyphs_logical.clear();
Glyph *sd_glyphs = sd->glyphs.ptrw();
Glyph *sd_glyphs = sd->glyphs.ptr();
int sd_size = sd->glyphs.size();
if (!sd->jstops.is_empty()) {
for (int i = 0; i < sd_size; i++) {
@@ -6349,7 +6349,7 @@ bool TextServerAdvanced::_shaped_text_update_justification_ops(const RID &p_shap
// Update write pointer and size.
sd_size = sd->glyphs.size();
sd_glyphs = sd->glyphs.ptrw();
sd_glyphs = sd->glyphs.ptr();
continue;
}
}
@@ -6389,7 +6389,7 @@ bool TextServerAdvanced::_shaped_text_update_justification_ops(const RID &p_shap
// Update write pointer and size.
sd_size = sd->glyphs.size();
sd_glyphs = sd->glyphs.ptrw();
sd_glyphs = sd->glyphs.ptr();
continue;
}
}

View File

@@ -543,8 +543,8 @@ class TextServerAdvanced : public TextServerExtension {
TrimData overrun_trim_data;
bool fit_width_minimum_reached = false;
Vector<Glyph> glyphs;
Vector<Glyph> glyphs_logical;
LocalVector<Glyph> glyphs;
LocalVector<Glyph> glyphs_logical;
/* Intermediate data */
Char16String utf16;