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

Commit a2fe17f

Browse files
authored
Merge pull request #167 from UnityTech/kgdev
performance improvements...
2 parents 459df12 + 47c4c94 commit a2fe17f

18 files changed

+259
-252
lines changed

Runtime/foundation/basic_types.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using Unity.UIWidgets.ui;
45
using UnityEngine;
56
using Object = UnityEngine.Object;
67

@@ -133,21 +134,17 @@ public static string toStringList<T>(this IList<T> it) {
133134
return "{ " + string.Join(", ", it.Select(item => item.ToString())) + " }";
134135
}
135136

136-
public static void resize<T>(this List<T> list, int size, T value) {
137-
int curSize = list.Count;
138-
if (size < curSize) {
139-
list.RemoveRange(size, curSize - size);
140-
} else if(size > curSize) {
141-
if (size > list.Capacity) {
142-
list.Capacity = size;
143-
}
144-
list.AddRange(Enumerable.Repeat(value, size - curSize));
145-
}
146-
147-
int remains = Math.Min(curSize, size);
148-
for (int i = 0; i < remains; ++i) {
149-
list[i] = value;
150-
}
137+
public static void reset<T>(this List<T> list, int size) {
138+
NoAllocHelpers<T>.EnsureListElemCount(list, size);
139+
}
140+
141+
public static ref T refAt<T>(this List<T> list, int index) {
142+
var array = NoAllocHelpers<T>.ExtractArrayFromListT(list);
143+
return ref array[index];
144+
}
145+
146+
public static T[] array<T>(this List<T> list) {
147+
return NoAllocHelpers<T>.ExtractArrayFromListT(list);
151148
}
152149
}
153150
}

