On Tue, Nov 24, 2015 at 10:18 AM, Graham Leggett <minf...@sharp.fm> wrote:

> On 24 Nov 2015, at 6:15 PM, Yann Ylavic <ylavic....@gmail.com> wrote:
>
> > Not sure:
> >    if (!strcmp(h, "max-age")
> >        || ap_cmpcasestr(h, "max-age"))
> > is likely to be a bit faster than a single ap_cmpcasestr() when it
> > matches, but much slower when it does not.
>
> Yep, that’s the point.
>
> The vast majority of comparisons are lowercase for tokens like this. Might
> as well test that fast path first before testing the worst case scenario.
>

Hmmm, it's our implementation, why increase the pain?  Consider the
following 'optimization' - fallthrough from identity to case insensitivity
with one wasted equality test...

AP_DECLARE(int) ap_casecmpstr(const char *s1, const char *s2)
{
    const unsigned char *ps1 = (const unsigned char *) s1;
    const unsigned char *ps2 = (const unsigned char *) s2;

    while (*ps1 == *ps2) {
        if (*ps1++ == '\0') {
            return (0);
        }
        ps2++;
    }
    while (ucharmap[*ps1] == ucharmap[*ps2]) {
        if (*ps1++ == '\0') {
            return (0);
        }
        ps2++;
    }
    return (ucharmap[*ps1] - ucharmap[*ps2]);
}


AP_DECLARE(int) ap_casecmpstrn(const char *s1, const char *s2, apr_size_t n)
{
    const unsigned char *ps1 = (const unsigned char *) s1;
    const unsigned char *ps2 = (const unsigned char *) s2;
    while (n) {
        if (*ps1 != *ps2) {
            break;
        }
        if (*ps1++ == '\0') {
            return (0);
        }
        ps2++;
        n--;
    }
    while (n--) {
        if (ucharmap[*ps1] != ucharmap[*ps2]) {
            return (ucharmap[*ps1] - ucharmap[*ps2]);
        }
        if (*ps1++ == '\0') {
            break;
        }
        ps2++;
    }
    return (0);
}

Reply via email to