Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace dimension array access with simple index #23

Closed
chrispcampbell opened this issue Jul 10, 2020 · 2 comments · Fixed by #26
Closed

Replace dimension array access with simple index #23

chrispcampbell opened this issue Jul 10, 2020 · 2 comments · Fixed by #26
Assignees
Milestone

Comments

@chrispcampbell
Copy link
Contributor

SDEverywhere generates arrays of index numbers to help support subdimensions:

  function arrayDimensionsSection() {
    // Emit a declaration for each array dimension's index numbers.
    // These index number arrays will be used to indirectly reference array elements.
    // The indirection is required to support subdimensions that are a non-contiguous subset of the array elements.
    let a = R.map(dim => `const size_t ${dim.name}[${dim.size}] = { ${indexNumberList(sub(dim.name).value)} };`)
    let arrayDims = R.pipe(a, asort, lines)
    return arrayDims(allDimensions())
  }

These get declared at the top of the generated c file, for example:

// Array dimensions
const size_t _all_things[5] = { 0, 1, 2, 3, 4 };
const size_t _all_things_except_foo_and_bar[3] = { 1, 2, 4 };

When accessing values, these arrays are used as a form of indirection, for example:

for (i = 0; ...) {
  _something[_all_things[i]] = _another[_all_things[i]];
}

In En-ROADS, some subsets are non-contiguous, but in a majority of cases, these dimension index arrays are "trivial", meaning their elements are identical to their index/position. For those cases, we can generate more optimal code that skips the array access; the example above would become:

for (i = 0; ...) {
  _something[i] = _another[i];
}

Avoiding unnecessary array accesses leads to some more modest performance gains.

@chrispcampbell chrispcampbell self-assigned this Jul 10, 2020
@chrispcampbell
Copy link
Contributor Author

Here are some performance numbers relative to other recent optimizations.

Performance

MacBook Pro (2019) | 2.4 GHz 8-core i9, 32 GB RAM, macOS 10.15

Issue C run (ms) Wasm run (ms) Wasm init (ms) JS mem (MB) Page mem (MB)
baseline 45.8 87.5 38.0 94 685
SDE 18 46.0 85.6 18.0 39 672
SDE 19 42.8 49.4 15.0 38 25
SDE 22 34.8 44.8 15.0 38 21
SDE 23 32.7 42.8 13.0 38 32

iPhone 8 | A11, iOS 13

Issue C run (ms) Wasm run (ms) Wasm init (ms) JS mem (MB) Page mem (MB)
baseline 39.9 187.0 165.0 39 645
SDE 18 40.3 219.0 86.0 38 724
SDE 19 40.1 81.6 83.0 38 41
SDE 22 35.5 74.6 86.0 40 40
SDE 23 31.1 73.6 82.0 41 39

iPad Air (2013) | A7, iOS 12

Issue C run (ms) Wasm run (ms) Wasm init (ms) JS mem (MB) Page mem (MB)
baseline 151.0 1372.2 30146.0 77 331
SDE 18 166.0 1408.0 4416.0 42 395
SDE 19 151.0 837.6 1291.0 45 41
SDE 22 137.0 771.6 1484.0 44 40
SDE 23 110.1 642.2 1148.0 44 41

Size

Issue Wasm size (bytes)
baseline 1,084,036
SDE 18 773,968
SDE 19 776,851
SDE 22 737,028
SDE 23 741,668

Legend

Issue Date Notes
baseline 2020/07/08 baseline prior to performance work
SDE 18 2020/07/09 change lookup init to use static arrays
SDE 19 2020/07/09 break large functions into chunked subfunctions
SDE 22 2020/07/10 replace wrapper functions with macros
SDE 23 2020/07/10 replace dimension array access with simple index

@chrispcampbell
Copy link
Contributor Author

Merged to develop in 767a1c8.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant