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_pageskeeps 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_pageswith an explicit mapping limit.- set_
mapping_ limit - Keeps an output-sized buffer on the heap up to
mapping_limitbytes.