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

Commit c1dda04

Browse files
committed
remove potential performance-relevant linq expr
1 parent 1478f5b commit c1dda04

17 files changed

+80
-47
lines changed

Runtime/flow/physical_shape_layer.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ public override void paint(PaintContext context) {
7777
context.canvas.restore();
7878
}
7979

80+
static Color _inAmbient;
81+
static Color _inSpot;
82+
static Color _ambientColor;
83+
static Color _spotColor;
84+
static Vector3 _zPlane = new Vector3();
85+
static Vector3 _devLight = new Vector3();
8086

8187
public static void drawShadow(Canvas canvas, Path path, Color color, float elevation, bool transparentOccluder,
8288
float dpr) {
@@ -88,19 +94,23 @@ public static void drawShadow(Canvas canvas, Path path, Color color, float eleva
8894
Rect bounds = path.getBounds();
8995
float shadow_x = (bounds.left + bounds.right) / 2f;
9096
float shadow_y = bounds.top - 600.0f;
91-
Color inAmbient = color.withAlpha((int) (kAmbientAlpha * color.alpha));
92-
Color inSpot = color.withAlpha((int) (kSpotAlpha * color.alpha));
93-
Color ambientColor = null;
94-
Color spotColor = null;
95-
ShadowUtils.computeTonalColors(inAmbient, inSpot, ref ambientColor, ref spotColor);
97+
_inAmbient = color.withAlpha((int) (kAmbientAlpha * color.alpha));
98+
_inSpot = color.withAlpha((int) (kSpotAlpha * color.alpha));
99+
_ambientColor = null;
100+
_spotColor = null;
101+
ShadowUtils.computeTonalColors(_inAmbient, _inSpot, ref _ambientColor, ref _spotColor);
102+
103+
_zPlane.Set(0, 0, dpr * elevation);
104+
_devLight.Set(shadow_x, shadow_y, dpr * kLightHeight);
105+
96106
ShadowUtils.drawShadow(
97107
canvas,
98108
path,
99-
new Vector3(0, 0, dpr * elevation),
100-
new Vector3(shadow_x, shadow_y, dpr * kLightHeight),
109+
_zPlane,
110+
_devLight,
101111
dpr * kLightRadius,
102-
ambientColor,
103-
spotColor,
112+
_ambientColor,
113+
_spotColor,
104114
0
105115
);
106116
}

Runtime/foundation/basic_types.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,14 @@ public static ref T refAt<T>(this List<T> list, int index) {
146146
public static T[] array<T>(this List<T> list) {
147147
return NoAllocHelpersBridge<T>.ExtractArrayFromListT(list);
148148
}
149+
150+
public static List<T> CreateRepeatedList<T>(T value, int length) {
151+
List<T> newList = new List<T>(length);
152+
for (int i = 0; i < length; i++) {
153+
newList.Add(value);
154+
}
155+
156+
return newList;
157+
}
149158
}
150159
}

