Skip to content

Commit 5c7bb12

Browse files
committed
dofs: Skip empty pending lookups
Avoid resolving parent paths on read and metadata operations when the database has no pending create buffers.
1 parent 6237da3 commit 5c7bb12

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

packages/dofs/src/fs/pendingWriteBuffer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import { resolveInode } from "./resolve.js";
44
import {
55
getPendingWriteBufferByParent,
66
getPendingWriteBufferByPath,
7+
hasPendingWriteBuffers,
78
type WriteBufferEntry,
89
} from "./writeBuffer.js";
910

1011
export function findPendingWriteBuffer(db: Database, path: string): WriteBufferEntry | undefined {
12+
if (!hasPendingWriteBuffers(db)) return undefined;
13+
1114
const { parts, path: canonical } = canonicalizePath(path);
1215
const direct = getPendingWriteBufferByPath(db, canonical);
1316
if (direct !== undefined || parts.length === 0) return direct;

packages/dofs/src/fs/writeBuffer.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
22

33
import type { Database } from "../storage.js";
44
import { mkdir } from "./mkdir.js";
5+
import { findPendingWriteBuffer } from "./pendingWriteBuffer.js";
56
import { readRangeSync } from "./readFile.js";
67
import { resolveInode } from "./resolve.js";
78
import { stat } from "./stat.js";
@@ -150,6 +151,20 @@ describe("buffered write lifecycle", () => {
150151
});
151152

152153
describe("deferred-create lifecycle", () => {
154+
it("skips parent resolution when no pending buffers exist", async () => {
155+
await withDB((db) => {
156+
const originalAll = db.all.bind(db);
157+
let queries = 0;
158+
db.all = ((query: string, ...bindings: unknown[]) => {
159+
queries += 1;
160+
return originalAll(query, ...bindings);
161+
}) as typeof db.all;
162+
163+
expect(findPendingWriteBuffer(db, "/missing/file.txt")).toBeUndefined();
164+
expect(queries).toBe(0);
165+
});
166+
});
167+
153168
it("holds the file in memory until release commits one transaction", async () => {
154169
await withDB(async (db) => {
155170
openWriteBufferForCreateSync(db, "/pending.txt", { mode: 0o600 }, () => 1000);

packages/dofs/src/fs/writeBuffer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ export function getPendingWriteBufferByParent(
9696
return caches.get(db)?.byPendingParent.get(pendingParentKey(parentInode, leafName));
9797
}
9898

99+
export function hasPendingWriteBuffers(db: Database): boolean {
100+
return (caches.get(db)?.byPendingParent.size ?? 0) > 0;
101+
}
102+
99103
// List pending-create buffers whose parent dirent matches `parentInode`.
100104
// Used by readdir so freshly-created-but-not-yet-released files show
101105
// up in directory listings between open and release.

0 commit comments

Comments
 (0)