raz / commit 31f6696c

commit
31f6696c3f65a12d314439ffa29e34c166ab2069
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
69532854
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.

patch · log

1 file changed, +19 -2

src/gen.pas modified +19 -2

@@ -453,10 +453,26 @@
453453
454454 { Copying }
455455
456+{ size bytes from [rsi] to [rdi], through rcx; rsi and rdi are left
457+ as they fall. Up to 64 bytes, as a record or a small array mostly
458+ is, they go as moves of 8, 4, 2 and 1 bytes: rep movsb takes some
459+ twenty cycles to start, longer than the few moves take. }
456460 procedure Copy(size: int64);
461+var o, n: int64;
457462 begin
458- MovRI(RCX, size);
459- RepMovsb;
463+ if size > 64 then begin
464+ MovRI(RCX, size);
465+ RepMovsb;
466+ exit;
467+ end;
468+ o := 0;
469+ while o < size do begin
470+ n := 8;
471+ while n > size - o do n := n div 2;
472+ Load(RCX, Mem(RSI, o), n, false);
473+ Store(Mem(RDI, o), RCX, n);
474+ Inc(o, n);
475+ end;
460476 end;
461477
462478 procedure CopyToTemp(var x: TItem);
@@ -495,6 +511,7 @@
495511 Lea(RDI, Mem(RBP, tmp));
496512 Lea(RSI, MemRip(x.chunk, 0));
497513 Copy(x.val);
514+ Lea(RDI, Mem(RBP, tmp + x.val));
498515 MovRI(RCX, t^.len - x.val);
499516 MovRI(RAX, 0);
500517 RepStosb;