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

[PATCH v2] macos: lazily initialize iconv

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 31, 2012, 18:37 UTC
Message-ID
<7v1ujrkc9p.fsf@alter.siamese.dyndns.org>
In-Reply-To
<7vk3xjked0.fsf@alter.siamese.dyndns.org>

In practice, the majority of paths do not have utf8 that needs the canonicalization. Lazily call iconv_open()/iconv_close() to avoid unnecessary overhead.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Ralf Thielow <ralf.thielow@gmail.com>
Helped-by: Linus Torvalds <torvalds@linux-foundation.org>
---
 * This is not even compile tested, so it needs testing and
   benchmarking, as I do not even know how costly the calls to
   open/close are when we do not have to call iconv() itself.
   This reroll corrects an obvious thinko pointed out by Ralf, and
   gets rid of an extra iconv_opened field added unnecessarily in
   the previous round.
   This was brought up by Linus in http://goo.gl/INWVc
 compat/precompose_utf8.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)
diff --git a/compat/precompose_utf8.c b/compat/precompose_utf8.c
index d40d1b3..79b5528 100644
--- a/compat/precompose_utf8.c
+++ b/compat/precompose_utf8.c
@@ -67,7 +67,7 @@ void probe_utf8_pathname_composition(char *path, int len)
 
 void precompose_argv(int argc, const char **argv)
 {
-	int i = 0;
+	int i;
 	const char *oldarg;
 	char *newarg;
 	iconv_t ic_precompose;
@@ -75,11 +75,19 @@ void precompose_argv(int argc, const char **argv)
 	if (precomposed_unicode != 1)
 		return;
 
+	/* Avoid iconv_open()/iconv_close() if there is nothing to convert */
+	for (i = 0; i < argc; i++) {
+		if (has_utf8(argv[i], (size_t)-1, NULL))
+			break;
+	}
+	if (argc <= i)
+		return; /* no utf8 found */
+
 	ic_precompose = iconv_open(repo_encoding, path_encoding);
 	if (ic_precompose == (iconv_t) -1)
 		return;
 
-	while (i < argc) {
+	for (i = 0; i < argc; i++) {
 		size_t namelen;
 		oldarg = argv[i];
 		if (has_utf8(oldarg, (size_t)-1, &namelen)) {
@@ -87,7 +95,6 @@ void precompose_argv(int argc, const char **argv)
 			if (newarg)
 				argv[i] = newarg;
 		}
-		i++;
 	}
 	iconv_close(ic_precompose);
 }
@@ -106,8 +113,7 @@ PREC_DIR *precompose_utf8_opendir(const char *dirname)
 		return NULL;
 	} else {
 		int ret_errno = errno;
-		prec_dir->ic_precompose = iconv_open(repo_encoding, path_encoding);
-		/* if iconv_open() fails, die() in readdir() if needed */
+		prec_dir->ic_precompose = (iconv_t)-1;
 		errno = ret_errno;
 	}
 
@@ -136,6 +142,9 @@ struct dirent_prec_psx *precompose_utf8_readdir(PREC_DIR *prec_dir)
 		prec_dir->dirent_nfc->d_type = res->d_type;
 
 		if ((precomposed_unicode == 1) && has_utf8(res->d_name, (size_t)-1, NULL)) {
+			if (prec_dir->ic_precompose == (iconv_t)-1)
+				prec_dir->ic_precompose =
+					iconv_open(repo_encoding, path_encoding);
 			if (prec_dir->ic_precompose == (iconv_t)-1) {
 				die("iconv_open(%s,%s) failed, but needed:\n"
 						"    precomposed unicode is not supported.\n"
-- 
1.7.12.rc1.43.g3fa3e7e
Previous: Junio C HamanoNext: Ralf Thielow
Message 5 of 15 in “macos: lazily initialize iconv”
  1. macos: lazily initialize iconvJunio C Hamano, Jul 31, 2012
  2. Junio C HamanoJul 31, 2012
  3. Ralf ThielowJul 31, 2012
  4. Junio C HamanoJul 31, 2012
  5. macos: lazily initialize iconvJunio C Hamano, Jul 31, 2012
  6. Ralf ThielowJul 31, 2012
  7. Junio C HamanoJul 31, 2012
  8. Linus TorvaldsJul 31, 2012
  9. Junio C HamanoJul 31, 2012
  10. Linus TorvaldsJul 31, 2012
  11. Torsten BögershausenAug 1, 2012
  12. Torsten BögershausenAug 10, 2012
  13. Junio C HamanoAug 10, 2012
  14. Torsten BögershausenAug 10, 2012
  15. Junio C HamanoAug 10, 2012

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.