LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Trim Whitespace.vi is not re-entrant. Why?


@sth wrote:

  Why this should be a big hit in performance either preallocated or shared is my question.


It's the strings that needs new buffers, and if they all do it at the same time (some 100+ instances was mentioned) it can be a memory hog while working. However, if the requirement to use Shared reentrancy is non-subroutine, i'd say it's okay to lower it to Time Critical. 🙂

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 41 of 95
(1,620 Views)

@mcduff wrote:

@drj

Not a member but I long time reader, lavag has given me a server error whenever I try to go there. Is it only open for members?

 

 


There is some kind of bug affecting LAVAg.org at the moment.  I have to log in to read it, too.

0 Kudos
Message 42 of 95
(1,612 Views)

@sth wrote:

@drjdpowell wrote:

Original conversation on OpenG Trim Whitespace.


That conversation was entirely on speed and not memory.  My guess is removing the reverse array will contribute to both but only noticeable for long strings.  


Why are you “guessing”?   You should be testing.  Personally, I would not expect “Reverse Array” to cost anything, as that primitive doesn’t actually reverse the bytes in memory (it just marks which end to index from).

0 Kudos
Message 43 of 95
(1,610 Views)

@drjdpowell wrote:

@sth wrote:

@drjdpowell wrote:

Original conversation on OpenG Trim Whitespace.


That conversation was entirely on speed and not memory.  My guess is removing the reverse array will contribute to both but only noticeable for long strings.  


Why are you “guessing”?   You should be testing.  Personally, I would not expect “Reverse Array” to cost anything, as that primitive doesn’t actually reverse the bytes in memory (it just marks which end to index from).


Yes, but implementing a good testing program for each small issue is a time consuming project and takes away from the idea of getting things done.   Actually more information about the LV internals makes things more transparent and testing can be just a time consuming reverse engineering exercise.  As you know, a simple benchmark is easy but doesn't tell you what you want to know, a complex benchmark that really tests the cases you need is a bigger undertaking.

 

BTW: The reverse array primitive shows a "buffer allocation" for the reverse array operation on the byte array in the OGTK version of trim white space.  Right there, that should make me "guess" that there is a performance hit.

 

Lastly, there seems to be a confusion about the shared clone / pre-allocated clone memory handling.  All the clones share instruction space so the length of code does not matter (or only 1 copy).  HOWEVER the difference is if the data space for the VI is allocated at compile time in the caller or at run time in the VI itself.

 

BUT, this is not true for strings and arrays!  There is no string memory preallocated for buffers in either type of reentrant VI.  There cannot be since the VI can handle any length string.  The handle (i.e. pointer) to the string on the heap is passed to the reentrant VI.  The memory for that handle is pre allocated NOT the string itself.  It is unknown how much memory to preallocate for an unknown length string.

 

The Trim Whitespace VI has to make at least one buffer copy from the input to the output which is a different string.  In my implementation that is in the "String Subset" operation.  All other buffer allocations are of small scalars.  In the OGTK version there are 2 allocations in the reverse array and the array subset operation.  In the NI version there are FOUR! allocations one each in the match string primitive and one at the boundary of each of the case statements.

 

If you are passing large strings then the allocations add up in reentrant VIs that can use the memory simultaneously.  By non-reentrant use one is merely using the same memory but allocating and reallocating it as necessary for each sequential call.

LabVIEW ChampionLabVIEW Channel Wires

0 Kudos
Message 44 of 95
(1,596 Views)

The buffer allocation tool is useful, but it doesn't always tell "the truth", the dots just may be possible allocations.

 

Not sure about your use case, but the speed of the Match String primitive can be improved if you are only matching one type of string, for example, assume in your use case you only have spaces, then matching \s is much faster than matching \s\n\r\t, etc.

 

I agree with Dr.J, you need to test and test, and yes that is annoying to do at times, so I also agree with you. But how many times is your function running, is it millions or only a few, you need to pick your test cases wisely.

0 Kudos
Message 45 of 95
(1,586 Views)

sth wrote:

BTW: The reverse array primitive shows a "buffer allocation" for the reverse array operation on the byte array in the OGTK version of trim white space.  Right there, that should make me "guess" that there is a performance hit.


Another reason why "copy dots" is a bad name for "buffer allocations"


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 46 of 95
(1,586 Views)

@mcduff wrote:

@Jeff

 

Works nicely! I will wait before I test it to see if Altenbach posts a faster solution. Smiley Very Happy

 

mcduff

 

 


Just because you asked

I modified The test bench from the OpenG link

TEST - Trim Whitespace (Performance).png

So, in the cases where you do not mind trimming more than whitespace,  Use Trim String

Let me make that FP shot a bit larger

Capture.png

In sth's use case (cleaning up VISA Strings) the result might be better anyway (Gets rid of control codes too)


"Should be" isn't "Is" -Jay
Message 47 of 95
(1,567 Views)

@Jeff

 

Nice!!

 

Ironically enough, I have having the same conversation, DrJ was there also, about the trim whitespace with respect to the JKI State machine on GitHub. I may have to try your solution. One thing that DrJ noticed was that if there is good chance of having an empty string, then having a case structure was much faster, this was true for the Match String Case. However the case structure needed to look like this, string wired directly to case structure:

 

Snip1.png

 

Snip2.png

 

0 Kudos
Message 48 of 95
(1,564 Views)

@Jeff

 

I am having trouble recreating your benchmark, what exactly did you do? Are you using the same function as you previously showed?

 

Thanks

0 Kudos
Message 49 of 95
(1,545 Views)

I was a bit worried about large string performance

TEST - Trim Whitespace (Performance).png

NOTE: The native Trim Whitespace.vi just could not play with the others and I had to unload the loop count 1000:1 to make it finish in a reasonable time.  OGTK loses some performance as expected but Trim String.vi is essentially un-affected.  I suspect there is a very smart search in the Threshhold 1D array.

 


"Should be" isn't "Is" -Jay
0 Kudos
Message 50 of 95
(1,544 Views)