yakra / DataProcessing

Data Processing Scripts and Programs for Travel Mapping Project
0 stars 0 forks source link

Waypoint ctor: avoid unnecessary URL copy #256

Closed yakra closed 9 months ago

yakra commented 11 months ago

468 took from from 2 unnecessary allocations, copies & constructions of the URL string to 1.

We can do better, and get rid of that remaining unnecessary copy.

Instead of https://github.com/yakra/DataProcessing/blob/c51fd20e1f0b896d072bef7ba7740a8e260f8f2b/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp#L21-L30

Something like this ought to work:

// split line into fields
char* c = strchr(line, ' ');
if (c){ do *c = 0; while (*++c == ' ');
    while (char*d = strchr(c, ' '))
    {   do *d = 0; while (*++d == ' ');
        alt_labels.emplace_back(c);
        c = d;
    }
      }
label = line;

This doesn't push to alt_labels or update c until the following field is found. With nothing following the URL, c is left pointing to its beginning, and it can be parsed in place in the char* buffer. That's the simple part.

This could yield a decent speedup, especially under FreeBSD where we experience a minor memory bottleneck. (OTOH, much of this bottleneck may already disappear due to the TMArray conversion itself, which replaces on average 12.77 Waypoint allocations per route (and 5 vector reallocations) with one TMArray allocation, not to mention corresponding improvements the TMArray<HighwaySegment> conversion.)


Side topic: Search & destroy strcspn. The remaining cases can be similarly converted to strchr or strpbrk as appropriate. The difference may be small, but directly manipulating a pointer uses fewer ops than array indexing. Prefer strchr when there's only 1 delimiter, to avoid the overhead of looping thru 1-char strings. For each case, there may be other nearby minor improvements to make (e.g. in TravelerList ctor) as well. Open up a new issue when this one is closed.

Or not.

Asides: