util.datamanager: Pad list writes to avoid crossing block boundaries
authorKim Alvefur <zash@zash.se>
Wed, 07 Jun 2023 00:39:30 +0200
changeset 13190 affaf6d08d26
parent 13189 b57f45165e1e
child 13191 fe1229919070
util.datamanager: Pad list writes to avoid crossing block boundaries By padding items so that they do not cross block boundaries, it becomes eaiser to delete whole blocks with fallocate() without cutting items in half, improving efficiency of such operations. Since list stores are used for message archives, where the most common deletion operation would be of the oldest entires, at the top of the file. With this, all blocks that contain items to be removed could be deleted without needing to read, delete and write out the whole file.
util/datamanager.lua
--- a/util/datamanager.lua	Wed Jul 12 11:45:12 2023 +0200
+++ b/util/datamanager.lua	Wed Jun 07 00:39:30 2023 +0200
@@ -32,6 +32,7 @@
 
 local prosody = prosody;
 
+local blocksize = 0x1000;
 local raw_mkdir = lfs.mkdir;
 local atomic_append;
 local remove_blocks;
@@ -244,6 +245,12 @@
 	end
 
 	local pos = f:seek("end");
+	if (blocksize-(pos%blocksize)) < (#data%blocksize) then
+		-- pad to blocksize with newlines so that the next item is both on a new
+		-- block and a new line
+		atomic_append(f, ("\n"):rep(blocksize-(pos%blocksize)));
+		pos = f:seek("end");
+	end
 
 	local ok, msg = atomic_append(f, data);