From 82cedba63a97c4afde1440751585759888384883 Mon Sep 17 00:00:00 2001 From: Nick Bowler Date: Sat, 18 Nov 2023 20:05:23 -0500 Subject: [PATCH] libcdecl: Prefer memchr over strchr in the scanner. Since nothing else in the library uses strchr, but we do use memchr elsewhere, using memchr instead avoids a comparatively expensive PLT entry. We already know the string length so the only real difference is the additional argument marshalling. --- src/scan.l | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/scan.l b/src/scan.l index 7549e6f..2b92846 100644 --- a/src/scan.l +++ b/src/scan.l @@ -157,9 +157,10 @@ INTEGER 0x[[:xdigit:]]+|0[0-7]+|[[:digit:]]+ } {IDENT} { - unsigned x = cdecl__to_keyword(yytext, yyleng, yyextra); - int tok; + int len = yyleng, tok; + unsigned x; + x = cdecl__to_keyword(yytext, len, yyextra); yylval->spectype = UNPACK_SPEC(x & 0xff); if ((tok = (x >> 8)) == PACK_TOKEN(T_IDENT)) { /* @@ -173,14 +174,14 @@ INTEGER 0x[[:xdigit:]]+|0[0-7]+|[[:digit:]]+ * downsides. */ #if 1 - if ((c = strchr(yytext, '-'))) + if ((c = memchr(yytext, '-', len))) goto invalid_char; #else yyless(strcspn(yytext, "-")); #endif - if (!(yylval->item = cdecl__alloc_item(yyleng+1))) + if (!(yylval->item = cdecl__alloc_item(len+1))) return T_LEX_ERROR; \ - memcpy(yylval->item->s, yytext, yyleng+1); + memcpy(yylval->item->s, yytext, len+1); } return UNPACK_TOKEN(tok); } -- 2.43.2