[pacman-dev] [PATCH] Use puts() instead of no-op printf() where applicable

Dan McGee dan at archlinux.org
Thu Oct 13 22:12:49 EDT 2011


This replaces several printf calls of the following styles:
   printf("%s", ...);
   printf("some fixed string");
   printf("x");

We can use either fputs() or putchar() here to do the same thing
without incurring the overhead of the printf format parser.

The biggest gain here comes when we are calling the print function in a
loop repeatedly; notably when printing local package files.

    $ /usr/bin/time ./pacman-before -Ql | md5sum
      0.25user 0.04system 0:00.30elapsed 98%CPU
    $ /usr/bin/time ./pacman-after -Ql | md5sum
      0.17user 0.06system 0:00.25elapsed 94%CPU

    $ /usr/bin/time ./pacman-before -Qlq | md5sum
      0.20user 0.05system 0:00.26elapsed 98%CPU
    $ /usr/bin/time ./pacman-after -Qlq | md5sum
      0.15user 0.05system 0:00.23elapsed 93%CPU

So '-Ql' shows a 17% improvement while '-Qlq' shows a 13% improvement on
382456 total files.

Signed-off-by: Dan McGee <dan at archlinux.org>
---
 src/pacman/callback.c |    4 ++--
 src/pacman/package.c  |   15 ++++++++++-----
 src/pacman/pacman.c   |    2 +-
 src/pacman/query.c    |   10 +++++-----
 src/pacman/sync.c     |   10 +++++-----
 src/pacman/util.c     |    8 ++++----
 6 files changed, 27 insertions(+), 22 deletions(-)

diff --git a/src/pacman/callback.c b/src/pacman/callback.c
index 7235d39..80db402 100644
--- a/src/pacman/callback.c
+++ b/src/pacman/callback.c
@@ -228,7 +228,7 @@ void cb_event(alpm_event_t event, void *data1, void *data2)
 			printf(_("failed.\n"));
 			break;
 		case ALPM_EVENT_SCRIPTLET_INFO:
-			printf("%s", (char *)data1);
+			fputs((const char *)data1, stdout);
 			break;
 		case ALPM_EVENT_RETRIEVE_START:
 			printf(_(":: Retrieving packages from %s...\n"), (char *)data1);
@@ -495,7 +495,7 @@ void cb_progress(alpm_progress_t event, const char *pkgname, int percent,
 		alpm_list_t *i = NULL;
 		on_progress = 0;
 		for(i = output; i; i = i->next) {
-			printf("%s", (char *)i->data);
+			fputs((const char *)i->data, stdout);
 		}
 		fflush(stdout);
 		FREELIST(output);
diff --git a/src/pacman/package.c b/src/pacman/package.c
index 4950957..f82be47 100644
--- a/src/pacman/package.c
+++ b/src/pacman/package.c
@@ -252,11 +252,16 @@ void dump_pkg_files(alpm_pkg_t *pkg, int quiet)
 
 	for(i = 0; i < pkgfiles->count; i++) {
 		const alpm_file_t *file = pkgfiles->files + i;
+		/* Regular: '<pkgname> <root><filepath>\n'
+		 * Quiet  : '<root><filepath>\n'
+		 */
 		if(!quiet) {
-			fprintf(stdout, "%s %s%s\n", pkgname, root, file->name);
-		} else {
-			fprintf(stdout, "%s%s\n", root, file->name);
+			fputs(pkgname, stdout);
+			putchar(' ');
 		}
+		fputs(root, stdout);
+		fputs(file->name, stdout);
+		putchar('\n');
 	}
 
 	fflush(stdout);
@@ -281,10 +286,10 @@ void dump_pkg_changelog(alpm_pkg_t *pkg)
 				/* if we hit the end of the file, we need to add a null terminator */
 				*(buf + ret) = '\0';
 			}
-			printf("%s", buf);
+			fputs(buf, stdout);
 		}
 		alpm_pkg_changelog_close(pkg, fp);
