[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v1 2/2] misc: Replace strcmp with memcmp when it obviously works
From: |
Noah Goldstein |
Subject: |
[PATCH v1 2/2] misc: Replace strcmp with memcmp when it obviously works |
Date: |
Thu, 21 Apr 2022 21:53:24 -0500 |
memcmp is always faster than strcmp. The change in the `a_word`
comparison function is the most important.
All tests pass:
```
$> git clean -xf; ./bootstrap && ./configure && make check
...
977 Tests in 132 Categories Complete ... No Failures :-)
=========================================================================
Regression PASSED: GNU Make 4.3.90 (x86_64-pc-linux-gnu) built with gcc
=========================================================================
```
---
src/commands.c | 2 +-
src/function.c | 9 +++++----
src/main.c | 2 +-
src/read.c | 2 +-
4 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/src/commands.c b/src/commands.c
index ee9c5367..00baaca4 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -119,7 +119,7 @@ set_file_variables (struct file *file)
for (d = enter_file (strcache_add (".SUFFIXES"))->deps; d ; d = d->next)
{
size_t slen = strlen (dep_name (d));
- if (len > slen && strneq (dep_name (d), name + (len - slen), slen))
+ if (len > slen && (memcmp (dep_name (d), name + (len - slen), slen)
== 0))
{
file->stem = strcache_add_len (name, len - slen);
break;
diff --git a/src/function.c b/src/function.c
index 38ab9667..92625620 100644
--- a/src/function.c
+++ b/src/function.c
@@ -990,8 +990,9 @@ a_word_hash_cmp (const void *x, const void *y)
int result = (int) ((struct a_word const *) x)->length - ((struct a_word
const *) y)->length;
if (result)
return result;
- return_STRING_COMPARE (((struct a_word const *) x)->str,
- ((struct a_word const *) y)->str);
+ return_STRING_N_COMPARE (((struct a_word const *) x)->str,
+ ((struct a_word const *) y)->str,
+ ((struct a_word const *) y)->length);
}
struct a_pattern
@@ -1110,7 +1111,7 @@ func_filter_filterout (char *o, char **argv, const char
*funcname)
else
for (wp = words; wp < word_end; ++wp)
wp->matched |= (wp->length == pp->length
- && strneq (pp->str, wp->str, wp->length));
+ && (memcmp (pp->str, wp->str, wp->length) == 0));
}
/* Output the words that matched (or didn't, for filter-out). */
@@ -1245,7 +1246,7 @@ func_sort (char *o, char **argv, const char *funcname
UNUSED)
{
len = strlen (words[i]);
if (i == wordi - 1 || strlen (words[i + 1]) != len
- || strcmp (words[i], words[i + 1]))
+ || memcmp (words[i], words[i + 1], len))
{
o = variable_buffer_output (o, words[i], len);
o = variable_buffer_output (o, " ", 1);
diff --git a/src/main.c b/src/main.c
index 66086029..33deb8ff 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1404,7 +1404,7 @@ main (int argc, char **argv, char **envp)
/* If this is MAKE_RESTARTS, check to see if the "already printed
the enter statement" flag is set. */
- if (len == 13 && strneq (envp[i], "MAKE_RESTARTS", 13))
+ if (len == 13 && (memcmp (envp[i], "MAKE_RESTARTS", 13) == 0))
{
if (*ep == '-')
{
diff --git a/src/read.c b/src/read.c
index 12e3cfd0..cb09e7e4 100644
--- a/src/read.c
+++ b/src/read.c
@@ -166,7 +166,7 @@ static char *unescape_char (char *string, int c);
/* Compare a word, both length and contents.
P must point to the word to be tested, and WLEN must be the length.
*/
-#define word1eq(s) (wlen == CSTRLEN (s) && strneq (s, p, CSTRLEN (s)))
+#define word1eq(s) (wlen == CSTRLEN (s) && (memcmp (s, p, wlen) == 0))
/* Read in all the makefiles and return a chain of targets to rebuild. */
--
2.25.1