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

[PATCH 2/3] reftable/stack: move list lock to `struct reftable_stack`

From
Karthik Nayak <karthik.188@gmail.com>
Date
Aug 19, 2026, 13:19 UTC
Message-ID
<20260819-740-optimize-reloading-the-reftable-stack-v1-2-6bf5305d4e43@gmail.com>
In-Reply-To
<20260819-740-optimize-reloading-the-reftable-stack-v1-0-6bf5305d4e43@gmail.com>

The struct `reftable_addition` is used to modify a given stack, as such, it also includes a `struct reftable_flock` used to obtain the lock to the list file. While the scope of the field lies within this struct, it doesn't allow for optimizations to be made on `struct reftable_stack` itself.

Move the field to `struct reftable_stack`, allowing us to make a simple optimization around avoiding a stack reload when we have already obtained a lock. While this is currently possible in the write path, the write path also contains multiple branches to reads which only work on top of `struct reftable_stack`, and we would miss the optimization in such paths.

While here, remove an unused header file from 'reftable/stack.h'.
Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 reftable/stack.c | 15 ++++++++-------
 reftable/stack.h |  7 ++++++-
 2 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/reftable/stack.c b/reftable/stack.c
index 540f5e77ac..e449af9c03 100644
--- a/reftable/stack.c
+++ b/reftable/stack.c
@@ -536,6 +536,8 @@ int reftable_new_stack(struct reftable_stack **dest, const char *dir,
 		goto out;
 	}
 
+	p->list_lock = REFTABLE_FLOCK_INIT;
+
 	err = reftable_stack_reload_maybe_reuse(p, 1);
 	if (err < 0)
 		goto out;
@@ -628,7 +630,6 @@ int reftable_stack_reload(struct reftable_stack *st)
 }
 
 struct reftable_addition {
-	struct reftable_flock tables_list_lock;
 	struct reftable_stack *stack;
 	struct reftable_write_options opts;
 
@@ -653,7 +654,7 @@ static void reftable_addition_close(struct reftable_addition *add)
 	add->new_tables_len = 0;
 	add->new_tables_cap = 0;
 
-	flock_release(&add->tables_list_lock);
+	flock_release(&add->stack->list_lock);
 	reftable_buf_release(&nm);
 }
 
@@ -669,13 +670,13 @@ static int reftable_stack_init_addition(struct reftable_addition *add,
 	if (opts)
 		add->opts = *opts;
 
-	err = flock_acquire(&add->tables_list_lock, st->list_file,
+	err = flock_acquire(&add->stack->list_lock, st->list_file,
 			    add->opts.lock_timeout_ms);
 	if (err < 0)
 		goto done;
 
 	if (add->opts.default_permissions) {
-		if (chmod(add->tables_list_lock.path,
+		if (chmod(add->stack->list_lock.path,
 			  add->opts.default_permissions) < 0) {
 			err = REFTABLE_IO_ERROR;
 			goto done;
@@ -774,7 +775,7 @@ int reftable_addition_commit(struct reftable_addition *add)
 			goto done;
 	}
 
-	err = reftable_write_data(add->tables_list_lock.fd,
+	err = reftable_write_data(add->stack->list_lock.fd,
 				  table_list.buf, table_list.len);
 	reftable_buf_release(&table_list);
 	if (err < 0) {
@@ -782,13 +783,13 @@ int reftable_addition_commit(struct reftable_addition *add)
 		goto done;
 	}
 
-	err = fsync(add->tables_list_lock.fd);
+	err = fsync(add->stack->list_lock.fd);
 	if (err < 0) {
 		err = REFTABLE_IO_ERROR;
 		goto done;
 	}
 
-	err = flock_commit(&add->tables_list_lock);
+	err = flock_commit(&add->stack->list_lock);
 	if (err < 0) {
 		err = REFTABLE_IO_ERROR;
 		goto done;
diff --git a/reftable/stack.h b/reftable/stack.h
index f7901e6c6f..52e07ad551 100644
--- a/reftable/stack.h
+++ b/reftable/stack.h
@@ -10,7 +10,6 @@
 #define STACK_H
 
 #include "system.h"
-#include "reftable-writer.h"
 #include "reftable-stack.h"
 
 struct reftable_stack {
@@ -18,6 +17,12 @@ struct reftable_stack {
 	char *list_file;
 	int list_fd;
 
+	/*
+	 * Set while an addition holds the stack locked. Used by
+	 * stack_uptodate() to skip reload checks while locked.
+	 */
+	struct reftable_flock list_lock;
+
 	char *reftable_dir;
 
 	struct reftable_stack_options opts;
-- 
2.55.GIT
Previous: Karthik NayakNext: Justin Tobler
Message 8 of 17 in “reftable/stack: avoid reloading the stack when locked”
  1. 0/3 reftable/stack: avoid reloading the stack when lockedKarthik Nayak, Aug 19, 2026
  2. 1/3 reftable/stack: remove `REFTABLE_STACK_NEW_ADDITION_RELOAD`Karthik Nayak, Aug 19, 2026
  3. Justin ToblerAug 19, 2026
  4. Karthik NayakAug 20, 2026
  5. Patrick SteinhardtAug 20, 2026
  6. Karthik NayakAug 20, 2026
  7. Karthik NayakAug 20, 2026
  8. 2/3 reftable/stack: move list lock to `struct reftable_stack`Karthik Nayak, Aug 19, 2026
  9. Justin ToblerAug 19, 2026
  10. Junio C HamanoAug 19, 2026
  11. Karthik NayakAug 23, 2026
  12. 3/3 reftable/stack: avoid reloading the stack when already lockedKarthik Nayak, Aug 19, 2026
  13. Justin ToblerAug 19, 2026
  14. Karthik NayakAug 23, 2026
  15. Jeff KingAug 20, 2026
  16. Karthik NayakAug 23, 2026
  17. Jeff KingAug 24, 2026

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.