Skip to content

[RFC]: replace static memory allocation of large arrays in C benchmarks with dynamic memory allocation (tracking issue) #8643

@kgryte

Description

@kgryte

Instructions

  1. Read the issue description below.
  2. Read the issue comment which follows the description.
  3. Search the code base for a package having a C benchmark which needs updating according to the description below.
  4. 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 #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 f0f8a70.

In particular, notice three things:

  1. Instead of a static allocation x[ len ], we declare a pointer *x.
  2. We perform dynamic memory allocation x = (double *) malloc(...).
  3. 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:

  1. Study the changes made in commit f0f8a70, as this commit contains the sorts of changes that we are looking for.
  2. Ensure you have setup your local development environment by following the contributing guide, including make install-node-modules and make init.
  3. 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 ];).
  4. 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/c folder of a package (if it exists).
  5. 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/.*".
  6. Commit your changes.
  7. Submit a PR updating the C benchmarks for that package (and only that package).
  8. 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

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

  • I have read and understood the Code of Conduct.
  • Searched for existing issues and pull requests.
  • The issue name begins with RFC:.

Metadata

Metadata

Assignees

No one assigned

    Labels

    AcceptedRFC feature request which has been accepted.BenchmarksPull requests adding or improving benchmarks for measuring performance.CIssue involves or relates to C.Good First IssueA good first issue for new contributors!RFCRequest for comments. Feature requests and proposed changes.Tracking IssueTracking issue.difficulty: 1Low degree of difficulty. Should be straightforward to implement and/or resolve.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions