From 31f6696c3f65a12d314439ffa29e34c166ab2069 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Sun, 11 Oct 2026 23:39:42 +0100 Subject: [PATCH] gen: copy up to 64 bytes as moves, not rep movsb rep movsb takes some twenty cycles to start, so copying a small record cost far more than its bytes: in rat, a line feed copies 80 cells of 16 bytes and 24 lines of 24, and a flood (seq 1 300000 to rat -dump 80 24) went from 1.03 s to 0.42 s. Copy now moves up to 64 bytes in 8, 4, 2 and 1 byte pieces through rcx; rsi and rdi are left as they fall, so Coerce points rdi past a string itself before filling its array. --- diff --git a/src/gen.pas b/src/gen.pas index fb4d2a7..f4d0cfe 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -453,10 +453,26 @@ { Copying } +{ size bytes from [rsi] to [rdi], through rcx; rsi and rdi are left + as they fall. Up to 64 bytes, as a record or a small array mostly + is, they go as moves of 8, 4, 2 and 1 bytes: rep movsb takes some + twenty cycles to start, longer than the few moves take. } procedure Copy(size: int64); +var o, n: int64; begin - MovRI(RCX, size); - RepMovsb; + if size > 64 then begin + MovRI(RCX, size); + RepMovsb; + exit; + end; + o := 0; + while o < size do begin + n := 8; + while n > size - o do n := n div 2; + Load(RCX, Mem(RSI, o), n, false); + Store(Mem(RDI, o), RCX, n); + Inc(o, n); + end; end; procedure CopyToTemp(var x: TItem); @@ -495,6 +511,7 @@ Lea(RDI, Mem(RBP, tmp)); Lea(RSI, MemRip(x.chunk, 0)); Copy(x.val); + Lea(RDI, Mem(RBP, tmp + x.val)); MovRI(RCX, t^.len - x.val); MovRI(RAX, 0); RepStosb;