1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 05:58:35 +01:00

fixed unnecessary memory allocations since 8d689952cc

This commit is contained in:
Ell 2022-10-22 11:46:11 +02:00
parent 17ce7b668d
commit be7676d37e
4 changed files with 22 additions and 7 deletions

View file

@ -33,7 +33,7 @@ namespace MLEM.Extended.Font {
/// <inheritdoc /> /// <inheritdoc />
protected override float MeasureCharacter(int codePoint) { protected override float MeasureCharacter(int codePoint) {
return this.Font.MeasureString(char.ConvertFromUtf32(codePoint)).X; return this.Font.MeasureString(this.ConvertCachedUtf32(codePoint)).X;
} }
/// <inheritdoc /> /// <inheritdoc />

View file

@ -48,6 +48,8 @@ namespace MLEM.Font {
/// </summary> /// </summary>
public abstract float LineHeight { get; } public abstract float LineHeight { get; }
private readonly Dictionary<int, string> codePointCache = new Dictionary<int, string>();
/// <summary> /// <summary>
/// Measures the width of the given code point with the default scale for use in <see cref="MeasureString(string,bool)"/>. /// Measures the width of the given code point with the default scale for use in <see cref="MeasureString(string,bool)"/>.
/// Note that this method does not support <see cref="Nbsp"/>, <see cref="Zwsp"/> and <see cref="Emsp"/> for most generic fonts, which is why <see cref="MeasureString(string,bool)"/> should be used even for single characters. /// Note that this method does not support <see cref="Nbsp"/>, <see cref="Zwsp"/> and <see cref="Emsp"/> for most generic fonts, which is why <see cref="MeasureString(string,bool)"/> should be used even for single characters.
@ -173,6 +175,19 @@ namespace MLEM.Font {
return this.SplitStringSeparate(new CodePointSource(text), width, scale, null); return this.SplitStringSeparate(new CodePointSource(text), width, scale, null);
} }
/// <summary>
/// Converts the given UTF-32 <paramref name="codePoint"/> into a string using <see cref="char.ConvertFromUtf32"/>, but caches the result in a <see cref="Dictionary{TKey,TValue}"/> cache to avoid allocating excess memory.
/// </summary>
/// <param name="codePoint">The UTF-32 code point to convert.</param>
/// <returns>The string representation of the code point.</returns>
public string ConvertCachedUtf32(int codePoint) {
if (!this.codePointCache.TryGetValue(codePoint, out var ret)) {
ret = char.ConvertFromUtf32(codePoint);
this.codePointCache.Add(codePoint, ret);
}
return ret;
}
internal Vector2 MeasureString(CodePointSource text, bool ignoreTrailingSpaces, Func<int, GenericFont> fontFunction) { internal Vector2 MeasureString(CodePointSource text, bool ignoreTrailingSpaces, Func<int, GenericFont> fontFunction) {
var size = Vector2.Zero; var size = Vector2.Zero;
if (text.Length <= 0) if (text.Length <= 0)
@ -225,9 +240,9 @@ namespace MLEM.Font {
var innerIndex = fromBack ? text.Length - 1 - index : index; var innerIndex = fromBack ? text.Length - 1 - index : index;
var (codePoint, length) = text.GetCodePoint(innerIndex, fromBack); var (codePoint, length) = text.GetCodePoint(innerIndex, fromBack);
if (fromBack) { if (fromBack) {
total.Insert(0, char.ConvertFromUtf32(codePoint)); total.Insert(0, this.ConvertCachedUtf32(codePoint));
} else { } else {
total.Append(char.ConvertFromUtf32(codePoint)); total.Append(this.ConvertCachedUtf32(codePoint));
} }
if (this.MeasureString(new CodePointSource(total + ellipsis), false, fontFunction).X * scale >= width) { if (this.MeasureString(new CodePointSource(total + ellipsis), false, fontFunction).X * scale >= width) {
@ -258,7 +273,7 @@ namespace MLEM.Font {
currWidth = 0; currWidth = 0;
} else { } else {
var font = fontFunction?.Invoke(index) ?? this; var font = fontFunction?.Invoke(index) ?? this;
var character = char.ConvertFromUtf32(codePoint); var character = this.ConvertCachedUtf32(codePoint);
var charWidth = font.MeasureString(character).X * scale; var charWidth = font.MeasureString(character).X * scale;
if (codePoint == ' ' || codePoint == GenericFont.Emsp || codePoint == GenericFont.Zwsp) { if (codePoint == ' ' || codePoint == GenericFont.Emsp || codePoint == GenericFont.Zwsp) {
// remember the location of this (breaking!) space // remember the location of this (breaking!) space
@ -334,7 +349,7 @@ namespace MLEM.Font {
offset.X = 0; offset.X = 0;
offset.Y += this.LineHeight; offset.Y += this.LineHeight;
} else { } else {
var character = char.ConvertFromUtf32(codePoint); var character = this.ConvertCachedUtf32(codePoint);
var charSize = this.MeasureString(character); var charSize = this.MeasureString(character);
var charPos = offset; var charPos = offset;

View file

@ -35,7 +35,7 @@ namespace MLEM.Font {
/// <inheritdoc /> /// <inheritdoc />
protected override float MeasureCharacter(int codePoint) { protected override float MeasureCharacter(int codePoint) {
return this.Font.MeasureString(char.ConvertFromUtf32(codePoint)).X; return this.Font.MeasureString(this.ConvertCachedUtf32(codePoint)).X;
} }
/// <inheritdoc /> /// <inheritdoc />

View file

@ -164,7 +164,7 @@ namespace MLEM.Formatting {
var line = new CodePointSource(token.SplitDisplayString[l]); var line = new CodePointSource(token.SplitDisplayString[l]);
while (charIndex < line.Length) { while (charIndex < line.Length) {
var (codePoint, length) = line.GetCodePoint(charIndex); var (codePoint, length) = line.GetCodePoint(charIndex);
var character = char.ConvertFromUtf32(codePoint); var character = drawFont.ConvertCachedUtf32(codePoint);
if (indexInToken == 0) if (indexInToken == 0)
token.DrawSelf(time, batch, pos + innerOffset, drawFont, color, scale, depth); token.DrawSelf(time, batch, pos + innerOffset, drawFont, color, scale, depth);