@ref0/git 0.31.0 → 0.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1537,14 +1537,15 @@ const bitmapsCutTry = /* @__PURE__ */ Effect.fnUntraced(function* (parts, files,
1537
1537
  const callsFit = counted === void 0 || madeCalls === void 0 || directNeeds <= counted.most.bucket - madeCalls.bucket && directNeeds <= counted.most.subrequests - madeCalls.subrequests;
1538
1538
  const putBuffer = putHeldBytes(parts.bucket.multipart, { atMost });
1539
1539
  if (callsFit && (putBuffer === 0 || allotment.tryGrow(putBuffer))) return yield* Effect.ensuring(Effect.gen(function* () {
1540
- const already = yield* storedSize(parts.bucket, files.bitmaps);
1540
+ const already = chained ? void 0 : yield* storedSize(parts.bucket, files.bitmaps);
1541
1541
  const written = already === void 0 ? yield* Effect.scoped(Effect.gen(function* () {
1542
1542
  const bitmaps = partBitmaps(into.finish());
1543
1543
  if (bitmaps?.length !== into.count) return yield* Effect.fail(lost(`${files.bitmaps} part buffer is unreadable`));
1544
1544
  const put = yield* bitmapsPut(parts.bucket, files.bitmaps, input.positions, into.count);
1545
1545
  for (const bitmap of bitmaps) yield* put.addBytes(bitmap.position, bitmap.number, bitmap.bytes);
1546
1546
  return yield* put.finish;
1547
- })) : yield* storedBitmaps(parts.bucket, files.bitmaps, input.positions, already);
1547
+ })).pipe(Effect.catchIf(alreadyThere, () => Effect.succeed(void 0))) : yield* storedBitmaps(parts.bucket, files.bitmaps, input.positions, already);
1548
+ if (written === void 0) return data;
1548
1549
  const { bitmapsCut: _, ...rest } = data;
1549
1550
  return {
1550
1551
  ...rest,
@@ -0,0 +1,286 @@
1
+ import { bucketBodyIn, joined } from "./calls.mjs";
2
+ import { replayPieceBytes } from "./range-cache.mjs";
3
+ import * as Effect from "effect/Effect";
4
+ import * as Stream from "effect/Stream";
5
+ import * as Exit from "effect/Exit";
6
+ // src/storage/index-step-files.ts
7
+ /**
8
+ * The most a repo's index build keeps of its index files between its steps, 16 MiB. svelte's
9
+ * first build, 91,275 objects, reads about 10 MiB of them over its 10 chained steps, and about
10
+ * 5 s of its wall went to reading them again (#596). Pack bodies are never kept: the build reads
11
+ * about 150 MiB of those, past what a Durable Object's 128 MB could hold.
12
+ */
13
+ const indexStepFilesMost = 16777216;
14
+ /** A pack's own index file, as `packFiles` names it: `packs/<ID>.idx` and its kin. */
15
+ const packIndexFile = /^packs\/[^/]+\.(?:idx|pos|rev|graph|types)$/;
16
+ /**
17
+ * Whether a put of `key` is kept: an index file, under `index/`, or a pack's `.idx`, `.pos`,
18
+ * `.rev`, `.graph` or `.types`, as `packFiles` names them. Never a pack's bytes, nor a receive's
19
+ * keys under its pack's. Each is put once, with the same bytes by any try that puts it, so kept
20
+ * bytes are what the bucket holds until the key is removed.
21
+ */
22
+ const keepable = (key) => key.startsWith("index/") || packIndexFile.test(key);
23
+ /**
24
+ * Whether a read of `key` is kept: a pack's index file. A file under `index/` is kept only as a
25
+ * step puts it: one it didn't, an earlier layer's, `PackIndexes` holds whole where it's read whole.
26
+ */
27
+ const readKept = (key) => packIndexFile.test(key);
28
+ /**
29
+ * `kept`'s bytes from `from` to `to`, `replayPieceBytes` a piece, each its own copy read from them
30
+ * as it's pulled, so no reader changes another's. Bytes let go mid-way are no longer held here:
31
+ * the rest comes from `bucket`, read in `context` once pulled.
32
+ */
33
+ const replayed = (context, bucket, kept, from, to) => {
34
+ let at = from;
35
+ let rest;
36
+ return new ReadableStream({
37
+ pull: async (controller) => {
38
+ if (rest === void 0) {
39
+ const { bytes } = kept;
40
+ if (at === to) return controller.close();
41
+ if (bytes !== void 0) {
42
+ const end = Math.min(at + replayPieceBytes, to);
43
+ controller.enqueue(bytes.slice(at, end));
44
+ at = end;
45
+ return;
46
+ }
47
+ const range = {
48
+ offset: kept.offset + at,
49
+ length: to - at
50
+ };
51
+ rest = bucketBodyIn(context, bucket, kept.key, range).getReader();
52
+ }
53
+ const next = await rest.read();
54
+ if (next.done) controller.close();
55
+ else controller.enqueue(next.value);
56
+ },
57
+ cancel: (reason) => rest?.cancel(reason)
58
+ }, { highWaterMark: 0 });
59
+ };
60
+ /**
61
+ * The index files one repo's build reads and writes, kept between its steps within `most`, each
62
+ * charged to `charge`: a server's per-instance room (`instanceRoomOf`), which takes only what's
63
+ * free and gives way first, so what it keeps never holds back a live operation (#596).
64
+ *
65
+ * A step reads through `over`'s bucket. A read of a kept file, or of a range inside a kept one,
66
+ * answers from memory with no call. A put of an index file is kept as it lands, and a read of a
67
+ * pack's index file as its body is read to its end. The least recently used goes first past `most`,
68
+ * all of it when the room shrinks, and a removed key is let go. A step for another repo, or
69
+ * `clear`, lets everything go: the cache holds one build's files at a time.
70
+ */
71
+ var IndexStepFiles = class {
72
+ #charge;
73
+ most;
74
+ /** Least recently used first. */
75
+ #kept = /* @__PURE__ */ new Map();
76
+ #byKey = /* @__PURE__ */ new Map();
77
+ /** What kept entries are charged, and reads and puts under way that may keep theirs. */
78
+ #held = 0;
79
+ #filling = 0;
80
+ /** Each fill under way, by what lets its copies go: everything let go lets them go too. */
81
+ #fills = /* @__PURE__ */ new Set();
82
+ #repo;
83
+ constructor(charge, most = indexStepFilesMost) {
84
+ this.#charge = charge;
85
+ this.most = most;
86
+ charge.watch(() => this.#letGo(false));
87
+ }
88
+ /** What it holds now: kept entries and fills under way, as charged. */
89
+ get held() {
90
+ return this.#held + this.#filling;
91
+ }
92
+ /** `bucket`, for a step of `repo`'s build: a step of another repo's lets the last one's go. */
93
+ over(bucket, repo) {
94
+ if (this.#repo !== repo) {
95
+ this.#letGo(true);
96
+ this.#repo = repo;
97
+ }
98
+ return {
99
+ ...bucket,
100
+ read: (key, range) => Effect.suspend(() => {
101
+ if (!keepable(key)) return bucket.read(key, range);
102
+ const found = this.#replay(key, range);
103
+ if (found !== void 0) return Effect.map(Effect.context(), (context) => replayed(context, bucket, found.kept, found.from, found.to));
104
+ if (!readKept(key)) return bucket.read(key, range);
105
+ return Effect.map(bucket.read(key, range), (body) => this.#teed(key, range, body));
106
+ }),
107
+ put: (key, body, options) => Effect.suspend(() => {
108
+ if (!keepable(key)) return bucket.put(key, body, options);
109
+ const fill = this.#fill(options?.size);
110
+ if (fill === void 0) return bucket.put(key, body, options);
111
+ return bucket.put(key, Stream.map(body, fill.add), options).pipe(Effect.onExit((exit) => Effect.sync(() => Exit.isSuccess(exit) ? fill.end(key, void 0, exit.value.size) : fill.drop())));
112
+ }),
113
+ remove: (key) => Effect.andThen(Effect.sync(() => this.#forget(key)), bucket.remove(key)),
114
+ removeMany: (keys, options) => Effect.andThen(Effect.sync(() => keys.forEach((key) => this.#forget(key))), bucket.removeMany(keys, options))
115
+ };
116
+ }
117
+ /** Lets every kept file go, its charge given back: the build's end. */
118
+ clear() {
119
+ this.#letGo(true);
120
+ this.#repo = void 0;
121
+ }
122
+ /** The kept entry answering `range` of `key`, made the most recent, and where in it, or `undefined`. */
123
+ #replay(key, range) {
124
+ for (const kept of this.#byKey.get(key) ?? []) {
125
+ const from = (range?.offset ?? 0) - kept.offset;
126
+ const to = range === void 0 ? kept.size : from + range.length;
127
+ if (!(range === void 0 ? kept.whole : from >= 0 && to <= kept.size)) continue;
128
+ this.#kept.delete(kept.id);
129
+ this.#kept.set(kept.id, kept);
130
+ return {
131
+ kept,
132
+ from,
133
+ to
134
+ };
135
+ }
136
+ }
137
+ /** `body`, read through: kept once it ends at `range`'s length, or whole for no range. */
138
+ #teed(key, range, body) {
139
+ const fill = this.#fill(range?.length);
140
+ if (fill === void 0) return body;
141
+ const reader = body.getReader();
142
+ return new ReadableStream({
143
+ pull: async (controller) => {
144
+ const next = await reader.read().catch((cause) => {
145
+ fill.drop();
146
+ throw cause;
147
+ });
148
+ if (next.done) {
149
+ fill.end(key, range?.offset, range?.length);
150
+ return controller.close();
151
+ }
152
+ controller.enqueue(fill.add(next.value));
153
+ },
154
+ cancel: (reason) => {
155
+ fill.drop();
156
+ return reader.cancel(reason);
157
+ }
158
+ }, { highWaterMark: 0 });
159
+ }
160
+ /**
161
+ * One read's or put's bytes as they pass, charged as they come, or at once for a known `size`:
162
+ * `undefined` when even that doesn't fit. `add` copies each piece while it fits, into one block of
163
+ * a known `size`, `end` keeps them when they came to `size`, and `drop` lets them go. One that
164
+ * everything let go under keeps nothing, its copies gone at once and its charge given back with
165
+ * the rest.
166
+ */
167
+ #fill(size) {
168
+ let charged = 0;
169
+ let open = true;
170
+ let block;
171
+ let pieces;
172
+ let got = 0;
173
+ const abandon = () => {
174
+ open = false;
175
+ block = void 0;
176
+ pieces = void 0;
177
+ };
178
+ const charge = (bytes) => {
179
+ if (!this.#reserve(bytes)) return false;
180
+ charged += bytes;
181
+ return true;
182
+ };
183
+ const drop = () => {
184
+ if (!open) return;
185
+ abandon();
186
+ this.#fills.delete(abandon);
187
+ this.#filling -= charged;
188
+ this.#charge.shrink(charged);
189
+ };
190
+ if (!charge(320 + (size ?? 0))) return void 0;
191
+ if (size === void 0) pieces = [];
192
+ else block = new Uint8Array(size);
193
+ this.#fills.add(abandon);
194
+ return {
195
+ add: (piece) => {
196
+ if (!open) return piece;
197
+ const at = got;
198
+ got += piece.byteLength;
199
+ if (block !== void 0) {
200
+ if (got > block.byteLength) drop();
201
+ else block.set(piece, at);
202
+ } else if (!charge(piece.byteLength)) drop();
203
+ else pieces?.push(piece.slice());
204
+ return piece;
205
+ },
206
+ drop,
207
+ end: (key, offset, length) => {
208
+ if (!open) return;
209
+ if (length !== void 0 && got !== length || size !== void 0 && got !== size) return drop();
210
+ let bytes = block;
211
+ if (bytes === void 0) {
212
+ if (!charge(got)) return drop();
213
+ bytes = joined(pieces ?? []);
214
+ pieces = void 0;
215
+ charged -= got;
216
+ this.#filling -= got;
217
+ this.#charge.shrink(got);
218
+ }
219
+ abandon();
220
+ this.#fills.delete(abandon);
221
+ this.#filling -= charged;
222
+ this.#keep({
223
+ id: `${key}@${offset ?? "whole"}+${got}`,
224
+ key,
225
+ offset: offset ?? 0,
226
+ bytes,
227
+ size: got,
228
+ whole: offset === void 0,
229
+ cost: charged
230
+ });
231
+ }
232
+ };
233
+ }
234
+ /**
235
+ * Room for `bytes` more within `most`, the least recent let go first, then the charge's grow. What
236
+ * fills now can't make room, so bytes past `most` with it let nothing go.
237
+ */
238
+ #reserve(bytes) {
239
+ if (this.#filling + bytes > this.most) return false;
240
+ for (const [, oldest] of this.#kept) {
241
+ if (this.#held + this.#filling + bytes <= this.most) break;
242
+ this.#remove(oldest, true);
243
+ }
244
+ if (this.#held + this.#filling + bytes > this.most || !this.#charge.tryGrow(bytes)) return false;
245
+ this.#filling += bytes;
246
+ return true;
247
+ }
248
+ /** `kept`, its charge moved from filling to held: one already there with its ID is let go. */
249
+ #keep(kept) {
250
+ const prior = this.#kept.get(kept.id);
251
+ if (prior !== void 0) this.#remove(prior, true);
252
+ this.#kept.set(kept.id, kept);
253
+ this.#byKey.set(kept.key, [...this.#byKey.get(kept.key) ?? [], kept]);
254
+ this.#held += kept.cost;
255
+ }
256
+ /** `kept` let go: a replay of it under way reads the rest from the bucket. */
257
+ #remove(kept, release) {
258
+ if (!this.#kept.delete(kept.id)) return;
259
+ kept.bytes = void 0;
260
+ this.#held -= kept.cost;
261
+ if (release) this.#charge.shrink(kept.cost);
262
+ const left = (this.#byKey.get(kept.key) ?? []).filter((each) => each !== kept);
263
+ if (left.length === 0) this.#byKey.delete(kept.key);
264
+ else this.#byKey.set(kept.key, left);
265
+ }
266
+ /** `key`'s kept bytes let go, for a removed file. */
267
+ #forget(key) {
268
+ for (const kept of this.#byKey.get(key) ?? []) this.#remove(kept, true);
269
+ }
270
+ /**
271
+ * Everything let go, kept and filling, a fill's copies and a replay's bytes included, its charge
272
+ * given back unless a shrink took it.
273
+ */
274
+ #letGo(release) {
275
+ const charged = this.#held + this.#filling;
276
+ for (const abandon of this.#fills) abandon();
277
+ for (const kept of this.#kept.values()) kept.bytes = void 0;
278
+ this.#fills.clear();
279
+ this.#kept.clear();
280
+ this.#byKey.clear();
281
+ this.#held = 0;
282
+ this.#filling = 0;
283
+ if (release && charged > 0) this.#charge.shrink(charged);
284
+ }
285
+ };
286
+ export { IndexStepFiles, indexStepFilesMost };
@@ -24,6 +24,7 @@ import { defaultRemapCpuMost, indexHolds } from "./index-remap.mjs";
24
24
  import { putHeldBytes } from "./put-holds.mjs";
25
25
  import { indexRowKey, isPackId, packFiles, prunePassKey } from "./layout.mjs";
26
26
  import { ReachCache, indexReach, reachCacheBytes, reachedRoots } from "./index-reach.mjs";
27
+ import { IndexStepFiles, indexStepFilesMost } from "./index-step-files.mjs";
27
28
  import { indexedVisible, setReachOf } from "./index-visible.mjs";
28
29
  import { PackIndexes, packReader, seedBytes } from "./pack-reads.mjs";
29
30
  import { maintainBatches, withBatchesDue } from "./batch-upkeep.mjs";
@@ -628,11 +629,15 @@ const packsWith = (options = {}, inner = {}) => {
628
629
  * with the scope.
629
630
  */
630
631
  const objectsAt = (listed, on, handles) => Effect.map(Effect.acquireRelease(openReader(listed, on, void 0, void 0, handles), (reader) => Effect.sync(() => reader.close())), (reader) => (oid) => reader.read({ oid }));
632
+ /** The tips' reaches a read walked, for tips with no stored bitmap yet. */
633
+ const reaches = new ReachCache(instanceRoomOf(budget, reachCacheBytes));
634
+ /** The index files a build's chained steps read again, kept from one step to the next (#596). */
635
+ const stepFiles = new IndexStepFiles(instanceRoomOf(budget, indexStepFilesMost));
631
636
  /** What a reach on the index reads through: a want check's, and a visible set's. */
632
637
  const reachParts = {
633
638
  bucket,
634
639
  budget,
635
- cache: new ReachCache(instanceRoomOf(budget, reachCacheBytes)),
640
+ cache: reaches,
636
641
  objects: (listed) => objectsAt(listed, { read: true }),
637
642
  indexes
638
643
  };
@@ -1371,12 +1376,13 @@ const packsWith = (options = {}, inner = {}) => {
1371
1376
  ...visible === void 0 && { copyRows: (listed, input) => copyRowsPage(calls, listed, input) },
1372
1377
  index: {
1373
1378
  step: (listed, input) => {
1379
+ const kept = stepFiles.over(bucket, listed.repo);
1374
1380
  const raw = {
1375
1381
  store: base.store,
1376
- bucket
1382
+ bucket: kept
1377
1383
  };
1378
1384
  const unbounded = {
1379
- bucket,
1385
+ bucket: kept,
1380
1386
  store: base.store,
1381
1387
  budget,
1382
1388
  objects: (at, allotment, handles) => objectsAt(at, { upkeep: allotment }, handles)
@@ -1395,7 +1401,8 @@ const packsWith = (options = {}, inner = {}) => {
1395
1401
  }, listed, input).pipe(Effect.tap((answer) => answer.kind === "moved" && own !== void 0 ? countInto(own, "store", 1) : Effect.void));
1396
1402
  return own === void 0 ? step : Effect.provideService(step, CallBudget, own);
1397
1403
  };
1398
- return stepMost === void 0 ? counted(void 0) : Effect.flatMap(indexBudget(input, stepMost), counted);
1404
+ const step = stepMost === void 0 ? counted(void 0) : Effect.flatMap(indexBudget(input, stepMost), counted);
1405
+ return Effect.onExit(step, (exit) => Exit.isSuccess(exit) && exit.value.kind === "more" ? Effect.void : Effect.sync(() => stepFiles.clear()));
1399
1406
  },
1400
1407
  reach: (listed, input) => {
1401
1408
  const built = indexReach(reachParts, listed, input);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ref0/git",
3
- "version": "0.31.0",
3
+ "version": "0.32.0",
4
4
  "private": false,
5
5
  "description": "Git hosting as a library: a server, a client and their merges",
6
6
  "license": "Apache-2.0",
@@ -51,11 +51,11 @@
51
51
  }
52
52
  },
53
53
  "dependencies": {
54
- "@ref0/infra": "0.31.0"
54
+ "@ref0/infra": "0.32.0"
55
55
  },
56
56
  "devDependencies": {
57
57
  "@effect/vitest": "4.1.7",
58
- "@ref0/xdiff-wasm": "0.31.0",
58
+ "@ref0/xdiff-wasm": "0.32.0",
59
59
  "effect": "4.1.7",
60
60
  "vite-plus": "1.8.0",
61
61
  "vitest": "5.1.1",