git: 1d6eb976774a - main - cache: save on branching when parsing the path by inserting a sentinel

Mateusz Guzik mjg at FreeBSD.org
Fri Jan 1 00:11:53 UTC 2021


The branch main has been updated by mjg:

URL: https://cgit.FreeBSD.org/src/commit/?id=1d6eb976774af6a84414a6785217b305021ce841

commit 1d6eb976774af6a84414a6785217b305021ce841
Author:     Mateusz Guzik <mjguzik at gmail.com>
AuthorDate: 2020-12-28 06:33:12 +0000
Commit:     Mateusz Guzik <mjg at FreeBSD.org>
CommitDate: 2021-01-01 00:10:42 +0000

    cache: save on branching when parsing the path by inserting a sentinel
    
    Tested by:      pho
---
 sys/kern/vfs_cache.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/sys/kern/vfs_cache.c b/sys/kern/vfs_cache.c
index 7f9339d2ed56..c9b0010020f8 100644
--- a/sys/kern/vfs_cache.c
+++ b/sys/kern/vfs_cache.c
@@ -4741,15 +4741,24 @@ cache_fplookup_parse(struct cache_fpl *fpl)
 	cnp = fpl->cnp;
 
 	/*
-	 * Search a new directory.
+	 * Find the end of this path component, it is either / or nul.
 	 *
-	 * The last component of the filename is left accessible via
-	 * cnp->cn_nameptr for callers that need the name. Callers needing
-	 * the name set the SAVENAME flag. When done, they assume
-	 * responsibility for freeing the pathname buffer.
+	 * Store / as a temporary sentinel so that we only have one character
+	 * to test for. Pathnames tend to be short so this should not be
+	 * resulting in cache misses.
 	 */
-	for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
+	KASSERT(cnp->cn_nameptr[ndp->ni_pathlen - 1] == '\0',
+	    ("%s: expected nul at %p + %zu; string [%s]\n", __func__,
+	    cnp->cn_nameptr, ndp->ni_pathlen - 1, cnp->cn_nameptr));
+	cnp->cn_nameptr[ndp->ni_pathlen - 1] = '/';
+	for (cp = cnp->cn_nameptr; *cp != '/'; cp++) {
+		KASSERT(*cp != '\0',
+		    ("%s: encountered unexpected nul; string [%s]\n", __func__,
+		    cnp->cn_nameptr));
 		continue;
+	}
+	cnp->cn_nameptr[ndp->ni_pathlen - 1] = '\0';
+
 	cnp->cn_namelen = cp - cnp->cn_nameptr;
 	if (__predict_false(cnp->cn_namelen > NAME_MAX)) {
 		cache_fpl_smr_exit(fpl);


More information about the dev-commits-src-all mailing list