Commit 33a24b1
chore: Small performance improvements (#3683)
## Purpose of this PR
@smitdylan2001 found and fixed some small performance improvements,
which do not change any functionality. Especially the position and
rotation calls and LINQ call removals are very helpful for performance
1. Merge looped `Add` calls on `List<T>` variables into `AddRange`
- `AddRange` will ensure the list is only extended once.
2. Optimize empty string checks
- Comparing strings using `String.Length` is faster than using `Equals`
([C# docs
reference](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1820?view=vs-2022#rule-description:~:text=Comparing%20strings%20using%20the%20String.Length%20property%20or%20the%20String.IsNullOrEmpty%20method%20is%20faster%20than%20using%20Equals))
3. Merge position & rotation calls into one
- `SetPositionAndRotation` has a small performance improvement
([docs](https://docs.unity3d.com/6000.2/Documentation/ScriptReference/Transform.SetPositionAndRotation.html#:~:text=When%20setting%20both%20the%20position%20and%20rotation%20of%20a%20transform%2C%20calling%20this%20method%20is%20more%20efficient%20than%20assigning%20to%20Transform.position%20and%20Transform.rotation%20individually.))
4. Short circuit operators for bools
- We had a couple of places that were using bytewise operations on
boolean properties. This fixes them.
5. Use native Count instead of LINQ
- `.Count()` uses LINQ and has a cost. `.Count` does the same thing
without the LINQ cost.
6. Use `StringBuilder.AppendJoin` rather than
`StringBuilder.Append(String.Join`
continues: #3680
## Jira ticket
n/a: contribution from external user
### Changelog
- Changed: made many very small performance improvements.
## Documentation
- Updated the scripting in the docs to use `SetPositionAndRotation`
rather than setting the two independently
## Testing & QA (How your changes can be verified during release
Playtest)
- These do not change functionality. Automated testing should catch any
compiler errors.
_Does the change require QA team to:_
- [ ] `Review automated tests`?
- [ ] `Execute manual tests`?
- [ ] `Provide feedback about the PR`?
If any boxes above are checked the QA team will be automatically added
as a PR reviewer.
## Backports
These are small performance improvements so they do not need to be
backported.
---------
Co-authored-by: Dylan Smit <[email protected]>
Co-authored-by: Unity Netcode CI <[email protected]>1 parent 5f16181 commit 33a24b1
File tree
34 files changed
+68
-148
lines changed- Examples
- CharacterControllerMovingBodies/Assets/Scripts
- OverridingScenesAndPrefabs/Assets/Scripts
- com.unity.netcode.gameobjects
- Documentation~/basics
- Editor
- CodeGen
- Configuration
- Runtime
- Components
- Helpers
- Configuration
- Core
- Messaging/Messages
- Spawning
- Tests/Runtime
- NetworkTransform
- NetworkVariable
- Prefabs
- Transports
- testproject
- Assets
- AddressableAssetsData
- Samples/PrefabPool
- Tests/Manual
- DeltaPositionNetworkTransform
- InSceneObjectParentingTests
- Scripts
- Packages
34 files changed
+68
-148
lines changedLines changed: 2 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
139 | | - | |
140 | | - | |
| 139 | + | |
141 | 140 | | |
142 | 141 | | |
143 | 142 | | |
| |||
173 | 172 | | |
174 | 173 | | |
175 | 174 | | |
176 | | - | |
177 | | - | |
| 175 | + | |
178 | 176 | | |
179 | 177 | | |
180 | 178 | | |
| |||
Lines changed: 4 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| |||
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
147 | | - | |
148 | | - | |
| 147 | + | |
149 | 148 | | |
150 | 149 | | |
151 | 150 | | |
| |||
182 | 181 | | |
183 | 182 | | |
184 | 183 | | |
185 | | - | |
| 184 | + | |
186 | 185 | | |
187 | 186 | | |
188 | 187 | | |
189 | 188 | | |
190 | 189 | | |
191 | | - | |
192 | | - | |
| 190 | + | |
193 | 191 | | |
194 | 192 | | |
195 | 193 | | |
| |||
Lines changed: 4 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
415 | 415 | | |
416 | 416 | | |
417 | 417 | | |
418 | | - | |
419 | | - | |
| 418 | + | |
420 | 419 | | |
421 | 420 | | |
422 | 421 | | |
| |||
522 | 521 | | |
523 | 522 | | |
524 | 523 | | |
525 | | - | |
| 524 | + | |
526 | 525 | | |
527 | 526 | | |
528 | 527 | | |
| |||
578 | 577 | | |
579 | 578 | | |
580 | 579 | | |
581 | | - | |
| 580 | + | |
582 | 581 | | |
583 | 582 | | |
584 | 583 | | |
585 | | - | |
| 584 | + | |
586 | 585 | | |
587 | 586 | | |
588 | 587 | | |
| |||
Lines changed: 1 addition & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
49 | | - | |
| 48 | + | |
50 | 49 | | |
51 | 50 | | |
52 | 51 | | |
| |||
64 | 63 | | |
65 | 64 | | |
66 | 65 | | |
67 | | - | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| |||
Lines changed: 2 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
153 | 153 | | |
154 | 154 | | |
155 | 155 | | |
156 | | - | |
157 | | - | |
| 156 | + | |
158 | 157 | | |
159 | 158 | | |
160 | 159 | | |
| |||
233 | 232 | | |
234 | 233 | | |
235 | 234 | | |
236 | | - | |
237 | | - | |
| 235 | + | |
238 | 236 | | |
239 | 237 | | |
240 | 238 | | |
| |||
Lines changed: 2 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1431 | 1431 | | |
1432 | 1432 | | |
1433 | 1433 | | |
1434 | | - | |
1435 | | - | |
1436 | | - | |
1437 | | - | |
1438 | | - | |
| 1434 | + | |
1439 | 1435 | | |
1440 | 1436 | | |
1441 | 1437 | | |
| |||
3101 | 3097 | | |
3102 | 3098 | | |
3103 | 3099 | | |
3104 | | - | |
3105 | | - | |
3106 | | - | |
3107 | | - | |
3108 | | - | |
| 3100 | + | |
3109 | 3101 | | |
3110 | 3102 | | |
3111 | 3103 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
58 | | - | |
59 | 58 | | |
60 | 59 | | |
61 | 60 | | |
| |||
76 | 75 | | |
77 | 76 | | |
78 | 77 | | |
79 | | - | |
80 | 78 | | |
81 | 79 | | |
82 | 80 | | |
| |||
129 | 127 | | |
130 | 128 | | |
131 | 129 | | |
132 | | - | |
| 130 | + | |
133 | 131 | | |
134 | 132 | | |
135 | 133 | | |
| |||
0 commit comments