Runtime/material/user_accounts_drawer_header.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override Widget build(BuildContext context) {
3737
right: 0.0f,
3838
child: new Row(
3939
children: (this.otherAccountsPictures ?? new List<Widget> { })
40-
.GetRange(0, Math.Min(3, this.otherAccountsPictures?.Count ?? 0))
40+
.GetRange(0, Mathf.Min(3, this.otherAccountsPictures?.Count ?? 0))
4141
.Select<Widget, Widget>(
4242
(Widget picture) => {
4343
return new Padding(

Runtime/rendering/editable.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ public TextPosition getParagraphForward(TextPosition position, TextAffinity? aff
877877

878878
public TextPosition getParagraphBackward(TextPosition position, TextAffinity? affinity = null) {
879879
var lineCount = this._textPainter.getLineCount();
880+
880881
Paragraph.LineRange line = null;
881882
for (int i = lineCount - 1; i >= 0; --i) {
882883
line = this._textPainter.getLineRange(i);
@@ -988,8 +989,8 @@ public void selectPositionAt(Offset from = null, Offset to = null, SelectionChan
988989
int baseOffset = fromPosition.offset;
989990
int extentOffset = fromPosition.offset;
990991
if (toPosition != null) {
991-
baseOffset = Math.Min(fromPosition.offset, toPosition.offset);
992-
extentOffset = Math.Max(fromPosition.offset, toPosition.offset);
992+
baseOffset = Mathf.Min(fromPosition.offset, toPosition.offset);
993+
extentOffset = Mathf.Max(fromPosition.offset, toPosition.offset);
993994
}
994995

995996
TextSelection newSelection = new TextSelection(

Runtime/rendering/paragraph.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ public void selectPositionAt(Offset from = null, Offset to = null, SelectionChan
311311
int baseOffset = fromPosition.offset;
312312
int extentOffset = fromPosition.offset;
313313
if (toPosition != null) {
314-
baseOffset = Math.Min(fromPosition.offset, toPosition.offset);
315-
extentOffset = Math.Max(fromPosition.offset, toPosition.offset);
314+
baseOffset = Mathf.Min(fromPosition.offset, toPosition.offset);
315+
extentOffset = Mathf.Max(fromPosition.offset, toPosition.offset);
316316
}
317317

318318
TextSelection newSelection = new TextSelection(

Runtime/service/text_formatter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Text.RegularExpressions;
33
using Unity.UIWidgets.foundation;
4+
using UnityEngine;
45

56
namespace Unity.UIWidgets.service {
67
public abstract class TextInputFormatter {
@@ -60,8 +61,8 @@ public LengthLimitingTextInputFormatter(int? maxLength) {
6061
public override TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
6162
if (this.maxLength != null && this.maxLength > 0 && newValue.text.Length > this.maxLength) {
6263
TextSelection newSelection = newValue.selection.copyWith(
63-
baseOffset: Math.Min(newValue.selection.start, this.maxLength.Value),
64-
extentOffset: Math.Min(newValue.selection.end, this.maxLength.Value)
64+
baseOffset: Mathf.Min(newValue.selection.start, this.maxLength.Value),
65+
extentOffset: Mathf.Min(newValue.selection.end, this.maxLength.Value)
6566
);
6667

6768
string truncated = newValue.text.Substring(0, this.maxLength.Value);

Runtime/ui/painting/NoAllocHelpers.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using UnityEngine;
5+
6+
namespace Unity.UIWidgets.ui {
7+
public static class NoAllocHelpers<T> {
8+
9+
static Func<List<T>, T[]> _extractArrayFromListDelegate;
10+
11+
static Action<List<T>, int> _resizeListDelegate;
12+
13+
public static T[] ExtractArrayFromListT(List<T> list) {
14+
if (_extractArrayFromListDelegate == null) {
15+
var ass = Assembly.GetAssembly(typeof(Mesh));
16+
var type = ass.GetType("UnityEngine.NoAllocHelpers");
17+
var methodInfo = type.GetMethod(
18+
"ExtractArrayFromListT",
19+
BindingFlags.Static | BindingFlags.Public)
20+
.MakeGenericMethod(typeof(T));
21+
22+
_extractArrayFromListDelegate = (Func<List<T>, T[]>)
23+
Delegate.CreateDelegate(typeof(Func<List<T>, T[]>), methodInfo);
24+
}
25+
26+
return _extractArrayFromListDelegate(list);
27+
}
28+
29+
public static void ResizeList(List<T> list, int size) {
30+
if (size < list.Count) {
31+
list.RemoveRange(size, list.Count - size);
32+
return;
33+
}
34+
35+
if (size == list.Count) {
36+
return;
37+
}
38+
39+
if (list.Capacity < size) {
40+
list.Capacity = size;
41+
}
42+
43+
if (_resizeListDelegate == null) {
44+
var ass = Assembly.GetAssembly(typeof(Mesh)); // any class in UnityEngine
45+
var type = ass.GetType("UnityEngine.NoAllocHelpers");
46+
var methodInfo = type.GetMethod(
47+
"ResizeList",
48+
BindingFlags.Static | BindingFlags.Public)
49+
.MakeGenericMethod(typeof(T));
50+
_resizeListDelegate = (Action<List<T>, int>)
51+
Delegate.CreateDelegate(typeof(Action<List<T>, int>), methodInfo);
52+
}
53+
54+
_resizeListDelegate(list, size);
55+
}
56+
57+
public static void EnsureListElemCount(List<T> list, int size) {
58+
list.Clear();
59+
if (list.Capacity < size) {
60+
list.Capacity = size;
61+
}
62+
63+
ResizeList(list, size);
64+
}
65+
}
66+
}

Runtime/ui/painting/NoAllocHelpers.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/ui/painting/canvas_impl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ void _drawTextBlob(TextBlob textBlob, Offset offset, Paint paint) {
720720

721721
// request font texture so text mesh could be generated correctly
722722
var style = textBlob.style;
723-
var font = FontManager.instance.getOrCreate(textBlob.style.fontFamily, style.fontWeight, style.fontStyle).font;
723+
var font = FontManager.instance.getOrCreate(style.fontFamily, style.fontWeight, style.fontStyle).font;
724724
var fontSizeToLoad = Mathf.CeilToInt(style.UnityFontSize * scale);
725725
var subText = textBlob.text.Substring(textBlob.textOffset, textBlob.textSize);
726726
font.RequestCharactersInTextureSafe(subText, fontSizeToLoad, style.UnityFontStyle);

0 commit comments

Comments
 (0)