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

Commit 41d7879

Browse files
author
Yuncong Zhang
committed
Use a pool to manage the paragraphs.
1 parent 388fddc commit 41d7879

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

Runtime/painting/text_painter.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public TextPainter(TextSpan text = null,
3434
this._ellipsis = ellipsis;
3535
}
3636

37+
public void releaseParagraph() {
38+
Paragraph.release(ref this._paragraph);
39+
}
40+
3741

3842
public float textScaleFactor {
3943
get { return this._textScaleFactor; }
@@ -43,9 +47,9 @@ public float textScaleFactor {
4347
}
4448

4549
this._textScaleFactor = value;
46-
this._paragraph = null;
4750
this._layoutTemplate = null;
4851
this._needsLayout = true;
52+
Paragraph.release(ref this._paragraph);
4953
}
5054
}
5155

@@ -57,7 +61,7 @@ public string ellipsis {
5761
}
5862

5963
this._ellipsis = value;
60-
this._paragraph = null;
64+
Paragraph.release(ref this._paragraph);
6165
this._needsLayout = true;
6266
}
6367
}
@@ -74,7 +78,7 @@ public TextSpan text {
7478
}
7579

7680
this._text = value;
77-
this._paragraph = null;
81+
Paragraph.release(ref this._paragraph);
7882
this._needsLayout = true;
7983
}
8084
}
@@ -94,7 +98,7 @@ public TextDirection? textDirection {
9498
}
9599

96100
this._textDirection = value;
97-
this._paragraph = null;
101+
Paragraph.release(ref this._paragraph);
98102
this._layoutTemplate = null;
99103
this._needsLayout = true;
100104
}
@@ -108,7 +112,7 @@ public TextAlign textAlign {
108112
}
109113

110114
this._textAlign = value;
111-
this._paragraph = null;
115+
Paragraph.release(ref this._paragraph);
112116
this._needsLayout = true;
113117
}
114118
}
@@ -128,7 +132,7 @@ public int? maxLines {
128132
}
129133

130134
this._maxLines = value;
131-
this._paragraph = null;
135+
Paragraph.release(ref this._paragraph);
132136
this._needsLayout = true;
133137
}
134138
}

Runtime/rendering/paragraph.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ public override void detach() {
327327
if (this._hoverAnnotation != null) {
328328
RendererBinding.instance.mouseTracker.detachAnnotation(this._hoverAnnotation);
329329
}
330+
331+
this._textPainter?.releaseParagraph();
330332
}
331333

332334
TextSelection _selection;

Runtime/ui/txt/paragraph.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,25 @@ public float ideographicBaseline {
243243
public bool didExceedMaxLines {
244244
get { return this._didExceedMaxLines; }
245245
}
246+
247+
static List<Paragraph> _paragraphs = new List<Paragraph>();
248+
249+
public static Paragraph create() {
250+
if (_paragraphs.isEmpty()) {
251+
return new Paragraph();
252+
}
253+
254+
Paragraph ret = _paragraphs.last();
255+
_paragraphs.RemoveAt(_paragraphs.Count-1);
256+
return ret;
257+
}
258+
259+
public static void release(ref Paragraph paragraph) {
260+
if (paragraph != null) {
261+
_paragraphs.Add(paragraph);
262+
paragraph = null;
263+
}
264+
}
246265

247266
public void paint(Canvas canvas, Offset offset) {
248267
for (int i = 0; i < this._paintRecordsCount; i++) {

Runtime/ui/txt/paragraph_builder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public ParagraphBuilder(ParagraphStyle style) {
1515

1616
public Paragraph build() {
1717
this._runs.endRunIfNeeded(this._text.Length);
18-
var paragraph = new Paragraph();
18+
var paragraph = Paragraph.create();
1919
paragraph.setText(this._text.ToString(), this._runs);
2020
paragraph.setParagraphStyle(this._paragraphStyle);
2121
return paragraph;

0 commit comments

Comments
 (0)