[pacman-dev] [PATCH 2/2] Reduce number of open()/close() calls on the DB file handle

Dan McGee dan at archlinux.org
Sun Mar 15 11:50:56 EDT 2009


If we lazily open a handle to the database, and close it later only if we
opened it, it could reduce the number of calls quite a bit in a large pacman
operation.

Signed-off-by: Dan McGee <dan at archlinux.org>
---
 lib/libalpm/be_files.c |   11 ++++++-----
 lib/libalpm/db.c       |    3 +++
 lib/libalpm/db.h       |    1 +
 3 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/lib/libalpm/be_files.c b/lib/libalpm/be_files.c
index 3da58a3..4b4df8a 100644
--- a/lib/libalpm/be_files.c
+++ b/lib/libalpm/be_files.c
@@ -347,13 +347,14 @@ static char *get_pkgpath(pmdb_t *db, pmpkg_t *info)
 
 static int sync_db(pmdb_t *db)
 {
-	int fd;
 	/* by syncing the parent directory, we can be sure any database
 	 * changes (adding or removing a package) are committed to disk. */
-	fd = open(db->path, 0);
-	if(fd != -1) {
-		fsync(fd);
-		close(fd);
+	if(!db->fd) {
+		db->fd = open(db->path, 0);
+		/* close() happens in _alpm_db_free() */
+	}
+	if(db->fd != -1) {
+		fsync(db->fd);
 		return 0;
 	} else {
 		return -1;
diff --git a/lib/libalpm/db.c b/lib/libalpm/db.c
index 561967c..78ac4ea 100644
--- a/lib/libalpm/db.c
+++ b/lib/libalpm/db.c
@@ -347,6 +347,9 @@ void _alpm_db_free(pmdb_t *db)
 	FREELIST(db->servers);
 	FREE(db->path);
 	FREE(db->treename);
+	if(db->fd > 0) {
+		close(db->fd);
+	}
 	FREE(db);
 
 	return;
diff --git a/lib/libalpm/db.h b/lib/libalpm/db.h
index 1af1635..fb19751 100644
--- a/lib/libalpm/db.h
+++ b/lib/libalpm/db.h
@@ -41,6 +41,7 @@ typedef enum _pmdbinfrq_t {
 struct __pmdb_t {
 	char *path;
 	char *treename;
+	int fd;
 	unsigned short pkgcache_loaded;
 	alpm_list_t *pkgcache;
 	unsigned short grpcache_loaded;
-- 
1.6.2



More information about the pacman-dev mailing list