microsoft / microsoft/mimalloc

Use mremap for `mi_realloc` on large objects

Open
#693 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stale
Dominant language
C
Stars
13.4k
Forks
1.2k
Avg merge
4d 45m
Merged PRs (30d)
13

Description

I'd like to be able to use mimalloc to manage my huge objects (arrays or hashmaps which are many gigabytes), not just my small objects. For arrays and hashmaps, we use `realloc` (in turn, `mi_realloc`) to grow the underlying storage. This ensures that, from the array/hashmap perspective, we atomically transition from having an `old_size` object in memory to having an `new_size` object in memory, without ever needing an `old_size+new_size` footprint.

Sadly, mimalloc's internal implementation breaks this invariant. It is internally implemented by allocating a new object, copying, and deallocating the old object.

I'd like the following extension to mimalloc:
* pick some `MMAP_THRESHOLD`, perhaps MMAP_THRESHOLD=64MiB
* all objects `>=MMAP_THRESHOLD` are allocated by a (non-cached) syscall to `mmap` (likely with appropriate HUGEPAGE flags)
* for `mi_realloc` where both the before and after size are `>=MMAP_THRESHOLD`, it is implemented by a (non-cached) syscall to `mremap`

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at mi_realloc and trace how large allocations are currently handled. Read the platform’s mmap and mremap interfaces and determine how the proposed MMAP_THRESHOLD, non-cached mappings, and huge-page flags would fit the allocator. Done means large allocations use mmap and eligible reallocations preserve the old-plus-new footprint invariant through mremap.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
operating-systems, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.