From patchwork Fri Oct 28 11:35:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wilco Dijkstra X-Patchwork-Id: 16897 Received: (qmail 3541 invoked by alias); 28 Oct 2016 11:35:28 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 3511 invoked by uid 89); 28 Oct 2016 11:35:27 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.8 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham version=3.3.2 spammy=4511, olds, 6517 X-HELO: eu-smtp-delivery-143.mimecast.com From: Wilco Dijkstra To: "libc-alpha@sourceware.org" CC: nd Subject: [PATCH] Improve strtok(_r) performance Date: Fri, 28 Oct 2016 11:35:13 +0000 Message-ID: x-ms-office365-filtering-correlation-id: bac6c2dc-4704-4af5-3261-08d3ff267b76 x-microsoft-exchange-diagnostics: 1; AM5PR0802MB2610; 7:nTRb6jPAUkqzUQc0tabEUGom5qqYKfh60IM1iuzs0jmlohKDYzbTNI0Szr7e+jLLY5VcPglazxS0AfRr8RDwMDrEQAZoJsmUPZs0VeGWFJsAAx3lSwB37thiUEL1EE0ArldNVicXG3Dgs5GdBWiB09ptGFqUSTb9XBGlgEqQ4Vz7UGJ8wgnhL54ujHA3gjUvnEhoZhihLrkH1fEF0NvvP9mFAfBH/nwibzRr+Ks0VB3/TwRKum7352HjXjKCcYMG+Ao8gOmAhid+hfKtt98ant5UPgZ1cDtjl1xV2V5Lu2UchMQaVruAXFZ/83RPxQ7f/W8b10Pun0ViYj13UQo7ayXw4EJzNuNwBqGk7g0xc6o= x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:AM5PR0802MB2610; nodisclaimer: True x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(180628864354917); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001)(6055026); SRVR:AM5PR0802MB2610; BCL:0; PCL:0; RULEID:; SRVR:AM5PR0802MB2610; x-forefront-prvs: 0109D382B0 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(6009001)(7916002)(199003)(189002)(377424004)(54534003)(86362001)(97736004)(76576001)(8936002)(54356999)(6916009)(33656002)(2906002)(4001150100001)(92566002)(450100001)(50986999)(10400500002)(11100500001)(5002640100001)(4326007)(77096005)(2900100001)(5660300001)(66066001)(586003)(6116002)(102836003)(3846002)(229853001)(2351001)(101416001)(7736002)(8676002)(3660700001)(81156014)(81166006)(9686002)(3280700002)(5640700001)(305945005)(106116001)(2501003)(19580395003)(7846002)(105586002)(68736007)(106356001)(74316002)(110136003)(19580405001)(189998001)(122556002)(7696004)(87936001); DIR:OUT; SFP:1101; SCL:1; SRVR:AM5PR0802MB2610; H:AM5PR0802MB2610.eurprd08.prod.outlook.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-originalarrivaltime: 28 Oct 2016 11:35:13.3014 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM5PR0802MB2610 X-MC-Unique: 21rmPh1nO1OlZoEZt-G3oQ-1 Improve strtok(_r) performance. Instead of calling strpbrk which calls strcspn, call strcspn directly so we get the end of the token without an extra call to rawmemchr. Also avoid an unnecessary call to strcspn after the last token by adding an early exit for an empty string. The result is a ~2x speedup of strtok on most inputs in bench-strtok. Passes regression tests, OK for commit? ChangeLog: 2015-10-28 Wilco Dijkstra * string/strtok.c (STRTOK): Optimize for performance. * string/strtok_r.c (__strtok_r): Likewise. diff --git a/string/strtok.c b/string/strtok.c index 7a4574db5c80501e47d045ad4347e8a287b32191..b1ed48c24c8d20706b7d05481a138b18a01ff802 100644 --- a/string/strtok.c +++ b/string/strtok.c @@ -38,11 +38,18 @@ static char *olds; char * STRTOK (char *s, const char *delim) { - char *token; + char *end; if (s == NULL) s = olds; + /* Return immediately at end of string. */ + if (*s == '\0') + { + olds = s; + return NULL; + } + /* Scan leading delimiters. */ s += strspn (s, delim); if (*s == '\0') @@ -52,16 +59,15 @@ STRTOK (char *s, const char *delim) } /* Find the end of the token. */ - token = s; - s = strpbrk (token, delim); - if (s == NULL) - /* This token finishes the string. */ - olds = __rawmemchr (token, '\0'); - else + end = s + strcspn (s, delim); + if (*end == '\0') { - /* Terminate the token and make OLDS point past it. */ - *s = '\0'; - olds = s + 1; + olds = end; + return s; } - return token; + + /* Terminate the token and make OLDS point past it. */ + *end = '\0'; + olds = end + 1; + return s; } diff --git a/string/strtok_r.c b/string/strtok_r.c index f351304766108dad2c1cff881ad3bebae821b2a0..e049a5c82e026a3b6c1ba5da16ce81743717805e 100644 --- a/string/strtok_r.c +++ b/string/strtok_r.c @@ -45,11 +45,17 @@ char * __strtok_r (char *s, const char *delim, char **save_ptr) { - char *token; + char *end; if (s == NULL) s = *save_ptr; + if (*s == '\0') + { + *save_ptr = s; + return NULL; + } + /* Scan leading delimiters. */ s += strspn (s, delim); if (*s == '\0') @@ -59,18 +65,17 @@ __strtok_r (char *s, const char *delim, char **save_ptr) } /* Find the end of the token. */ - token = s; - s = strpbrk (token, delim); - if (s == NULL) - /* This token finishes the string. */ - *save_ptr = __rawmemchr (token, '\0'); - else + end = s + strcspn (s, delim); + if (*end == '\0') { - /* Terminate the token and make *SAVE_PTR point past it. */ - *s = '\0'; - *save_ptr = s + 1; + *save_ptr = end; + return s; } - return token; + + /* Terminate the token and make *SAVE_PTR point past it. */ + *end = '\0'; + *save_ptr = end + 1; + return s; } #ifdef weak_alias libc_hidden_def (__strtok_r)