Add mj_stackAllocInt to get correct size for allocating ints on mjData stack. Reducing stack memory usage by 10% - 15%.

PiperOrigin-RevId: 517672195
Change-Id: Ie14e6c9c99e8b9e1c5d3686b77631f2df0651c67
This commit is contained in:
Kyle Bayes
2023-03-18 13:03:11 -07:00
committed by Copybara-Service
parent fae5e822c0
commit 8696247f9d
17 changed files with 103 additions and 48 deletions
+15 -2
View File
@@ -819,7 +819,7 @@ static void makeDSparse(const mjModel* m, mjData* d) {
int* colind = d->D_colind;
mjMARKSTACK;
int* remaining = (int*)mj_stackAlloc(d, nv);
int* remaining = mj_stackAllocInt(d, nv);
// compute rownnz
memset(rownnz, 0, nv * sizeof(int));
@@ -912,7 +912,7 @@ static void makeBSparse(const mjModel* m, mjData* d) {
// allocate and clear incremental row counts
mjMARKSTACK;
int* cnt = (int*)mj_stackAlloc(d, nbody);
int* cnt = mj_stackAllocInt(d, nbody);
memset(cnt, 0, sizeof(int) * nbody);
// add subtree dofs to colind
@@ -1232,6 +1232,19 @@ mjtNum* mj_stackAlloc(mjData* d, int size) {
int* mj_stackAllocInt(mjData* d, int size) {
// optimize for mjtNum being twice the size of int
if (2*sizeof(int) == sizeof(mjtNum)) {
return (int*)mj_stackAlloc(d, (size + 1) >> 1);
}
// arbitrary bytes sizes
int new_size = (sizeof(int)*size + sizeof(mjtNum) - 1) / sizeof(mjtNum);
return (int*)mj_stackAlloc(d, new_size);
}
// clear data, set defaults
static void _resetData(const mjModel* m, mjData* d, unsigned char debug_value) {
//------------------------------ save plugin state and data