Skip to main content

Module heap

Module heap 

Source
Expand description

Process heap tuning for the request forms that allocate output storage.

Net::infer, Extractor::extract_owned and their stored variants hand back independent storage, so every request allocates an output-sized buffer and frees it again. The cost of that cycle belongs to the process heap rather than to the wrapper: a C library that serves an output-sized request from a fresh mapping and returns the pages to the kernel on free makes the next request fault the same pages again.

retain_pages keeps those pages mapped for the rest of the process. It is the in-library counterpart of the MALLOC_MMAP_THRESHOLD_ and MALLOC_TRIM_THRESHOLD_ environment variables that docs/performance.md records, and it exists because a service cannot always set the environment of its own process.

The thresholds are glibc settings. The Windows heap serves the same pattern from committed pages and faults almost nothing, and its public API exposes no equivalent threshold, so every function here reports false on that target and leaves the heap alone. crates/ncnn/tests/heap.rs measures both hosts: it proves the faults and their removal on glibc, and asserts the absence of the faults and the false result on Windows.

The setting is process-wide and holds for the rest of the run, like a global allocator, so an application calls it once at start-up. Two library-side forms remove the allocations instead and need no tuning: reusing a buffer through Net::infer_into or Net::infer_into removes the caller-side vector, and NetBuilder::reuse_blob_memory gives the network’s own intermediate tensors a pool that outlives each request.

Constants§

DEFAULT_RETAINED_LIMIT
The allocation size, in bytes, that retain_pages keeps on the heap.

Functions§

disable_trimming
Stops the heap from returning freed pages to the kernel.
retain_pages
Keeps the pages of freed buffers mapped so the next request does not fault them again.
retain_pages_up_to
retain_pages with an explicit mapping limit.
set_mapping_limit
Keeps an output-sized buffer on the heap up to mapping_limit bytes.