-
Apr 14th, 2021, 12:00 PM
#1
Thread Starter
Addicted Member
[RESOLVED] is there other Copymemory faster than WinAPI ?
Hi ALL~
the floowing code work in WinXP , but crash in Win7
is there other Copymemory faster than WinAPI ?
Code:
'https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=49798&lngWId=1
'===============================================================================
' cMemory - class to employ SIMD based memory routines. If the processor doesn't
' support MMX/SSE then we seemlessly fallback to the regular API
'
' This is the TEST version of this class - it allows me to compare MMX against
' SSE codes. For actual usage, use the version of this class in the cMemory.cls
' file. That version will use SSE codes if present else MMX codes.
'
Option Explicit
#Const open_debug_01 = 0
Public Enum eSIMD_Support
esNone = 0
esMMX
esSSE
esSSE2
esSSE3
End Enum
Private Declare Sub RtlMoveMemory Lib "kernel32" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Sub RtlFillMemory Lib "kernel32" (Destination As Any, ByVal Length As Long, ByVal Fill As Byte)
Private Const CODE_SIMD_LVL As String = "9C580FBAF815509D9C5A31D0740431C0EB2753B8010000000FA231C00FBAE2177316400FBAE219730F400FBAE21A7308400FBAE1007301405B8B542408890231C0C20800"
Private Const CODE_COPY_MMX As String = "56578B7C24108B7424148B4C241889C829F929C183E10729C87E210F77F3A489C183E007C1E903741329F70F6F060F7F043783C6084975F301F70F7701C1F3A45F5EC21000"
Private Const CODE_COPY_SSE As String = "56578B7C24108B7424148B4C241889C829F929C183E10F29C87E230F77F3A489C183E00FC1E904741529F7660F6F06660F7F043783C6104975F10F7701F701C1F3A45F5EC21000"
Private Const CODE_FILL_MMX As String = "578B7C240C8B4424108B54241489D1C1E10809CA89D1C1E21009CA89F983E10729C8E30A881783C70183E90175F689C183E007C1E90374180F7752520F6F042483C4080F7F0783C70883E90175F50F7789C1E30A881783C70183E90175F65FC21000"
Private Const CODE_FILL_SSE As String = "578B7C240C8B4424108B54241489D1C1E10809CA89D1C1E21009CA89F983E10F29C8E30A881783C70183E90175F689C183E00FC1E904741B0F7752525252660F6F042483C4100F7F0783C71083E90175F50F7789C1E30A881783C70183E90175F65FC21000"
Private pMe As Long 'Address of this class instance's vtable
Private nEntry As Long 'vtable entry index
Private sCode() As String 'Machine code lives here
Private Sub Class_Initialize()
Dim nSIMD As eSIMD_Support
Call RtlMoveMemory(pMe, ByVal ObjPtr(Me), 4) 'Get the address of this class instance's vtable
'Patch the SIMD_Support function
Call Redirect(CODE_SIMD_LVL)
nSIMD = SIMD_Support
If nSIMD > esNone Then
Call Redirect(CODE_COPY_MMX)
Call Redirect(CODE_FILL_MMX)
If nSIMD >= esSSE Then
Call Redirect(CODE_COPY_SSE)
'There may be an issue with SSE FillMemory on early P4's so I'm commenting it out for the moment
'Call Redirect(CODE_FILL_SSE)
End If
End If
End Sub
'Return the level of SIMD support
Public Function SIMD_Support() As eSIMD_Support
End Function
'If MMX is present then this sub is patched with the MMX RtlMoveMemory replacement machine code.
'If MMX isn't present then the sub isn't patched and stays as below.... seemlessly falls back to the api call
Public Sub CopyMemMMX(ByVal Source As Long, ByVal Destination As Long, ByVal Length As Long)
Call RtlMoveMemory(ByVal Source, ByVal Destination, Length)
End Sub
'If MMX is present then this sub is patched with the MMX RtlFillMemory replacement machine code.
'If MMX isn't present then the sub isn't patched and stays as below.... seemlessly falls back to the api call
Public Sub FillMemMMX(ByVal Source As Long, ByVal Length As Long, ByVal Fill As Byte)
Call RtlFillMemory(ByVal Source, Length, Fill)
End Sub
'If SSE is present then this sub is patched with the SSE RtlMoveMemory replacement machine code.
'If SSE isn't present then the sub isn't patched and stays as below.... seemlessly falls back to the api call
Public Sub CopyMemSSE(ByVal Source As Long, ByVal Destination As Long, ByVal Length As Long)
Call RtlMoveMemory(ByVal Source, ByVal Destination, Length)
End Sub
'If SSE is present then this sub is patched with the SSE RtlFillMemory machine code.
'If SSE isn't present then the sub isn't patched and stays as below.... seemlessly falls back to the api call
Public Sub FillMemSSE(ByVal Source As Long, ByVal Length As Long, ByVal Fill As Byte)
Call RtlFillMemory(ByVal Source, Length, Fill)
End Sub
Private Sub Redirect(ByVal sHexCode As String)
Dim i As Long
Dim nLen As Long
Dim s As String
nLen = Len(sHexCode)
For i = 1 To nLen Step 2
s = s & ChrB$(Val("&H" & MID$(sHexCode, i, 2)))
Next i
ReDim Preserve sCode(0 To nEntry)
sCode(nEntry) = s
Call RtlMoveMemory(ByVal pMe + &H1C + (nEntry * 4), StrPtr(sCode(nEntry)), 4)
nEntry = nEntry + 1
End Sub
-
Apr 14th, 2021, 06:49 PM
#2
Re: is there other Copymemory faster than WinAPI ?
I'm not sure how that code is any different, CopyMemory is just an alias for RtlMoveMemory...
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
-
Apr 14th, 2021, 07:09 PM
#3
Thread Starter
Addicted Member
Re: is there other Copymemory faster than WinAPI ?
Originally Posted by fafalone
I'm not sure how that code is any different, CopyMemory is just an alias for RtlMoveMemory...
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
the author say
' cMemory - class to employ SIMD based memory routines. If the processor doesn't
' support MMX/SSE then we seemlessly fallback to the regular API
-
Apr 15th, 2021, 04:11 AM
#4
Re: is there other Copymemory faster than WinAPI ?
Use memcpy from msvcrt. It supports SIMD.
-
Apr 15th, 2021, 04:36 AM
#5
Thread Starter
Addicted Member
Re: is there other Copymemory faster than WinAPI ?
Originally Posted by The trick
Use memcpy from msvcrt. It supports SIMD.
whether I need use your add-in ??
https://www.vbforums.com/showthread....ons-in-VB6-IDE
Last edited by quickbbbb; Apr 15th, 2021 at 04:45 AM.
-
Apr 15th, 2021, 04:50 AM
#6
Re: is there other Copymemory faster than WinAPI ?
Originally Posted by quickbbbb
whether I need use your add-in ??
https://www.vbforums.com/showthread....ons-in-VB6-IDE
what is the right format ??
the following is error format
Private Declare Function memcpy CDecl Lib "msvcrt" _
Alias "_memcpy" ( _
ByVal dest As Long, _
ByVal src As Long, _
ByVal size As Long, _
) As Long
As you prefer. Alternatively you could use tlb (without debugging in IDE) or DispCallFunc (slower).
Using Add-in you can write like:
Code:
Option Explicit
Private Declare Function memcpy CDecl Lib "msvcrt" ( _
ByRef dest As Any, _
ByRef src As Any, _
ByVal size As Long) As Long
Private Sub Form_Load()
Dim z() As Byte
Dim u() As Byte
Dim i As Long
ReDim z(&H1000000)
ReDim u(&H1000000)
For i = 0 To UBound(z)
z(i) = Int(Rnd * 255)
Next
memcpy u(0), z(0), UBound(u) + 1
End Sub
-
Apr 15th, 2021, 04:59 AM
#7
Thread Starter
Addicted Member
Re: is there other Copymemory faster than WinAPI ?
memcpy speed is only little difference for CopyMemory
=========================
Private Declare Function memcpy CDecl Lib "msvcrt" _
( _
ByVal dest As Long, _
ByVal src As Long, _
ByVal size As Long _
) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Private Sub Form_Load()
Dim w As Long, tt
Const QQ = 1000000
Dim a(1 To QQ) As Double
Dim b(1 To QQ) As Double
tt = Timer
For w = 1 To 1000
CopyMemory VarPtr(a(1)), VarPtr(a(2)), 8 * (QQ - 1)
Next
MsgBox Timer - tt
tt = Timer
For w = 1 To 1000
memcpy VarPtr(b(1)), VarPtr(b(2)), 8 * (QQ - 1)
Next
MsgBox Timer - tt
Stop
End Sub
-
Apr 15th, 2021, 05:39 AM
#8
Re: is there other Copymemory faster than WinAPI ?
I'm trying to understand what you're trying to do.....
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Apr 15th, 2021, 05:43 AM
#9
Re: is there other Copymemory faster than WinAPI ?
I'm trying to understand what you're trying to do.....
Looks like "shifting" an array "down" an element (a.k.a removing first element)
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Apr 15th, 2021, 09:25 AM
#10
Hyperactive Member
Re: is there other Copymemory faster than WinAPI ?
Originally Posted by quickbbbb
memcpy speed is only little difference for CopyMemory
=========================
Private Declare Function memcpy CDecl Lib "msvcrt" _
( _
ByVal dest As Long, _
ByVal src As Long, _
ByVal size As Long _
) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)
Private Sub Form_Load()
Dim w As Long, tt
Const QQ = 1000000
Dim a(1 To QQ) As Double
Dim b(1 To QQ) As Double
tt = Timer
For w = 1 To 1000
CopyMemory VarPtr(a(1)), VarPtr(a(2)), 8 * (QQ - 1)
Next
MsgBox Timer - tt
tt = Timer
For w = 1 To 1000
memcpy VarPtr(b(1)), VarPtr(b(2)), 8 * (QQ - 1)
Next
MsgBox Timer - tt
Stop
End Sub
Have you benchmarked against just using the native = to move your elements.
I have often found that the calling overhead is a large contributor to time although in your case your loop is quite short and the bytes it moves quite large so I don't know
edit
Maybe I don't understand what you are doing but on the old steam engine I use for VB I got 4454 ms for the copy memory loop and 3 milliseconds just saying b(1) = b(2)
Edit 2
I definitely don't understand what you are trying to do. The example code does not seem to copy anything as far as I can tell.
Last edited by vbwins; Apr 15th, 2021 at 09:45 AM.
-
Apr 15th, 2021, 09:47 AM
#11
Thread Starter
Addicted Member
Re: is there other Copymemory faster than WinAPI ?
Originally Posted by vbwins
Have you benchmarked against just using the native = to move your elements.
yes ..........
I use for VB I got 760 ms for the copy memory ( CPU I7 - 3770 DDR3 1600 )
Last edited by quickbbbb; Apr 15th, 2021 at 09:59 AM.
-
Apr 15th, 2021, 10:17 AM
#12
Hyperactive Member
Re: is there other Copymemory faster than WinAPI ?
What did you get not calling anything and just setting 1 array element to the other with =
-
Apr 15th, 2021, 10:31 AM
#13
Re: is there other Copymemory faster than WinAPI ?
Originally Posted by quickbbbb
the floowing code work in WinXP , but crash in Win7
The snippet would crash on XP when you turn on DEP for VBIDE or your application if you are testing it compiled.
The snippet is so ancient it mentions "early P4" CPUs which was when, early 90's?
The snippet makes no effort to heed DEP and mark the thunks with NX (reverse of non-executable) flag so it would crash on every modern version of Windows made this century :-)).
RtlMoveMemory already (probably) uses SIMD and that's why you see no discernable differences in speed on any of those micro-benchmarks.
cheers,
</wqw>
-
Apr 15th, 2021, 11:09 AM
#14
Thread Starter
Addicted Member
Re: is there other Copymemory faster than WinAPI ?
I only copy code from https://www.planet-source-code.com
but the website colosed now
RtlMoveMemory already (probably) uses SIMD and that's why you see no discernable differences in speed on any of those micro-benchmarks.
OK!
-
Apr 15th, 2021, 12:05 PM
#15
Re: is there other Copymemory faster than WinAPI ?
Originally Posted by quickbbbb
I know that this snippet is not yours and I just mention that it's ancient and its age shows and that's why there are problems with it.
Btw, there is a PSC mirror here: https://github.com/Planet-Source-Code
You can go to the site and search by the original submission ID: 49798 (this is the number after txtCodeId in ShowCode.asp?txtCodeId=49798&lngWId=1)
This submission by Paul Caton is fortunately preserved in the archive on github.
cheers,
</wqw>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|