-		printf("\n");
+		putchar('\n');
 	}
 }
 
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index 5ebced2..ac793df 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -208,7 +208,7 @@ static void usage(int op, const char * const myname)
 	}
 	list = alpm_list_msort(list, alpm_list_count(list), options_cmp);
 	for (i = list; i; i = alpm_list_next(i)) {
-		printf("%s", (const char *)i->data);
+		fputs((const char *)i->data, stdout);
 	}
 	alpm_list_free(list);
 #undef addlist
diff --git a/src/pacman/query.c b/src/pacman/query.c
index dbea966..f3df923 100644
--- a/src/pacman/query.c
+++ b/src/pacman/query.c
@@ -268,23 +268,23 @@ static int query_search(alpm_list_t *targets)
 		if(!config->quiet) {
 			printf("local/%s %s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
 		} else {
-			printf("%s", alpm_pkg_get_name(pkg));
+			fputs(alpm_pkg_get_name(pkg), stdout);
 		}
 
 
 		if(!config->quiet) {
 			if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
 				alpm_list_t *k;
-				printf(" (");
+				fputs(" (", stdout);
 				for(k = grp; k; k = alpm_list_next(k)) {
 					const char *group = k->data;
-					printf("%s", group);
+					fputs(group, stdout);
 					if(alpm_list_next(k)) {
 						/* only print a spacer if there are more groups */
-						printf(" ");
+						putchar(' ');
 					}
 				}
-				printf(")");
+				putchar(')');
 			}
 
 			/* we need a newline and initial indent first */
diff --git a/src/pacman/sync.c b/src/pacman/sync.c
index dd8eb74..925257e 100644
--- a/src/pacman/sync.c
+++ b/src/pacman/sync.c
@@ -359,22 +359,22 @@ static int sync_search(alpm_list_t *syncs, alpm_list_t *targets)
 				printf("%s/%s %s", alpm_db_get_name(db), alpm_pkg_get_name(pkg),
 							 alpm_pkg_get_version(pkg));
 			} else {
-				printf("%s", alpm_pkg_get_name(pkg));
+				fputs(alpm_pkg_get_name(pkg), stdout);
 			}
 
 			if(!config->quiet) {
 				if((grp = alpm_pkg_get_groups(pkg)) != NULL) {
 					alpm_list_t *k;
-					printf(" (");
+					fputs(" (", stdout);
 					for(k = grp; k; k = alpm_list_next(k)) {
 						const char *group = k->data;
-						printf("%s", group);
+						fputs(group, stdout);
 						if(alpm_list_next(k)) {
 							/* only print a spacer if there are more groups */
-							printf(" ");
+							putchar(' ');
 						}
 					}
-					printf(")");
+					putchar(')');
 				}
 
 				print_installed(db_local, pkg);
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 278b65c..2696451 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -266,7 +266,7 @@ void indentprint(const char *str, size_t indent)
 	/* if we're not a tty, or our tty is not wide enough that wrapping even makes
 	 * sense, print without indenting */
 	if(cols == 0 || indent > cols) {
-		printf("%s", str);
+		fputs(str, stdout);
 		return;
 	}
 
@@ -623,7 +623,7 @@ void list_display(const char *title, const alpm_list_t *list)
 		const unsigned short maxcols = getcols();
 		size_t cols = len;
 		const char *str = list->data;
-		printf("%s", str);
+		fputs(str, stdout);
 		cols += string_length(str);
 		for(i = alpm_list_next(list); i; i = alpm_list_next(i)) {
 			str = i->data;
@@ -641,10 +641,10 @@ void list_display(const char *title, const alpm_list_t *list)
 				printf("  ");
 				cols += 2;
 			}
-			printf("%s", str);
+			fputs(str, stdout);
 			cols += s;
 		}
-		printf("\n");
+		putchar('\n');
 	}
 }
 
-- 
1.7.7



More information about the pacman-dev mailing list