Improving performance of byte pointer copy

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7664
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Improving performance of byte pointer copy

Post by infratec »

Copy is working:

Code: Select all

EnableExplicit


Procedure CopyMemoryOso(*src, *dest, len.i)
  
  Structure TypeArray_Structure
    StructureUnion
      a.a
      u.u
      l.l
      q.q
    EndStructureUnion
  EndStructure
  
  Protected CopyLen.i
  Protected.TypeArray_Structure *srcptr, *destptr
  
  
  *srcptr  = *src        ; Set starting pointer
  *destptr = *dest       ; Set destination pointer
  
  CopyLen = len
  While CopyLen
    If CopyLen > 8
      *destptr\q = *srcptr\q      ; Copy the character
      *srcptr + 8                 ; Advance source pointer
      *destptr + 8                ; Advance destination pointer
      CopyLen - 8
    ElseIf CopyLen > 4
      *destptr\l = *srcptr\l      ; Copy the character
      *srcptr + 4                 ; Advance source pointer
      *destptr + 4                ; Advance destination pointer
      CopyLen - 4
    ElseIf CopyLen > 2
      *destptr\u = *srcptr\u      ; Copy the character
      *srcptr + 2                 ; Advance source pointer
      *destptr + 2                ; Advance destination pointer
      CopyLen - 2
    Else
      *destptr\a = *srcptr\a      ; Copy the character
      *srcptr + 1                 ; Advance source pointer
      *destptr + 1                ; Advance destination pointer
      CopyLen - 1
    EndIf
  Wend
  
EndProcedure


Define *Src, *Dest
Define t1.q, t2.q

*Src = AllocateMemory(20001, #PB_Memory_NoClear)
PokeQ(*Src, $0123456789ABCDEF)
PokeQ(*Src + MemorySize(*Src) - 8, $FEDCBA9876543210)
*Dest = AllocateMemory(MemorySize(*Src))

DisableDebugger
t1 = ElapsedMilliseconds()
CopyMemoryOso(*Src, *Dest, MemorySize(*Src))
t2 = ElapsedMilliseconds()
EnableDebugger
Debug t2 - t1
ShowMemoryViewer(*Dest, MemorySize(*Src))
User avatar
idle
Always Here
Always Here
Posts: 6040
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Improving performance of byte pointer copy

Post by idle »

infratec wrote: Thu Dec 21, 2023 10:17 pm Copy is working:

Code: Select all

EnableExplicit


Procedure CopyMemoryOso(*src, *dest, len.i)
  
  Structure TypeArray_Structure
    StructureUnion
      a.a
      u.u
      l.l
      q.q
    EndStructureUnion
  EndStructure
  
  Protected CopyLen.i
  Protected.TypeArray_Structure *srcptr, *destptr
  
  
  *srcptr  = *src        ; Set starting pointer
  *destptr = *dest       ; Set destination pointer
  
  CopyLen = len
  While CopyLen
    If CopyLen > 8
      *destptr\q = *srcptr\q      ; Copy the character
      *srcptr + 8                 ; Advance source pointer
      *destptr + 8                ; Advance destination pointer
      CopyLen - 8
    ElseIf CopyLen > 4
      *destptr\l = *srcptr\l      ; Copy the character
      *srcptr + 4                 ; Advance source pointer
      *destptr + 4                ; Advance destination pointer
      CopyLen - 4
    ElseIf CopyLen > 2
      *destptr\u = *srcptr\u      ; Copy the character
      *srcptr + 2                 ; Advance source pointer
      *destptr + 2                ; Advance destination pointer
      CopyLen - 2
    Else
      *destptr\a = *srcptr\a      ; Copy the character
      *srcptr + 1                 ; Advance source pointer
      *destptr + 1                ; Advance destination pointer
      CopyLen - 1
    EndIf
  Wend
  
EndProcedure


Define *Src, *Dest
Define t1.q, t2.q

*Src = AllocateMemory(20001, #PB_Memory_NoClear)
PokeQ(*Src, $0123456789ABCDEF)
PokeQ(*Src + MemorySize(*Src) - 8, $FEDCBA9876543210)
*Dest = AllocateMemory(MemorySize(*Src))

DisableDebugger
t1 = ElapsedMilliseconds()
CopyMemoryOso(*Src, *Dest, MemorySize(*Src))
t2 = ElapsedMilliseconds()
EnableDebugger
Debug t2 - t1
ShowMemoryViewer(*Dest, MemorySize(*Src))
It's a good solution 1st branch will be favored so the additional cmps won't hurt to much.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Improving performance of byte pointer copy

Post by Oso »

I've just been testing with random data also and yes it seems to work perfectly.

It seemed to successfully copy any type of raw data, not only strings, hence my earlier question about whether \c could copy passively.
Last edited by Oso on Thu Dec 21, 2023 11:00 pm, edited 1 time in total.
infratec
Always Here
Always Here
Posts: 7664
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Improving performance of byte pointer copy

Post by infratec »

The question is ... why :?:

CopyMemory() is inbuild and working.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Improving performance of byte pointer copy

Post by Oso »

infratec wrote: Thu Dec 21, 2023 10:59 pm The question is ... why :?:

CopyMemory() is inbuild and working.
In some cases I have to manipulate the data, so it isn't always a straight copy.
Post Reply