Runtime/gestures/arena.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void sweep(int pointer) {
122122
D.assert(this._debugLogDiagnostic(
123123
pointer, () => $"Winner: {state.members.First()}"));
124124

125-
state.members.First().acceptGesture(pointer);
125+
state.members[0].acceptGesture(pointer);
126126
for (int i = 1; i < state.members.Count; i++) {
127127
state.members[i].rejectGesture(pointer);
128128
}
@@ -206,12 +206,11 @@ void _resolveByDefault(int pointer, _GestureArena state) {
206206

207207
D.assert(this._arenas[pointer] == state);
208208
D.assert(!state.isOpen);
209-
List<GestureArenaMember> members = state.members;
210-
D.assert(members.Count == 1);
209+
D.assert(state.members.Count == 1);
211210
this._arenas.Remove(pointer);
212211
D.assert(this._debugLogDiagnostic(pointer,
213212
() => $"Default winner: {state.members.First()}"));
214-
state.members.First().acceptGesture(pointer);
213+
state.members[0].acceptGesture(pointer);
215214
}
216215

217216
void _resolveInFavorOf(int pointer, _GestureArena state, GestureArenaMember member) {

Runtime/gestures/lsq_resolver.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class _Vector {
88
internal _Vector(int size) {
99
this._offset = 0;
1010
this._length = size;
11-
this._elements = Enumerable.Repeat(0.0f, size).ToList();
11+
this._elements = CollectionUtils.CreateRepeatedList(0.0f, size);
1212
}
1313

1414
_Vector(List<float> values, int offset, int length) {
@@ -49,7 +49,7 @@ public float norm() {
4949
class _Matrix {
5050
internal _Matrix(int rows, int cols) {
5151
this._columns = cols;
52-
this._elements = Enumerable.Repeat(0.0f, rows * cols).ToList();
52+
this._elements = CollectionUtils.CreateRepeatedList(0.0f, rows * cols);
5353
}
5454

5555
readonly int _columns;
@@ -71,7 +71,7 @@ public _Vector getRow(int row) {
7171

7272
public class PolynomialFit {
7373
public PolynomialFit(int degree) {
74-
this.coefficients = Enumerable.Repeat(0.0f, degree + 1).ToList();
74+
this.coefficients = CollectionUtils.CreateRepeatedList(0.0f, degree + 1);
7575
}
7676

7777
public readonly List<float> coefficients;

Runtime/gestures/multidrag.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using Unity.UIWidgets.async;
54
using Unity.UIWidgets.foundation;
65
using Unity.UIWidgets.ui;
@@ -238,7 +237,9 @@ void _removeState(int pointer) {
238237

239238

240239
public override void dispose() {
241-
this._pointers.Keys.ToList().ForEach(this._removeState);
240+
foreach (var key in this._pointers.Keys) {
241+
this._removeState(key);
242+
}
242243
D.assert(this._pointers.isEmpty);
243244
this._pointers = null;
244245
base.dispose();

Runtime/gestures/multitap.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
3-
using RSG.Promises;
42
using Unity.UIWidgets.async;
53
using Unity.UIWidgets.foundation;
64
using Unity.UIWidgets.ui;
@@ -170,7 +168,9 @@ void _registerSecondTap(_TapTracker tracker) {
170168
}
171169

172170
void _clearTrackers() {
173-
this._trackers.Values.ToList().Each(this._reject);
171+
foreach (var tracker in this._trackers.Values) {
172+
this._reject(tracker);
173+
}
174174
D.assert(this._trackers.isEmpty());
175175
}
176176

Runtime/gestures/velocity_tracker.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using Unity.UIWidgets.foundation;
54
using Unity.UIWidgets.ui;
65
using UnityEngine;
@@ -143,7 +142,7 @@ public class VelocityTracker {
143142
const int _minSampleSize = 3;
144143

145144
readonly List<_PointAtTime> _samples =
146-
Enumerable.Repeat<_PointAtTime>(null, _historySize).ToList();
145+
CollectionUtils.CreateRepeatedList<_PointAtTime>(null, _historySize);
147146

148147
int _index = 0;
149148

Runtime/rendering/table.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
32
using Unity.UIWidgets.foundation;
43
using Unity.UIWidgets.gestures;
54
using Unity.UIWidgets.painting;
@@ -268,7 +267,7 @@ public RenderTable(
268267
D.assert(rows == null || rows >= 0);
269268
D.assert(rows == null || children == null);
270269

271-
this._columns = columns ?? (children != null && children.isNotEmpty() ? children.First().Count : 0);
270+
this._columns = columns ?? (children != null && children.isNotEmpty() ? children[0].Count : 0);
272271
this._rows = rows ?? 0;
273272
this._children = new List<RenderBox>();
274273
for (int i = 0; i < this._columns * this._rows; i++) {
@@ -588,7 +587,7 @@ void setChildren(List<List<RenderBox>> cells) {
588587
}
589588

590589
this._children.Clear();
591-
this._columns = cells.isNotEmpty() ? cells.First().Count : 0;
590+
this._columns = cells.isNotEmpty() ? cells[0].Count : 0;
592591
this._rows = 0;
593592
foreach (List<RenderBox> row in cells) {
594593
this.addRow(row);
@@ -923,7 +922,7 @@ protected override void performLayout() {
923922
}
924923

925924
this._columnLefts = positions;
926-
tableWidth = positions.Last() + widths.Last();
925+
tableWidth = positions[positions.Count - 1] + widths[widths.Count - 1];
927926

928927
this._rowTops.Clear();
929928
this._baselineDistance = null;
@@ -1094,7 +1093,7 @@ public override void paint(PaintingContext context, Offset offset) {
10941093
D.assert(this._rows == this._rowTops.Count - 1);
10951094
D.assert(this._columns == this._columnLefts.Count);
10961095
if (this.border != null) {
1097-
Rect borderRect = Rect.fromLTWH(offset.dx, offset.dy, this.size.width, this._rowTops.Last());
1096+
Rect borderRect = Rect.fromLTWH(offset.dx, offset.dy, this.size.width, this._rowTops[this._rowTops.Count - 1]);
10981097
List<float> rows = this._rowTops.GetRange(1, this._rowTops.Count - 2);
10991098
List<float> columns = this._columnLefts.GetRange(1, this._columnLefts.Count - 1);
11001099
this.border.paint(context.canvas, borderRect, rows: rows, columns: columns);

Runtime/rendering/wrap.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using Unity.UIWidgets.foundation;
54
using Unity.UIWidgets.gestures;
65
using Unity.UIWidgets.painting;

Runtime/ui/painting/txt/mesh_generator.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using UnityEngine;
54

65
namespace Unity.UIWidgets.ui {
@@ -108,12 +107,19 @@ public static long frameCount {
108107
public static int meshCount {
109108
get { return _meshes.Count; }
110109
}
110+
111+
static readonly List<MeshKey> _keysToRemove = new List<MeshKey>();
111112

112113
public static void tickNextFrame() {
113114
_frameCount++;
114-
var keysToRemove = _meshes.Values.Where(info => info.timeToLive < _frameCount)
115-
.Select(info => info.key).ToList();
116-
foreach (var key in keysToRemove) {
115+
_keysToRemove.Clear();
116+
foreach (var info in _meshes.Values) {
117+
if (info.timeToLive < _frameCount) {
118+
_keysToRemove.Add(info.key);
119+
}
120+
}
121+
122+
foreach (var key in _keysToRemove) {
117123
_meshes.Remove(key);
118124
}
119125
}

0 commit comments

Comments
 (0)