Skip to content
STIMSMITH

Memory Allocation Minimization

Technique WIKI v1 · 5/26/2026

Memory Allocation Minimization is a performance technique described in the RISCV-DV/eUVM context: reduce the number of runtime allocation requests, even for small or temporary values such as formatted strings, because each allocation can impose microsecond-scale runtime-manager overhead.

Overview

Memory Allocation Minimization focuses on reducing the number of calls that request new memory from the runtime memory manager. In the cited RISCV-DV/eUVM discussion, every memory allocation, regardless of size, may require several microseconds of runtime-manager processing. The performance issue is therefore tied strongly to allocation frequency, not only to the total amount of memory ultimately used.

Example: formatted immediates

The evidence describes a RISCV-DV instruction-randomization path where a RISC-V instruction's 32-bit immediate value is converted to a decimal string during post_randomize. In SystemVerilog, $sformatf is used to produce the formatted string. Because strings are variable-size arrays, $sformatf must allocate memory to return the string to the caller.

eUVM approach

The eUVM implementation avoids this trivial allocation by using a fixed-size character buffer inside the riscv_instr object. Since a 32-bit number formatted as a decimal string requires at most 10 characters, the implementation declares a fixed-size char array, imm_str_buffer, as part of the object. Because the buffer size is fixed, it is allocated with the containing class object rather than through a separate formatting-time allocation.

The D language, on which eUVM is built, provides two formatting styles in the cited discussion:

  • format, which behaves like SystemVerilog $sformatf by returning a formatted string.
  • sformat, which lets the user provide scratch memory where the formatted result is written, returning a pointer to the character array.

The eUVM example uses sformat with the preallocated scratch buffer and then casts the result to a string before storing it in imm_str.

Performance implication

The technique does not necessarily reduce the final amount of memory needed to hold the formatted string. Instead, it reduces the number of runtime allocation calls. In the cited example, the authors state that the total allocated memory eventually remains the same, but the number of malloc calls is reduced by half.

LINKED ENTITIES

1 links

CITATIONS

6 sources
6 citations
[1] Each memory allocation, whether large or small, may require several microseconds of runtime memory-manager processing. [PDF] Crafting a Million Instructions/Sec RISCV-DV - DVCon Proceedings
[2] SystemVerilog $sformatf allocates memory when returning a formatted string, because strings are variable-size arrays. [PDF] Crafting a Million Instructions/Sec RISCV-DV - DVCon Proceedings
[3] RISCV-DV invokes string formatting as part of post-randomization when randomizing a RISC-V instruction immediate. [PDF] Crafting a Million Instructions/Sec RISCV-DV - DVCon Proceedings
[4] eUVM avoids a formatting-time allocation by using a fixed-size character buffer inside the riscv_instr object for a 32-bit decimal immediate string. [PDF] Crafting a Million Instructions/Sec RISCV-DV - DVCon Proceedings
[5] D provides format and sformat functions, where sformat accepts user-specified scratch memory and returns a pointer to the resulting character array. [PDF] Crafting a Million Instructions/Sec RISCV-DV - DVCon Proceedings
[6] The cited optimization keeps the eventual amount of allocated memory the same but reduces the number of malloc calls by half. [PDF] Crafting a Million Instructions/Sec RISCV-DV - DVCon Proceedings