Skip to content

Skip redundant memset for large calloc allocations backed by OS-zeroed pages. - #1915

Draft
copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1019402138
Draft

copybara-service[bot] wants to merge 1 commit into
masterfrom
test_1019402138

Conversation

@copybara-service

Copy link
Copy Markdown

Skip redundant memset for large calloc allocations backed by OS-zeroed pages.

Large calloc allocations (such as Android bitmaps exceeding kMaxSize) that are satisfied from freshly mapped or MADV_DONTNEED-released memory are already guaranteed by the kernel to read as zero. Running memset across the entire allocation eagerly faults in every physical page, increasing memory footprint and allocation latency even for pages the caller never touches.

Propagate a zeroed status in PageAllocatorInterface::AllocationState (packed into existing struct padding with zero size increase) from HugePageAwareAllocator using existing unbacked-page metadata:

  • HugeCache allocations report zeroed = from_released when satisfied from HugeAllocator or released cache entries.
  • HugeRegion::Inc reports zeroed = true when every hugepage spanned by the allocation was previously unbacked in backed_.
  • HugePageFiller::TryGet reports zeroed = true when previously_unbacked == n (all pages were subreleased), and RefillFiller reports zeroed = from_released for the initial allocation on a newly refilled PageTracker.
  • HugePageAwareAllocator::Finalize clears zeroed if the SystemAllocator has ever released pages via MADV_FREE (has_released_with_madv_free()), or if a custom AddressRegionFactory is installed.

In do_calloc, large allocations (!is_small) now call slow_calloc_large, which skips memset when do_malloc_pages reports zeroed = true and no MallocHook::InvokeNewHook was invoked.

…d pages.

Large `calloc` allocations (such as Android bitmaps exceeding `kMaxSize`) that are satisfied from freshly mapped or `MADV_DONTNEED`-released memory are already guaranteed by the kernel to read as zero. Running `memset` across the entire allocation eagerly faults in every physical page, increasing memory footprint and allocation latency even for pages the caller never touches.

Propagate a `zeroed` status in `PageAllocatorInterface::AllocationState` (packed into existing struct padding with zero size increase) from `HugePageAwareAllocator` using existing unbacked-page metadata:
- `HugeCache` allocations report `zeroed = from_released` when satisfied from `HugeAllocator` or released cache entries.
- `HugeRegion::Inc` reports `zeroed = true` when every hugepage spanned by the allocation was previously unbacked in `backed_`.
- `HugePageFiller::TryGet` reports `zeroed = true` when `previously_unbacked == n` (all pages were subreleased), and `RefillFiller` reports `zeroed = from_released` for the initial allocation on a newly refilled `PageTracker`.
- `HugePageAwareAllocator::Finalize` clears `zeroed` if the `SystemAllocator` has ever released pages via `MADV_FREE` (`has_released_with_madv_free()`), or if a custom `AddressRegionFactory` is installed.

In `do_calloc`, large allocations (`!is_small`) now call `slow_calloc_large`, which skips `memset` when `do_malloc_pages` reports `zeroed = true` and no `MallocHook::InvokeNewHook` was invoked.

PiperOrigin-RevId: 1019402138
@google-cla

google-cla Bot commented Nov 12, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

This branch has not been deployed

No deployments
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.

0 participants