Author Topic: small refactoring in globals.cpp  (Read 12587 times)

Offline frithjofh

  • Regular
  • ***
  • Posts: 376
small refactoring in globals.cpp
« on: January 29, 2016, 05:44:37 pm »
hi everybody,

took a look into file globals.cpp and I prose some refactoring of three functions:

- general code clean up
- improved readability
- simplification of logic
- removed some duplication
- speed up measured to be about 14% due, I suppose, to more cache locality (all is relative: on my machine, with my test case,... )

regards

frithjof
architect with some spare time  -  c::b compiled from last svn  -   openSuSE leap x86_64  -  AMD FX-4100

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: small refactoring in globals.cpp
« Reply #1 on: January 29, 2016, 08:31:55 pm »
GetVectorFromString should not call wxArrayString, because there is an additional copy!
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline frithjofh

  • Regular
  • ***
  • Posts: 376
Re: small refactoring in globals.cpp
« Reply #2 on: January 29, 2016, 09:51:11 pm »
because first I have a const wxArrayString and then return a std::vector ?

my thoughts were: in the original function I have two vector's, one for the values in the function and the other as a copy during the return.

Now I have one wxArrayString and because the function is so "short" the unnamed object which is returned gets the benefit of RVO. so there are at most the same number of containers, at best even one less. the original function cannot count on RVO imho.

but maybe I get something wrong here...

thx for he feedback
architect with some spare time  -  c::b compiled from last svn  -   openSuSE leap x86_64  -  AMD FX-4100

Offline frithjofh

  • Regular
  • ***
  • Posts: 376
Re: small refactoring in globals.cpp
« Reply #3 on: January 29, 2016, 10:03:26 pm »
nah... I think you are right... there is an extra copy of the array... damm
architect with some spare time  -  c::b compiled from last svn  -   openSuSE leap x86_64  -  AMD FX-4100

Offline frithjofh

  • Regular
  • ***
  • Posts: 376
Re: small refactoring in globals.cpp
« Reply #4 on: January 29, 2016, 10:11:19 pm »
so maybe make a template function out of it in order to not textually duplicate the code... what do you think?

never mind... i am too tired to think strait
« Last Edit: January 29, 2016, 10:13:42 pm by frithjofh »
architect with some spare time  -  c::b compiled from last svn  -   openSuSE leap x86_64  -  AMD FX-4100

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: small refactoring in globals.cpp
« Reply #5 on: January 29, 2016, 10:23:25 pm »
A template is fine, but generally doesn't make much difference.
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline frithjofh

  • Regular
  • ***
  • Posts: 376
Re: small refactoring in globals.cpp
« Reply #6 on: January 29, 2016, 11:07:28 pm »
not for the size of executable, but to avoid the duplicate
architect with some spare time  -  c::b compiled from last svn  -   openSuSE leap x86_64  -  AMD FX-4100

Offline frithjofh

  • Regular
  • ***
  • Posts: 376
Re: small refactoring in globals.cpp
« Reply #7 on: January 30, 2016, 09:43:49 am »
factory recall !

found a strange error in my patch. sorry, haven't tested it enough, I think.

will post new patch these days
architect with some spare time  -  c::b compiled from last svn  -   openSuSE leap x86_64  -  AMD FX-4100

Offline frithjofh

  • Regular
  • ***
  • Posts: 376
Re: small refactoring in globals.cpp
« Reply #8 on: January 30, 2016, 11:12:04 am »
here the new version of the patch.

discovered, that assign() for wxArrayString does something else than it should. it appends the given values instead of replacing the contents of the wxArrayString...

if anyone is interested, here my measuring in msec for the functions returning an array/vector from a string:
item count - current arry - new array - current std::vec - new std::vec
5121010
1.2007070
2.500240261
5.0009321063
10.00040223956

regards
« Last Edit: February 04, 2016, 09:08:52 am by frithjofh »
architect with some spare time  -  c::b compiled from last svn  -   openSuSE leap x86_64  -  AMD FX-4100

Offline frithjofh

  • Regular
  • ***
  • Posts: 376
Re: small refactoring in globals.cpp
« Reply #9 on: February 04, 2016, 09:15:02 am »
reworked the next function: EscapeSpaces

speed up in msec:
words in text |current version |new version
3200
6410
12860
256280
512970
10243880
204815700
409662641

removed the comment about speeding this function up.

some minor changes

regards
architect with some spare time  -  c::b compiled from last svn  -   openSuSE leap x86_64  -  AMD FX-4100

Offline Alpha

  • Developer
  • Lives here!
  • *****
  • Posts: 1513
Re: small refactoring in globals.cpp
« Reply #10 on: February 04, 2016, 10:13:45 pm »
Have not tested, but it looks like GetStringFromArray() will have problems with an empty array.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: small refactoring in globals.cpp
« Reply #11 on: February 05, 2016, 01:07:41 am »
Why are you doing 3 iterations on the array in GetArrayFromString?
Why are you doing additional copy of the whole array?
And why are you doing Mid(a, npos-something) (hint happens when the array doesn't have a separator at the end)?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: small refactoring in globals.cpp
« Reply #12 on: February 05, 2016, 01:14:06 am »
Code: diff
-    if (!ret.IsEmpty() && ret[0] != _T('"') && ret[0] != _T('\''))
+    if (str.IsEmpty() || str.StartsWith(_T("\"")) || str.StartsWith(_T("\'")))

Why is this change needed? Why have you replaced the operator[] with startswith?
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline frithjofh

  • Regular
  • ***
  • Posts: 376
Re: small refactoring in globals.cpp
« Reply #13 on: February 05, 2016, 10:43:12 am »
@alpha

thx, you are right. I will add a test for empty array.
architect with some spare time  -  c::b compiled from last svn  -   openSuSE leap x86_64  -  AMD FX-4100

Offline frithjofh

  • Regular
  • ***
  • Posts: 376
Re: small refactoring in globals.cpp
« Reply #14 on: February 05, 2016, 11:41:39 am »
@obfuscated

doing three separate iterations on the array results in the code running much faster due to more cache locality. my measurements confirmed the speedup. the original version continuously jumped from one context to another. putting each action in a separate loop is just faster.

for instance, the original version repeated the test for the value of trimspaces in each iteration. now it is done only once.

same for the third iteration of removing empty entries.

the part of the extra whole copy I don't understand. do you mean in the return statement? if so, that would happen in the original version too. or is there another extra copy that slipped me? are you looking at the last version of the patch? the one before had the problem of the extra copy in the std:vector version of the function, but I already changed that.

and if there would be an extra copy, wouldn't the gained speed justify it?

as to your last point: hit and sunk. my functions have a problem with the case where there is no separator at the end. it just omits the part after the last separator. I will correct it, slipped me, thx for the find.
« Last Edit: February 05, 2016, 12:24:55 pm by frithjofh »
architect with some spare time  -  c::b compiled from last svn  -   openSuSE leap x86_64  -  AMD FX-4100