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

refactor(profiling): start porting memalloc to C++ #12519

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nsrip-dd
Copy link
Contributor

Python profiling is implemented in several different programming
languages: Python, Cython, C, C++, and we also call out to a Rust
library. Particularly on the native side, that's a lot of cognitive load
to work on this code. In the long term we should try to consolidate the
code as much as we reasonably can, so it's easier to work on. Most of
the native code is C++. The memory profiler, memalloc, is an outlier and
is written in C. This PR starts porting memalloc to C++. There are a few
benefits I expect to get from this:

  • memalloc uses a dynamic array type implemented as a C preprocessor
    macro. I have already found this hard to debug, for example when I
    found out it was prone to integer arithmetic bugs when the index type
    was set to a 16-bit unsigned integer. We shouldn't even be able to do
    that for a container. C++ has vectors that do this, but better.
  • memalloc has its own platform-specific mutex and atomic code. C++ has
    mutex and atomic support in the standard library, with support for the
    platforms we're interested in.
  • C++ has other data structures in its standard library. For example, it
    has has tables. I expect that heap profiling will have poor scaling
    behavior for heaps with lots of objects, and our tracking data
    structure is an array. Having the door open to easily switch to a
    different data structure, without having to roll our own in C or add
    a 3rd-party dependency, would be good.
  • The code is in C already, and while C++ is a substantially different
    language, it's similar enough that we can do a gradual bug-for-bug
    translation and be able to reason about its correctness (or at least
    lack of significant deviance from past behavior) as we go. Rust has
    all the things listed above, but it would be a much bigger jump to
    go from C to a basic working Rust version than to go to C++.
    Eventually we might be able to simplify this code enough in C++ that a
    Rust translation actually seems easy. So Rust isn't off the table.

This commit starts by renaming the memalloc files and switching some of
the dynamic array types to std::vectors. It otherwise makes no
significant changes; just enough to keep it compiling. Followup changes
will port over the synchronization code and make the traceback_t
implementation a bit easier to understand (e.g. using a vector rather
than allocating space for frames past the end of the actual traceback_t
struct).

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

Python profiling is implemented in several different programming
languages: Python, Cython, C, C++, and we also call out to a Rust
library. Particularly on the native side, that's a lot of cognitive load
to work on this code. In the long term we should try to consolidate the
code as much as we reasonably can, so it's easier to work on. Most of
the native code is C++. The memory profiler, memalloc, is an outlier and
is written in C. This PR starts porting memalloc to C++. There are a few
benefits I expect to get from this:

- memalloc uses a dynamic array type implemented as a C preprocessor
  macro. I have already found this hard to debug, for example when I
  found out it was prone to integer arithmetic bugs when the index type
  was set to a 16-bit unsigned integer. We shouldn't even be able to do
  that for a container. C++ has vectors that do this, but better.
- memalloc has its own platform-specific mutex and atomic code. C++ has
  mutex and atomic support in the standard library, with support for the
  platforms we're interested in.
- C++ has other data structures in its standard library. For example, it
  has has tables. I expect that heap profiling will have poor scaling
  behavior for heaps with lots of objects, and our tracking data
  structure is an array. Having the door open to easily switch to a
  different data structure, without having to roll our own in C or add
  a 3rd-party dependency, would be good.
- The code is in C already, and while C++ is a substantially different
  language, it's similar enough that we can do a gradual bug-for-bug
  translation and be able to reason about its correctness (or at least
  lack of significant deviance from past behavior) as we go. Rust has
  all the things listed above, but it would be a much bigger jump to
  go from C to a basic working Rust version than to go to C++.
  Eventually we might be able to simplify this code enough in C++ that a
  Rust translation actually seems easy. So Rust isn't off the table.

This commit starts by renaming the memalloc files and switching some of
the dynamic array types to std::vectors. It otherwise makes no
significant changes; just enough to keep it compiling. Followup changes
will port over the synchronization code and make the traceback_t
implementation a bit easier to understand (e.g. using a vector rather
than allocating space for frames past the end of the actual traceback_t
struct).
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 this pull request may close these issues.

1 participant