Repository navigation
Commit e36d52a
perf(math): build matrix results without zero-filling them first (#349)
* perf(math): build matrix results without zero-filling them first
Every Matrix operator that returns a new matrix value-initialised its
result (data = {}) and then overwrote every element. Without the O3
pragmas #348 removed, a consumer that builds at -O2 keeps those loops
rolled, so the zeroing survives. For a 3x3 result it is a memset call,
and the Arm GNU toolchain's newlib-nano memset stores one byte at a
time.
e-foc runs an RLS update in the control interrupt during mechanical
identification. Its covariance update, (P - g g^T / d) / lambda,
builds four 3x3 temporaries. In e-foc's SIL scenario that times the
control interrupt through a full calibration, the slowest execution
rose from 237 to 377 cycles of the emulated 25 MHz clock
(arm-none-eabi-gcc 15.2.1). The four memset calls were 596 of its 1892
instructions.
- Matrix gains a private constructor that leaves the elements
uninitialised. operator+, operator-, the matrix and scalar products,
Transpose, GetBlock and GetColumn build their results with it, since
each writes every element.
- The default and initializer-list constructors still zero-fill, so a
default-constructed or partly listed matrix keeps its zeros.
- A test evaluates those operators in constant expressions, so a
compiler that checks (Clang does, GCC does not) rejects an operator
that leaves an element unwritten.
The RLS update keeps its matrix expression and now makes no memset
calls. The same scenario peaks at 250 cycles.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HKhRVwu8yvxdKJgNo17oNc
* fix(math): build the matrix product's result in a member function
The matrix product is a friend function of its left operand's Matrix
specialisation. It built its result, a Matrix of another shape, with
that shape's private constructor. GCC and Clang allowed it, but MSVC
rejects it (C2248): friendship goes to the Matrix specialisations, not
to their friend functions.
The operator now calls a private member function, Multiply, which
builds the result in place, as Transpose, GetBlock and GetColumn
already do. Members of every Matrix specialisation are friends of the
others, so every compiler accepts it.
A static helper that returned the uninitialised result was not used:
GCC folds such an argument-less constexpr call into a zero-filled
constant, which brings back the memset calls.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HKhRVwu8yvxdKJgNo17oNc
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 31c7ac9 commit e36d52a
2 files changed
Lines changed: 61 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
| 66 | + | |
67 | 67 | | |
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
| 74 | + | |
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
| |||
80 | 80 | | |
81 | 81 | | |
82 | 82 | | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
| 83 | + | |
95 | 84 | | |
96 | 85 | | |
97 | 86 | | |
98 | 87 | | |
99 | | - | |
| 88 | + | |
100 | 89 | | |
101 | 90 | | |
102 | 91 | | |
| |||
121 | 110 | | |
122 | 111 | | |
123 | 112 | | |
124 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
125 | 126 | | |
126 | 127 | | |
127 | 128 | | |
| |||
141 | 142 | | |
142 | 143 | | |
143 | 144 | | |
144 | | - | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
145 | 148 | | |
146 | 149 | | |
147 | 150 | | |
| 151 | + | |
148 | 152 | | |
149 | 153 | | |
150 | 154 | | |
| |||
263 | 267 | | |
264 | 268 | | |
265 | 269 | | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
266 | 289 | | |
267 | 290 | | |
268 | 291 | | |
269 | 292 | | |
270 | | - | |
| 293 | + | |
271 | 294 | | |
272 | 295 | | |
273 | 296 | | |
| |||
328 | 351 | | |
329 | 352 | | |
330 | 353 | | |
331 | | - | |
| 354 | + | |
332 | 355 | | |
333 | 356 | | |
334 | 357 | | |
| |||
339 | 362 | | |
340 | 363 | | |
341 | 364 | | |
342 | | - | |
| 365 | + | |
343 | 366 | | |
344 | 367 | | |
345 | 368 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
427 | 427 | | |
428 | 428 | | |
429 | 429 | | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
430 | 448 | | |
431 | 449 | | |
432 | 450 | | |
| |||
0 commit comments