From 4329ff66989dc5b143e2524fb6143dba20f885d8 Mon Sep 17 00:00:00 2001 From: Jan Rysavy Date: Tue, 21 Jul 2026 11:35:48 +0200 Subject: [PATCH] Add Win64 in-place small-block realloc fast path --- FastMM5.pas | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/FastMM5.pas b/FastMM5.pas index 4d6aa4f..8657799 100644 --- a/FastMM5.pas +++ b/FastMM5.pas @@ -8104,7 +8104,7 @@ function FastMM_ReallocMem(APointer: Pointer; ANewSize: NativeInt): Pointer; mov eax, r8d and eax, (CBlockIsFreeFlag or CIsSmallBlockFlag) cmp eax, CIsSmallBlockFlag - je FastMM_ReallocMem_ReallocSmallBlock + je @SmallBlock mov eax, r8d and eax, (not CHasDebugInfoFlag) @@ -8120,6 +8120,29 @@ function FastMM_ReallocMem(APointer: Pointer; ANewSize: NativeInt): Pointer; xor edx,edx jmp HandleInvalidFreeMemOrReallocMem +@SmallBlock: + {Inline the common in-place small-block downsize path. This avoids the compiler-generated frame and saved registers + in FastMM_ReallocMem_ReallocSmallBlock on Win64.} + mov r9d, r8d + and r9d, CDropSmallBlockFlagsMask + shl r9d, CSmallBlockSpanOffsetBitShift + mov rax, rcx + and rax, -CMediumBlockAlignment + sub rax, r9 + mov rax, TSmallBlockSpanHeader(rax).SmallBlockManager + movzx r9d, TSmallBlockManager(rax).BlockSize + sub r9d, CSmallBlockHeaderSize + + {Upsizes and large downsizes use the existing moving path.} + cmp r9, rdx + jb FastMM_ReallocMem_ReallocSmallBlock + lea r10, [rdx * 4 + CSmallBlockDownsizeCheckAdder] + cmp r10, r9 + jb FastMM_ReallocMem_ReallocSmallBlock + + mov rax, rcx + ret + {$endif} {$else} var