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

Commit 23761d1

Browse files
committed
performance improvements...
1 parent f744058 commit 23761d1

19 files changed

+270
-262
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/physics/friction_simulation.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public static FrictionSimulation through(float startPosition, float endPosition,
3535
readonly float _v;
3636

3737
static float _dragFor(float startPosition, float endPosition, float startVelocity, float endVelocity) {
38-
return Mathf.Pow((float) Math.E, (startVelocity - endVelocity) / (startPosition - endPosition));
38+
return Mathf.Pow( Mathf.Epsilon, (startVelocity - endVelocity) / (startPosition - endPosition));
3939
}
4040

4141
public override float x(float time) {

Runtime/physics/spring_simulation.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ float velocity
140140
readonly float _r, _c1, _c2;
141141

142142
public override float x(float time) {
143-
return ((this._c1 + this._c2 * time) * Mathf.Pow((float) Math.E, this._r * time));
143+
return ((this._c1 + this._c2 * time) * Mathf.Pow(Mathf.Epsilon, this._r * time));
144144
}
145145

146146
public override float dx(float time) {
147-
float power = Mathf.Pow((float) Math.E, this._r * time);
147+
float power = Mathf.Pow(Mathf.Epsilon, this._r * time);
148148
return (this._r * (this._c1 + this._c2 * time) * power + this._c2 * power);
149149
}
150150

@@ -179,13 +179,13 @@ float velocity
179179
readonly float _r1, _r2, _c1, _c2;
180180

181181
public override float x(float time) {
182-
return (this._c1 * Mathf.Pow((float) Math.E, this._r1 * time) +
183-
this._c2 * Mathf.Pow((float) Math.E, this._r2 * time));
182+
return (this._c1 * Mathf.Pow(Mathf.Epsilon, this._r1 * time) +
183+
this._c2 * Mathf.Pow(Mathf.Epsilon, this._r2 * time));
184184
}
185185

186186
public override float dx(float time) {
187-
return (this._c1 * this._r1 * Mathf.Pow((float) Math.E, this._r1 * time) +
188-
this._c2 * this._r2 * Mathf.Pow((float) Math.E, this._r2 * time));
187+
return (this._c1 * this._r1 * Mathf.Pow(Mathf.Epsilon, this._r1 * time) +
188+
this._c2 * this._r2 * Mathf.Pow(Mathf.Epsilon, this._r2 * time));
189189
}
190190

191191
public override SpringType type {
@@ -219,12 +219,12 @@ float velocity
219219
readonly float _w, _r, _c1, _c2;
220220

221221
public override float x(float time) {
222-
return (Mathf.Pow((float) Math.E, this._r * time) *
222+
return (Mathf.Pow(Mathf.Epsilon, this._r * time) *
223223
(this._c1 * Mathf.Cos(this._w * time) + this._c2 * Mathf.Sin(this._w * time)));
224224
}
225225

226226
public override float dx(float time) {
227-
float power = Mathf.Pow((float) Math.E, this._r * time);
227+
float power = Mathf.Pow(Mathf.Epsilon, this._r * time);
228228
float cosine = Mathf.Cos(this._w * time);
229229
float sine = Mathf.Sin(this._w * time);
230230
return (power * (this._c2 * this._w * cosine - this._c1 * this._w * sine) +

Runtime/rendering/editable.cs

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

878878
public TextPosition getParagraphBackward(TextPosition position, TextAffinity? affinity = null) {
879879
var lineCount = this._textPainter.getLineCount();
880-
Paragraph.LineRange line = null;
880+
if (lineCount == 0) {
881+
return new TextPosition(position.offset, affinity ?? position.affinity);
882+
}
883+
884+
Paragraph.LineRange line = default;
881885
for (int i = lineCount - 1; i >= 0; --i) {
882886
line = this._textPainter.getLineRange(i);
883887
if (i != 0 && !this._textPainter.getLineRange(i - 1).hardBreak) {
@@ -988,8 +992,8 @@ public void selectPositionAt(Offset from = null, Offset to = null, SelectionChan
988992
int baseOffset = fromPosition.offset;
989993
int extentOffset = fromPosition.offset;
990994
if (toPosition != null) {
991-
baseOffset = Math.Min(fromPosition.offset, toPosition.offset);
992-
extentOffset = Math.Max(fromPosition.offset, toPosition.offset);
995+
baseOffset = Mathf.Min(fromPosition.offset, toPosition.offset);
996+
extentOffset = Mathf.Max(fromPosition.offset, toPosition.offset);
993997
}
994998

995999
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/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)