git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] cache_tree_find(): remove redundant checks

From
David Kastrup <dak@gnu.org>
Date
Mar 4, 2014, 09:40 UTC
Message-ID
<87ppm2i9ot.fsf@fencepost.gnu.org>
In-Reply-To
<1393921868-4382-1-git-send-email-mhagger@alum.mit.edu>
Michael Haggerty <mhagger@alum.mit.edu> writes:
Show 10 quoted lines
> The beginning of the loop ensures that slash can never be NULL.  So
> don't keep checking whether it is NULL later in the loop.
>
> Furthermore, there is no need for an early
>
>     return it;
>
> from the loop if slash points at the end of the string, because that
> is exactly what will happen when the while condition fails at the
> start of the next iteration.
Hm.  Another suggestion.  You have
		const char *slash = strchr(path, '/');
 		if (!slash)
 			slash = path + strlen(path);
[...]
		sub = find_subtree(it, path, slash - path, 0);
[...]
		path = slash;
		while (*path == '/')
			path++;
	}
At the price of introducing another variable, this could be
		const char *slash = strchr(path, '/');
 		size_t len = slash ? slash - path : strlen(path);
[...]
		sub = find_subtree(it, path, len, 0);
[...]
                if (!slash)
			break;
		for (path = slash; *path == '/';)
			path++;
	}

This introduces another variable and another condition. The advantage is that "slash" indeed points at a slash or is NULL, so the variable names correspond better to what happens. Alternatively, it might make sense to rename "slash" into "end" or "endpart" or whatever. Since I can't think of a pretty name, I lean towards preferring the latter version as it reads nicer. I prefer code to read like children's books rather than mystery novels.

-- 
David Kastrup
Previous: Michael HaggertyNext: Michael Haggerty
Message 2 of 11 in “cache_tree_find(): remove redundant checks”
  1. cache_tree_find(): remove redundant checksMichael Haggerty, Mar 4, 2014
  2. David KastrupMar 4, 2014
  3. Michael HaggertyMar 4, 2014
  4. David KastrupMar 4, 2014
  5. Junio C HamanoMar 4, 2014
  6. Michael HaggertyMar 4, 2014
  7. Junio C HamanoMar 4, 2014
  8. Junio C HamanoMar 5, 2014
  9. David KastrupMar 5, 2014
  10. Junio C HamanoMar 5, 2014
  11. [microproject idea]Junio C Hamano, Mar 4, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.