Skip to content
This repository was archived by the owner on Apr 29, 2021. It is now read-only.

Commit c33c6dd

Browse files
author
Yuncong Zhang
committed
Release paragraphs in all places where they are created.
1 parent bd11ae3 commit c33c6dd

File tree

6 files changed

+62
-30
lines changed

6 files changed

+62
-30
lines changed

Runtime/flow/instrumentation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ public void visualize(Canvas canvas, Rect rect) {
106106
paragraph.layout(new ParagraphConstraints(width: 800));
107107

108108
canvas.drawParagraph(paragraph, new Offset(rect.left, rect.top + rect.height - 12));
109+
Paragraph.release(ref paragraph);
109110
}
110111
}
111112
}

Runtime/flow/performance_overlay_layer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ void _drawFPS(Canvas canvas, float x, float y) {
4141
paragraph.layout(new ParagraphConstraints(width: 300));
4242

4343
canvas.drawParagraph(paragraph, new Offset(x, y));
44+
Paragraph.release(ref paragraph);
4445
}
4546
}
4647
}

Runtime/ui/txt/layout_utils.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ public static bool isWordBreakBefore(ushort c) {
6868
public static bool isWordBreakBeforeNotAfter(ushort c) {
6969
return c >= 3400 && c <= 0x9fff;
7070
}
71+
72+
public static int minPowerOfTwo(int i) {
73+
// Assume that int is 32 bit
74+
i--;
75+
i = i | (i >> 1);
76+
i = i | (i >> 2);
77+
i = i | (i >> 4);
78+
i = i | (i >> 8);
79+
i = i | (i >> 16);
80+
return i + 1;
81+
}
7182

7283
}
7384
}

