Skip to content

LABEL_SELFREF efficiency #173

Closed
Closed

Description

@yakra

if '_' in w.label[w.label.index('/')+1:] and w.label[w.label.index('/')+1:w.label.rindex('_')] == r.route[digit_starts+1:]:
selfref_found = True
if '_' in w.label[w.label.index('/')+1:] and w.label[w.label.index('/')+1:w.label.rindex('_')] == r.route:
selfref_found = True

Proposed:

	if '_' in w.label[w.label.index('/')+1:]:
            if w.label[w.label.index('/')+1:w.label.rindex('_')] == r.route[digit_starts+1:] \
	    or w.label[w.label.index('/')+1:w.label.rindex('_')] == r.route:
		selfref_found = True

Activity

  1. self-assigned this
    on Jan 17, 2019
  2. yakra commented on Jan 17, 2019

    @yakra
    ContributorAuthor

    Preceding ifs can also be combined or become elifs.
    Compare whatever I end up doing in the C++ version.

  3. yakra commented on Jan 17, 2019

    @yakra
    ContributorAuthor

    semi-related: check whether usanyp examples are flagged

  4. yakra commented on Jan 17, 2019

    @yakra
    ContributorAuthor

    ./siteupdate.py -e -l logs -c stats -u /home/yakra/TravelMapping/UserData/list_files -w /home/yakra/TravelMapping/HighwayData -t 1 | tee siteupdate_s1.log
    ython/selfref.py -e -l logs -c stats -u /home/yakra/TravelMapping/UserData/list_files -w /home/yakra/TravelMapping/HighwayData -t 1 | tee siteupdate_s1.log


    Before:
    [355.2] Performing data checks...
    [383.2] Found 11190 datacheck errors.

    After:
    [416.2] Performing data checks...
    [443.7] Found 11119 datacheck errors.

    There's work to do.

    • What datachecks are missing?
    • How does the C++ version compare?
  5. yakra commented on Jan 17, 2019

    @yakra
    ContributorAuthor

    datacheck.log

    1c1
    < Log file created at: 2019-01-17 13:51:14.011156
    ---
    > Log file created at: 2019-01-17 13:37:17.725312
    1351d1350
    < ita.ss125;SS125/199;;;LABEL_SELFREF;

    unmatchedfps.log

    1c1
    < Log file created at: 2019-01-17 13:51:14.010612
    ---
    > Log file created at: 2019-01-17 13:37:17.724615
    142a143,146
    > deubw.b010gin;B10/B466;;;LABEL_SELFREF;
    > deubw.l0220rei;L220/L221;;;LABEL_SELFREF;
    > deuby.st2309mil;St2309/2310;;;LABEL_SELFREF;
    > deunw.l403ric;L403/L353;;;LABEL_SELFREF;
    143a148,160
    > eng.a0010;A10/A414;;;LABEL_SELFREF;
    > eng.a0036owe;A36/A3090;;;LABEL_SELFREF;
    > fl.us017;US17/92Trk_S;;;LABEL_SELFREF;
    > ga.us001;US1/23Bus_N;;;LABEL_SELFREF;
    > ga.us019;US19/41Bus_N;;;LABEL_SELFREF;
    > ga.us019;US19/82Bus;;;LABEL_SELFREF;
    > ga.us025;US25/301Byp;;;LABEL_SELFREF;
    > ga.us129;US129/341Bus;;;LABEL_SELFREF;
    > ga.us129;US129/441Bus_N;;;LABEL_SELFREF;
    > ga.us129;US129/441Bus_S;;;LABEL_SELFREF;
    > ga.us129;US129/441BusWat_N;;;LABEL_SELFREF;
    > ga.us129;US129/441BusWat_S;;;LABEL_SELFREF;
    > ga.us129;US129/441Byp_S;;;LABEL_SELFREF;
    152a170,171
    > irl.r105;R105/R106_A;;;LABEL_SELFREF;
    > irl.r105;R105/R106_B;;;LABEL_SELFREF;
    157a177,181
    > ky.ky0015cam;KY15/191;;;LABEL_SELFREF;
    > ky.us025;US25/421Bus_N;;;LABEL_SELFREF;
    > ky.us025;US25/421Bus_S;;;LABEL_SELFREF;
    > ky.us127;US127/150Byp;;;LABEL_SELFREF;
    > la.us079;US79/80Trk;;;LABEL_SELFREF;
    165a190,192
    > nd.us052;US52/281Byp;;;LABEL_SELFREF;
    > nj.us001;US1/9Trk;;;LABEL_SELFREF;
    > nm.us054;US54/70Bus;;;LABEL_SELFREF;
    168a196
    > nv.us050;US50/95Alt;;;LABEL_SELFREF;
    173a202,242
    > sc.us001;US1/21Con;;;LABEL_SELFREF;
    > sc.us001;US1/521Trk;;;LABEL_SELFREF;
    > sc.us001;US1/52Trk_N;;;LABEL_SELFREF;
    > sc.us001;US1/52Trk_S;;;LABEL_SELFREF;
    > sc.us001;US1/78Trk;;;LABEL_SELFREF;
    > sc.us001;US1/78Trk_W;;;LABEL_SELFREF;
    > sc.us015;US15/301Con;;;LABEL_SELFREF;
    > sc.us021;US21/76Con;;;LABEL_SELFREF;
    > sc.us025;US25/178Bus_N;;;LABEL_SELFREF;
    > sc.us025;US25/276Con;;;LABEL_SELFREF;
    > sc.us029;US29/20Con;;;LABEL_SELFREF;
    > sc.us176;US176/221Con;;;LABEL_SELFREF;
    > sc.us221;US221/176ConSpa;;;LABEL_SELFREF;
    > sc.us221;US221/176ConSpar;;;LABEL_SELFREF;
    > sc.us301;US301/521Con;;;LABEL_SELFREF;
    > sc.us521;US521/601Bus_S;;;LABEL_SELFREF;
    > sc.us521;US521/601Trk_N;;;LABEL_SELFREF;
    > swe.r023vax;R23/R25;;;LABEL_SELFREF;
    > tn.us079;US79/70AltByp;;;LABEL_SELFREF;
    > ut.us006;US6/191Bus_N;;;LABEL_SELFREF;
    > ut.us006;US6/191Bus_S;;;LABEL_SELFREF;
    > va.us011;US11/19Trk;;;LABEL_SELFREF;
    > va.us011;US11/250Trk;;;LABEL_SELFREF;
    > va.us011;US11/460Alt;;;LABEL_SELFREF;
    > va.us011;US11/460Alt_W;;;LABEL_SELFREF;
    > va.us013;US13/58Bus_E;;;LABEL_SELFREF;
    > va.us015;US15/29BusRem_N;;;LABEL_SELFREF;
    > va.us015;US15/29BusRem_S;;;LABEL_SELFREF;
    > va.us015;US15/29BusWar_N;;;LABEL_SELFREF;
    > va.us015;US15/29BusWar_S;;;LABEL_SELFREF;
    > va.us015;US15/360Bus;;;LABEL_SELFREF;
    > va.us015;US15/360Bus_N;;;LABEL_SELFREF;
    > va.us015;US15/360Bus_S;;;LABEL_SELFREF;
    > va.us015;US15/460Bus_N;;;LABEL_SELFREF;
    > va.us019;US19/460Bus_N;;;LABEL_SELFREF;
    > va.us019;US19/460Bus_S;;;LABEL_SELFREF;
    > va.us023;US23/58Bus_E;;;LABEL_SELFREF;
    > va.us023;US23/58Bus_W;;;LABEL_SELFREF;
    > va.us211;US211/522Bus_N;;;LABEL_SELFREF;
    > va.us211;US211/522Bus_S;;;LABEL_SELFREF;
    > va.us360;US360/460Bus_E;;;LABEL_SELFREF;
    174a244
    > wy.us020;US20/26Bus;;;LABEL_SELFREF;
  6. yakra commented on Jan 18, 2019

    @yakra
    ContributorAuthor

    How does the C++ version compare?

    The same DIFFs are visible in the logs.
    I made the same syntactic changes in both languages. (I think.)
    Got something wrong though.

    Python diff:

    3181d3180
    <                 selfref_found = False
    3186,3194c3185,3191
    <                     if w.label[w.label.index('/')+1:] == r.route[digit_starts+1:]:
    <                         selfref_found = True
    <                     if w.label[w.label.index('/')+1:] == r.route:
    <                         selfref_found = True
    <                     if '_' in w.label[w.label.index('/')+1:] and w.label[w.label.index('/')+1:w.label.rindex('_')] == r.route[digit_starts+1:]:
    <                         selfref_found = True
    <                     if '_' in w.label[w.label.index('/')+1:] and w.label[w.label.index('/')+1:w.label.rindex('_')] == r.route:
    <                         selfref_found = True
    < 
    ---
    >                     if w.label[w.label.index('/')+1:] == r.route[digit_starts+1:] \
    >                     or w.label[w.label.index('/')+1:] == r.route:
    >                         datacheckerrors.append(DatacheckEntry(r,[w.label],'LABEL_SELFREF'))
    >                     elif '_' in w.label[w.label.index('/')+1:]:
    >                         if w.label[w.label.index('/')+1:w.label.rindex('_')] == r.route[digit_starts+1:] \
    >                         or w.label[w.label.index('/')+1:w.label.rindex('_')] == r.route:
    >                             datacheckerrors.append(DatacheckEntry(r,[w.label],'LABEL_SELFREF'))
    3196c3193
    <                 if selfref_found or r.route+r.banner == w.label or re.fullmatch(r.route+r.banner+'[_/].*',w.label):
    ---
    >                 elif r.route+r.banner == w.label or re.fullmatch(r.route+r.banner+'[_/].*',w.label):

    I'll recheck my work.
    Edit: The last elif should be an if. Testing again.

  7. added
    LABEL_SELFREF
    on Nov 15, 2020
  8. changed the title [-]LABEL_SELFREF check[/-] [+]LABEL_SELFREF efficiency[/+] on Nov 15, 2020
  9. yakra commented on Nov 15, 2020

    @yakra
    ContributorAuthor

    const char *l = label.data();
    const char *rb = rte_ban.data();
    const char *end = rb+rte_ban.size();
    while (rb < end)
    { if (*l != *rb) return;
    l++;
    rb++;
    }
    if (*l == 0 || *l == '_' || *l == '/')

    This can be boiled down...

    	size_t i;
    	for (i = 0; i < rte_ban.size(); i++)
    		if (label[i] != rte_ban[i]) return;
    	if (label[i] == 0 || label[i] == '_' || label[i] == '/')

    (If pos is equal to the string length, [std::string::operator[]] returns a reference to the null character that follows the last character in the string (which should not be modified).)

  10. yakra commented on Nov 16, 2020

    @yakra
    ContributorAuthor

    History:

    revision commit blame comments
    1 c3bd5f0 link r.route+r.banner == w.label
    Also note a commented-out "prehistoric" version, never part of production code, that had too many FPs to be useful.
    2 9258eff link optional additional _... or /...
    current 57082c1 link check for number match after slash
  11. yakra commented on Mar 2, 2021

    @yakra
    ContributorAuthor

    The high priority tag on this issue doesn't mean high priority overall, but simply that this is higher priority than other LABEL_SELFREF issues.

  12. yakra commented on Nov 15, 2021

    @yakra
    ContributorAuthor

    Here's something a little weird:

    // first check for number match after a slash, if there is one
    const char* slash = strchr(l, '/');
    if (slash)
    {	const char* digits = rte.data();
    	while (!isdigit(*digits) && *digits) ++digits;
    	if (	digits != rte.data()+rte.size()
    	     &&	!strdcmp(slash+1, digits, '_') && !coloc_same_number(digits)
    	     || !strdcmp(slash+1, rte.data(), '_') && !coloc_same_designation(rte)
    	   )	return Datacheck::add(route, label, "", "", "LABEL_SELFREF", "");
    }
    // now the remaining checks

    If strdcmp returns a match and coloc_same_number returns true, we go on to strdcmp(slash+1, rte.data(), '_') unnecessarily. We know it won't match because a label can't match the whole route name and just the digits at the same time. (Unless the route name is only digits. But then, still no need to compare, because coloc_same_designation will have the same result.)

    The following variation avoids the unnecessary comparisons...

    // first check for number match after a slash, if there is one
    const char* slash = strchr(l, '/');
    if (slash)
      if (!strdcmp(slash+1, rte.data(), '_'))
      {	if (!coloc_same_designation(rte))
    	  return Datacheck::add(route, label, "", "", "LABEL_SELFREF", "");
      } else
      {	const char* digits = rte.data();
    	while (!isdigit(*digits) && *digits) ++digits;
    	if (digits != rte.data()+rte.size() && !strdcmp(slash+1, digits, '_') && !coloc_same_number(digits))
    	  return Datacheck::add(route, label, "", "", "LABEL_SELFREF", "");
      }
    // now the remaining checks

    ...and even skips finding where the digits are if it doesn't need them.

    Just for good measure, another variation puts the digits comparison first again,

    // first check for number match after a slash, if there is one
    const char* slash = strchr(l, '/');
    if (slash)
    {	const char* digits = rte.data();
    	while (!isdigit(*digits) && *digits) ++digits;
    	if (digits != rte.data()+rte.size() && !strdcmp(slash+1, digits, '_'))
    	{	if (!coloc_same_number(digits))
    		  return Datacheck::add(route, label, "", "", "LABEL_SELFREF", "");
    	} else	if (!strdcmp(slash+1, rte.data(), '_') && !coloc_same_designation(rte))
    		  return Datacheck::add(route, label, "", "", "LABEL_SELFREF", "");
    }
    // now the remaining checks

    ...just in case there are more digits matches, and the expense of always finding the digits is outweighed by avoiding more full route name matches.

    The 2nd & 3rd versions take about 31.6% and 33% more time than the first version. Even though they avoid unnecessary string searches & comparisons. It must be outweighed by... a different branching structure? That's all that's different.
    Maybe with the compound conditions, some items can be executed speculatively or in parallel, without having to wait for a separate branch statement. (Pipelining?)
    Interesting how this stuff gets optimized under the hood.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

LABEL_SELFREFcode organizationdatacheckefficiencyhigh priority

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions