Bigarray: do not change GC pace when creating subarrays or slices #12493
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a proposed fix for #12491.
The issue
When creating bigarrays, we increase the GC pacing in a way that should be proportional to the amount of "external" memory held by the bigarray. The bug in #12491 is that creating proxies (copies, slices) of an existing bigarray increases the GC speed as if new external memory was allocated, which is unnecessary (and hurts performance) as the proxy reuses memory from an existing bigarray.
The fix (details)
Before this PR, the logic was as follows:
caml_ba_alloc
takesflags
as input anddata
data
is NULL, malloc some memory for the bigarray and add theMANAGED
flag toflags
flags
has MANAGED, increase GC pacing on bigarray creationAfter this PR, the logic is as follows:
data
is NULL, set amust_alloc
booleanmust_alloc
, malloc some memorymust_alloc
, increase GC pacing on bigarray creationThe only change in behavior corresponds to calls that initially have the
MANAGED
flag inflags
, but a non-nulldata
field. Those would increase the GC pacing before the PR, and they do not increase it after the PR.This exactly covers the case of slices of subarrays, but also possibly some other cases of
caml_ba_alloc
calls in user code.I believe that the change is correct (a bugfix) in all these cases: if the caller of
caml_ba_alloc
passes existing memory and, at the same time, claims that this memory is owned by the OCaml heap, then we should not increase the GC pacing.