Runtime/ui/txt/linebreaker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public float getWidth(int i) {
148148

149149
public void resize(int size) {
150150
if (this._charWidths == null || this._charWidths.Length < size) {
151-
this._charWidths = new float[size];
151+
this._charWidths = new float[LayoutUtils.minPowerOfTwo(size)];
152152
}
153153
}
154154

Runtime/ui/txt/paragraph.cs

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ public LineRange(int start, int end, int endExcludingWhitespace, int endIncludin
198198
int _lineCount;
199199
int _paintRecordsCount;
200200
int _codeUnitRunsCount;
201+
int _lineRangeCount;
202+
int _lineWidthCount;
201203
bool _didExceedMaxLines;
202204
TabStops _tabStops = new TabStops();
203205

@@ -299,34 +301,34 @@ public void layout(ParagraphConstraints constraints) {
299301

300302
int lineStyleRunsCount = this._computeLineBreak();
301303

302-
if (this._glyphLines == null || this._glyphLines.Length < this._lineRanges.Count) {
303-
this._glyphLines = new GlyphLine[this._lineRanges.Count];
304+
if (this._glyphLines == null || this._glyphLines.Length < this._lineRangeCount) {
305+
this._glyphLines = new GlyphLine[LayoutUtils.minPowerOfTwo(this._lineRangeCount)];
304306
}
305307

306-
// if (this._lineBaseLines == null || this._lineBaseLines.Length < this._lineRanges.Count) {
307-
// this._lineBaseLines = new float[this._lineRanges.Count];
308+
// if (this._lineBaseLines == null || this._lineBaseLines.Length < this._lineRangeCount) {
309+
// this._lineBaseLines = new float[this._lineRangeCount];
308310
// }
309311

310-
if (this._lineHeights == null || this._lineHeights.Length < this._lineRanges.Count) {
311-
this._lineHeights = new float[this._lineRanges.Count];
312+
if (this._lineHeights == null || this._lineHeights.Length < this._lineRangeCount) {
313+
this._lineHeights = new float[LayoutUtils.minPowerOfTwo(this._lineRangeCount)];
312314
}
313315

314316
if (this._paintRecords == null || this._paintRecords.Length < lineStyleRunsCount) {
315-
this._paintRecords = new PaintRecord[lineStyleRunsCount];
317+
this._paintRecords = new PaintRecord[LayoutUtils.minPowerOfTwo(lineStyleRunsCount)];
316318
}
317319

318320
this._paintRecordsCount = 0;
319321

320322
if (this._codeUnitRuns == null || this._codeUnitRuns.Length < lineStyleRunsCount) {
321-
this._codeUnitRuns = new CodeUnitRun[lineStyleRunsCount];
323+
this._codeUnitRuns = new CodeUnitRun[LayoutUtils.minPowerOfTwo(lineStyleRunsCount)];
322324
}
323325

324326
this._codeUnitRunsCount = 0;
325327

326328
int styleMaxLines = this._paragraphStyle.maxLines ?? int.MaxValue;
327-
this._didExceedMaxLines = this._lineRanges.Count > styleMaxLines;
329+
this._didExceedMaxLines = this._lineRangeCount > styleMaxLines;
328330

329-
var lineLimit = Mathf.Min(styleMaxLines, this._lineRanges.Count);
331+
var lineLimit = Mathf.Min(styleMaxLines, this._lineRangeCount);
330332
int styleRunIndex = 0;
331333
float yOffset = 0;
332334
float preMaxDescent = 0;
@@ -337,12 +339,12 @@ public void layout(ParagraphConstraints constraints) {
337339

338340
// All text blobs share a single position buffer, which is big enough taking ellipsis into consideration
339341
if (this._textBlobPositions == null || this._textBlobPositions.Length < ellipsizedLength) {
340-
this._textBlobPositions = new float[ellipsizedLength];
342+
this._textBlobPositions = new float[LayoutUtils.minPowerOfTwo(ellipsizedLength)];
341343
}
342344
builder.setPositions(this._textBlobPositions);
343345
// this._glyphLines and this._codeUnitRuns will refer to this array for glyph positions
344346
if (this._glyphPositions == null || this._glyphPositions.Length < ellipsizedLength) {
345-
this._glyphPositions = new GlyphPosition[ellipsizedLength];
347+
this._glyphPositions = new GlyphPosition[LayoutUtils.minPowerOfTwo(ellipsizedLength)];
346348
}
347349

348350
// Pointer to the _glyphPositions array, to keep track of where the next glyph is stored
@@ -389,11 +391,11 @@ public void layout(ParagraphConstraints constraints) {
389391
// TODO: find a way to compute the maxTextCount for the entire paragraph, so that this allocation
390392
// happens only once
391393
if (_advancesBuffer == null || _advancesBuffer.Length < maxTextCount) {
392-
_advancesBuffer = new float[maxTextCount];
394+
_advancesBuffer = new float[LayoutUtils.minPowerOfTwo(maxTextCount)];
393395
}
394396

395397
if (_positionsBuffer == null || _positionsBuffer.Length < maxTextCount) {
396-
_positionsBuffer = new float[maxTextCount];
398+
_positionsBuffer = new float[LayoutUtils.minPowerOfTwo(maxTextCount)];
397399
}
398400

399401
// Keep of the position in _glyphPositions before evaluating this line
@@ -573,7 +575,7 @@ void updateLineMetrics(FontMetrics metrics, float styleHeight) {
573575
}
574576

575577
int lineStart = lineRange.start;
576-
int nextLineStart = lineNumber < this._lineRanges.Count - 1
578+
int nextLineStart = lineNumber < this._lineRangeCount - 1
577579
? this._lineRanges[lineNumber + 1].start
578580
: this._text.Length;
579581
this._glyphLines[lineNumber] =
@@ -588,7 +590,7 @@ void updateLineMetrics(FontMetrics metrics, float styleHeight) {
588590
this._lineCount = lineLimit;
589591
this._maxIntrinsicWidth = 0;
590592
float lineBlockWidth = 0;
591-
for (int i = 0; i < this._lineWidths.Count; ++i) {
593+
for (int i = 0; i < this._lineWidthCount; ++i) {
592594
lineBlockWidth += this._lineWidths[i];
593595
if (this._lineRanges[i].hardBreak) {
594596
this._maxIntrinsicWidth = Mathf.Max(lineBlockWidth, this._maxIntrinsicWidth);
@@ -689,7 +691,7 @@ public List<TextBox> getRectsForRange(int start, int end) {
689691
boxs.Add(TextBox.fromLTBD(left, top, right, bottom, run.direction));
690692
}
691693

692-
for (int lineNumber = 0; lineNumber < this._lineRanges.Count; ++lineNumber) {
694+
for (int lineNumber = 0; lineNumber < this._lineRangeCount; ++lineNumber) {
693695
var line = this._lineRanges[lineNumber];
694696
if (line.start >= end) {
695697
break;
@@ -824,8 +826,8 @@ public int getLineCount() {
824826
}
825827

826828
int _computeLineBreak() {
827-
this._lineRanges.Clear();
828-
this._lineWidths.Clear();
829+
this._lineRangeCount = 0;
830+
this._lineWidthCount = 0;
829831
this._maxIntrinsicWidth = 0;
830832

831833
int lineLimit = this._paragraphStyle.ellipsized()
@@ -838,7 +840,7 @@ int _computeLineBreak() {
838840
int runIndex = 0;
839841
int countRuns = 0;
840842
for (var newlineIndex = 0; newlineIndex < newLineCount; ++newlineIndex) {
841-
if (lineLimit != 0 && this._lineRanges.Count >= lineLimit) {
843+
if (lineLimit != 0 && this._lineRangeCount >= lineLimit) {
842844
break;
843845
}
844846
var blockStart = newlineIndex > 0 ? newLinePositions[newlineIndex - 1] + 1 : 0;
@@ -848,12 +850,12 @@ int _computeLineBreak() {
848850
this._addEmptyLine(blockStart, blockEnd);
849851
continue;
850852
}
851-
if (lineLimit != 0 && this._lineRanges.Count >= lineLimit) {
853+
if (lineLimit != 0 && this._lineRangeCount >= lineLimit) {
852854
break;
853855
}
854856

855857
this._resetLineBreaker(lineBreaker, blockStart, blockSize,
856-
lineLimit == 0 ? 0 : lineLimit - this._lineRanges.Count);
858+
lineLimit == 0 ? 0 : lineLimit - this._lineRangeCount);
857859
countRuns += this._addStyleRuns(lineBreaker, ref runIndex, blockStart, blockEnd);
858860

859861
int breaksCount = lineBreaker.computeBreaks();
@@ -866,10 +868,27 @@ int _computeLineBreak() {
866868
return countRuns;
867869
}
868870

871+
void _addLineRangeAndWidth(LineRange lineRange, float width) {
872+
if (this._lineRanges.Count <= this._lineRangeCount) {
873+
this._lineRanges.Add(lineRange);
874+
this._lineRangeCount++;
875+
}
876+
else {
877+
this._lineRanges[this._lineRangeCount++] = lineRange;
878+
}
879+
880+
if (this._lineWidths.Count <= this._lineWidthCount) {
881+
this._lineWidths.Add(width);
882+
this._lineWidthCount++;
883+
}
884+
else {
885+
this._lineWidths[this._lineWidthCount++] = width;
886+
}
887+
}
888+
869889
void _addEmptyLine(int blockStart, int blockEnd) {
870-
this._lineRanges.Add(new LineRange(blockStart, blockEnd, blockEnd,
871-
blockEnd < this._text.Length ? blockEnd + 1 : blockEnd, true));
872-
this._lineWidths.Add(0);
890+
this._addLineRangeAndWidth(new LineRange(blockStart, blockEnd, blockEnd,
891+
blockEnd < this._text.Length ? blockEnd + 1 : blockEnd, true), 0);
873892
}
874893

875894
void _resetLineBreaker(LineBreaker lineBreaker, int blockStart, int blockSize, int lineLimit) {
@@ -926,15 +945,14 @@ void _updateBreaks(LineBreaker lineBreaker, int breaksCount, int blockStart, int
926945
lineEndExcludingWhitespace--;
927946
}
928947

929-
this._lineRanges.Add(new LineRange(lineStart, lineEnd,
930-
lineEndExcludingWhitespace, lineEndIncludingNewline, hardBreak));
931-
this._lineWidths.Add(lineBreaker.getWidth(i));
948+
this._addLineRangeAndWidth(new LineRange(lineStart, lineEnd,
949+
lineEndExcludingWhitespace, lineEndIncludingNewline, hardBreak), lineBreaker.getWidth(i));
932950
}
933951
}
934952

935953
int _computeMaxWordCount() {
936954
int max = 0;
937-
for (int lineNumber = 0; lineNumber < this._lineRanges.Count; lineNumber++) {
955+
for (int lineNumber = 0; lineNumber < this._lineRangeCount; lineNumber++) {
938956
var inWord = false;
939957
int wordCount = 0, start = this._lineRanges[lineNumber].start, end = this._lineRanges[lineNumber].end;
940958
for (int i = start; i < end; ++i) {

Tests/Editor/CanvasAndLayers.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,7 @@ void drawParagraph() {
437437
this._meshPool);
438438
canvas.drawParagraph(paragraph, new Offset(10f, 100f));
439439
canvas.flush();
440+
Unity.UIWidgets.ui.Paragraph.release(ref paragraph);
440441
}
441442

442443
void drawImageRect() {

0 commit comments

Comments
 (0)