[RFC]: replace static memory allocation of large arrays in C benchmarks with dynamic memory allocation (tracking issue)
Instructions
- Read the issue description below.
- Read the issue comment which follows the description.
- Search the code base for a package having a C benchmark which needs updating according to the description below.
- Follow any additional guidance specified in this issue.
Description
This RFC proposes improving C benchmarks by moving away from static memory allocation for large arrays to dynamic memory allocation for large arrays. Static memory allocation can be problematic due to the potential for segmentation faults for systems having limited memory (see https://github.com/stdlib-js/stdlib/issues/369 for context).
In short, this RFC seeks to refactor C benchmarks from, for example,
// ...
static double benchmark( int iterations, int len ) {
// ...
double x[ len ]; // <= static allocation
// ...
}
// ...
to
// ...
static double benchmark( int iterations, int len ) {
// ...
double *x;
// ...
x = (double *) malloc( len * sizeof( double ) );
// ...
free( x );
// ...
}
// ...
This refactoring is demonstrated in commit https://github.com/stdlib-js/stdlib/commit/f0f8a70989b6cfdabf6b928650c50b28aab5e224.
In particular, notice three things:
- Instead of a static allocation
x[ len ], we declare a pointer*x. - We perform dynamic memory allocation
x = (double *) malloc(...). - We free allocated memory
free( x ).
Steps
Given the relatively widespread practice of using static memory allocation in benchmarks, this RFC aims to be an open call for any contributor wanting to contribute to the project to do the following:
- Study the changes made in commit https://github.com/stdlib-js/stdlib/commit/f0f8a70989b6cfdabf6b928650c50b28aab5e224, as this commit contains the sorts of changes that we are looking for.
- Ensure you have setup your local development environment by following the contributing guide, including
make install-node-modulesandmake init. - Find a package containing a C benchmark which performs static memory allocation of potentially large arrays. A possible global project search could use the regular expression
(?:int8_t|uint8_t|int32_t|uint32_t|int64_t|uint64_t|double|float) [a-zA-Z0-9]+\[\s*len\s*\];. From the search results, you should be able to find a package in need of updating. You can ignore static allocations of small arrays (e.g.,int64_t shape[ 3 ];). - Update the C benchmarks for that package, and only that package, to migrate to use dynamic memory allocation. C benchmarks can be found in the
benchmarks/cfolder of a package (if it exists). - Run C benchmarks locally using the command
make benchmark-c BENCHMARKS_FILTER=".*/<package_name>/.*". For example,make benchmark-c BENCHMARKS_FILTER=".*/blas/ext/base/dfill/.*". - Commit your changes.
- Submit a PR updating the C benchmarks for that package (and only that package).
- For the PR title, use the following template "bench: refactor to use dynamic memory allocation in
<package_name>" where<package_name>is the name of the package you updated (e.g.,blas/ext/base/dfill).
Please do NOT make extraneous changes to benchmarks. We are not interested in changing benchmarks wholesale. We are only interested in replacing static memory allocation logic with dynamic memory allocation.
Related Issues
- https://github.com/stdlib-js/stdlib/issues/369
Questions
None.
Other
- If you are interested in working on this RFC, for each pull request, please only update the benchmarks for a single package.
- As mentioned above, please do NOT make extraneous changes to benchmarks. We are not interested in changing benchmarks wholesale. Nor are we interested in new "creative" changes. We are only interested in replacing static memory allocation with dynamic memory allocation. Failure to match the behavior of the existing benchmarks and to respect this guidance will result in your PRs being automatically closed without review.
- As this is a "Good First Issue", you are strongly encouraged to avoid using AI when authoring your contribution. One of the primary intents of Good First Issues is to help introduce you to stdlib, its development environment, and the contribution process, as documented in the contributing guide. Most new contributors are unfamiliar with stdlib and its conventions, and thus fail to appropriately use LLMs and AI when authoring contributions, most often generating AI slop and leading to wasted time. Don't be one of those people. :) Take the time to manually author your first several PRs, and, once you are intimately familiar with project conventions, you can consider leveraging AI to augment your dev tasks.
Checklist
- [x] I have read and understood the Code of Conduct.
- [x] Searched for existing issues and pull requests.
- [x] The issue name begins with
RFC:.
:wave: Important: PLEASE READ :wave:
This issue has been labeled as a good first issue and is available for anyone to work on.
If this is your first time contributing to an open source project, some aspects of the development process may seem unusual, arcane, or some combination of both.
- You cannot "claim" issues. People new to open source often want to "claim" or be assigned an issue before beginning work. The typical rationale is that people want to avoid wasted work in the event that someone else ends up working the issue. However, this practice is not effective in open source, as it often leads to "issue squatting", in which an individual asks to be assigned, is granted their request, and then never ends up working on the issue. Accordingly, you are encouraged to communicate your intent to address this issue, ideally by providing a rough outline as to how you plan to address the issue or asking clarifying questions, but, at the end of the day, we will take running code and rough consensus in order to move forward quickly.
- We have a very high bar for contributions. We have very high standards for contributions and expect all contributions—whether new features, tests, or documentation—to be rigorous, thorough, and complete. Once a pull request is merged into stdlib, that contribution immediately becomes the collective responsibility of all maintainers of stdlib. When we merge code into stdlib, we are saying that we, the maintainers, commit to reviewing subsequent changes and making bugfixes to the code. Hence, in order to ensure future maintainability, this naturally leads to a higher standard of contribution.
Before working on this issue and opening a pull request, please read the project's contributing guidelines. These guidelines and the associated development guide provide important information, including links to stdlib's Code of Conduct, license policy, and steps for setting up your local development environment.
To reiterate, we strongly encourage you to refer to our contributing guides before beginning work on this issue. Failure to follow our guidelines significantly decreases the likelihood that you'll successfully contribute to stdlib and may result in automatic closure of a pull request without review.
Setting up your local development environment is a critical first step, as doing so ensures that automated development processes for linting, license verification, and unit testing can run prior to authoring commits and pushing changes. If you would prefer to avoid manual setup, we provide pre-configured development containers for use locally or in GitHub Codespaces.
We place a high value on consistency throughout the stdlib codebase. We encourage you to closely examine other packages in stdlib and attempt to emulate the practices and conventions found therein.
- If you are attempting to contribute a new package, sometimes the best approach is to simply copy the contents of an existing package and then modify the minimum amount necessary to implement the feature (e.g., changing descriptions, parameter names, and implementation).
- If you are contributing tests, find a package implementing a similar feature and emulate the tests of that package.
- If you are updating documentation, examine several similar packages and emulate the content, style, and prose of those packages.
In short, the more effort you put in to ensure that your contribution looks and feels like stdlib—including variables names, bracket spacing, line breaks, etc—the more likely that your contribution will be reviewed and ultimately accepted. We encourage you to closely study the codebase before beginning work on this issue.
:sparkles: Thank you again for your interest in stdlib, and we look forward to reviewing your future contributions. :sparkles:
Hey @kgryte I’ve completed the refactor for one package as required. Before I proceed further, I wanted to confirm something: is it allowed to update multiple packages (i.e., all remaining dynamic allocation fixes) in a single PR, or should each package still be submitted as its own separate PR?
@farazghani The OP above answers that question.
Hey, I am intrested in working in this PR, can I proceed.