Slow String Performance

I just encountered slow string performance in LabVIEW when a large string is passed to a DLL as a C string pointer. It is always better to pass it as ‘Adapt to Type’ instead of a pointer.

Actually I would to implement fast string search on large string based on AVX2/AVX-512, and got strange penalty.

Here is the comparison between two ways.

“Traditional” way looks like this:

image-20250214140636323

“Advanced” way (and more convenient, because the length of the string will be passed as well):

image-20250214140747101

Let compare wot 1GiB string:

And the results:

image-20250214140926892

Its a difference by factor 50000x!

Behind the scenes of the two functions — it is just reading of the first byte:

Native String Pointer (just passed via rcx):

fnString PROC
	mov		al, [rcx]
	ret
ENDP fnString

Adapt to type (in rcx we have pointer to the structure, the second member is the address of the string):

fnHandle PROC
	mov     rax, [rcx]
	mov		al, [rax + 4] ; first 4 bytes - count
	ret
ENDP fnHandle

The reason is that when passed via C String Pointer, LabVIEW will create a copy of the whole string in the memory (because this string needs to be null-terminated), and in very inefficient way, bottleneck from profiler:

image-20250214141437789

Whole function (from Run-Time, which is affected as well) caused this:

image-20250214141523726

Refer to Assembly: REP MOVS mechanism / Enhanced REP MOVSB for memcpy.

Using “Adapt to Type” (or Pascal String pointer) you will avoid unnecessary copy.

Source code on the GitHub.