Skip to content

Commit febd7de

Browse files
committed
perf: improve mutation and cleanup
1 parent 840d977 commit febd7de

3 files changed

Lines changed: 237 additions & 63 deletions

File tree

‎packages/effect/src/virtual-file-system/internal/atomic.ts‎

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -232,26 +232,6 @@ export const validate = (operation: Operation, options: unknown): Result.Result<
232232
// Transaction helpers
233233
// -----------------------------------------------------------------------------
234234

235-
const authorizeCreate = (
236-
tx: E.Transaction,
237-
parent: E.DirectoryNode,
238-
requested: number | undefined,
239-
kind: E.Node["kind"]
240-
) =>
241-
Result.gen(function*() {
242-
yield* tx.requireResult(parent, E.WRITE_PERMISSION | E.EXECUTE_PERMISSION)
243-
if (requested !== undefined) yield* E.modeResult(requested, kind, tx.operation)
244-
if (
245-
kind === "directory" && requested !== undefined && (requested & E.SET_GID_BIT) !== 0 &&
246-
!tx.caller.policy.privileged
247-
) {
248-
const gid = (parent.mode & E.SET_GID_BIT) !== 0 ? parent.gid : tx.caller.policy.gid
249-
if (gid !== tx.caller.policy.gid && !tx.caller.policy.groups.includes(gid)) {
250-
return yield* Result.fail(new M.AccessDenied({ operation: tx.operation, reason: "ownership" }))
251-
}
252-
}
253-
})
254-
255235
const reserveFile = (tx: E.Transaction, amount: bigint): Result.Result<void, M.OperationError> =>
256236
Result.gen(function*() {
257237
if (amount > BigInt(Number.MAX_SAFE_INTEGER)) {
@@ -516,7 +496,7 @@ const writable = (
516496
let selected = initial
517497
const create = options.create ?? "ifmissing"
518498
if (create === "exclusive") {
519-
yield* authorizeCreate(tx, initial.parent, options.mode, "file")
499+
yield* tx.authorizeCreateResult(initial.parent, options.mode, "file")
520500
yield* tx.conditionsResult(initial.parent, initial.binding, options.conditions)
521501
if (initial.binding !== undefined) {
522502
return yield* Result.fail(new M.AlreadyExists({ operation: tx.operation, reason: "occupiedEntry" }))
@@ -540,7 +520,7 @@ const writable = (
540520
yield* tx.conditionsResult(initial.parent, initial.binding, options.conditions)
541521
return yield* Result.fail(new M.NotFound({ operation: tx.operation, reason: "missingEntry" }))
542522
}
543-
yield* authorizeCreate(tx, selected.parent, options.mode, "file")
523+
yield* tx.authorizeCreateResult(selected.parent, options.mode, "file")
544524
yield* tx.conditionsResult(initial.parent, initial.binding, options.conditions)
545525
}
546526
if (options.expectedRevision !== undefined) {
@@ -712,7 +692,7 @@ export const createFile = (
712692
Result.gen(function*() {
713693
yield* validate("createFile", options)
714694
const slot = yield* tx.slotResult(target)
715-
yield* authorizeCreate(tx, slot.parent, options?.mode, "file")
695+
yield* tx.authorizeCreateResult(slot.parent, options?.mode, "file")
716696
yield* tx.conditionsResult(slot.parent, slot.binding, options?.conditions)
717697
if (slot.binding !== undefined) {
718698
return yield* Result.fail(new M.AlreadyExists({ operation: tx.operation, reason: "occupiedEntry" }))
@@ -736,7 +716,7 @@ const mkdirOne = (
736716
if (node.kind === "directory") return node
737717
return yield* Result.fail(new M.InvalidResource({ operation: tx.operation, reason: "wrongKind" }))
738718
}
739-
yield* authorizeCreate(tx, slot.parent, options.mode, "directory")
719+
yield* tx.authorizeCreateResult(slot.parent, options.mode, "directory")
740720
if (final) yield* tx.conditionsResult(slot.parent, slot.binding, options.conditions)
741721
if (slot.binding !== undefined) {
742722
return yield* Result.fail(new M.AlreadyExists({ operation: tx.operation, reason: "occupiedEntry" }))
@@ -769,8 +749,42 @@ export const makeDirectory = (
769749
let first = end - 1
770750
while (first >= 0 && bytes[first] !== CHAR_FORWARD_SLASH) first--
771751
yield* E.nameValueResult(Result.getOrThrow(BytePath.fromBytes(bytes.subarray(first + 1, end))), tx.operation)
752+
const length = BigInt(end)
753+
const runtime = tx.engine.runtime
754+
const limits = runtime.limits
772755
node = yield* tx.directoryResult(tx.caller.policy.cwd)
773-
for (let index = 0; index <= end; index++) {
756+
let prefixEnd = 0
757+
if (
758+
length <= limits.pathBytes && length <= limits.expandedPathBytes &&
759+
length * BigInt("2") <= limits.transientBytes - runtime.payload
760+
) {
761+
const supplied = Result.getOrThrow(BytePath.fromBytes(bytes.subarray(0, end)))
762+
const route = yield* tx.resolveResult(supplied, false, undefined, 0, [], undefined, options)
763+
node = route.object
764+
if (node.kind === "directory") {
765+
const result = tx.observation(node)
766+
if (result.kind !== "directory") {
767+
return yield* Result.fail(new M.InvalidResource({ operation: tx.operation, reason: "wrongKind" }))
768+
}
769+
return result
770+
}
771+
if (node.kind !== "symlink" || route.parent === undefined || route.mkdirPrefixEnd === undefined) {
772+
return yield* Result.fail(new M.InvalidResource({ operation: tx.operation, reason: "wrongKind" }))
773+
}
774+
const state: E.ResolutionState = { follows: 0 }
775+
yield* tx.consumeLinkResult(state)
776+
const followed = tx.resolveResult(node.target, true, route.parent.id, state.follows, [], state)
777+
if (Result.isFailure(followed) && followed.failure instanceof M.NotFound) {
778+
return yield* Result.fail(new M.InvalidResource({ operation: tx.operation, reason: "wrongKind" }))
779+
}
780+
node = (yield* followed).object
781+
if (node.kind !== "directory") {
782+
return yield* Result.fail(new M.InvalidResource({ operation: tx.operation, reason: "wrongKind" }))
783+
}
784+
prefixEnd = route.mkdirPrefixEnd
785+
}
786+
// Prefix resolution preserves error order under constrained budgets and through symlinks.
787+
for (let index = prefixEnd + 1; index <= end; index++) {
774788
if (index < end && bytes[index] !== CHAR_FORWARD_SLASH) continue
775789
if (index === 0 || bytes[index - 1] === CHAR_FORWARD_SLASH) continue
776790
const prefix = Result.getOrThrow(BytePath.fromBytes(bytes.subarray(0, index)))
@@ -837,7 +851,7 @@ export const makeSymlink = (
837851
return yield* Result.fail(new M.LimitExceeded({ operation: tx.operation, domain: "path", stage: "resolution" }))
838852
}
839853
const slot = yield* tx.slotResult(target)
840-
yield* authorizeCreate(tx, slot.parent, undefined, "symlink")
854+
yield* tx.authorizeCreateResult(slot.parent, undefined, "symlink")
841855
yield* tx.conditionsResult(slot.parent, slot.binding, options)
842856
if (slot.binding !== undefined) {
843857
return yield* Result.fail(
@@ -890,7 +904,7 @@ export const makeHardLink = (
890904
)
891905
}
892906
const slot = yield* tx.slotResult(destination)
893-
yield* authorizeCreate(tx, slot.parent, undefined, node.kind)
907+
yield* tx.authorizeCreateResult(slot.parent, undefined, node.kind)
894908
yield* tx.conditionsResult(slot.parent, slot.binding, options)
895909
if (slot.binding !== undefined) {
896910
return yield* Result.fail(
@@ -1014,7 +1028,7 @@ export const publishCopiedFile = (
10141028
yield* validate("publishCopiedFile", options)
10151029
const slot = yield* tx.slotResult(destination)
10161030
const existing = slot.binding === undefined ? undefined : yield* tx.getResult(slot.binding.object)
1017-
if (existing === undefined) yield* authorizeCreate(tx, slot.parent, options?.mode, "file")
1031+
if (existing === undefined) yield* tx.authorizeCreateResult(slot.parent, options?.mode, "file")
10181032
else yield* tx.requireResult(existing, E.WRITE_PERMISSION)
10191033
if (existing !== undefined && options?.preserveTimes !== undefined) yield* tx.ownsResult(existing)
10201034
yield* tx.conditionsResult(slot.parent, slot.binding, options?.destination)

‎packages/effect/src/virtual-file-system/internal/capture.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ class Candidate extends R.ChargedBytes {
508508
if (node.kind === "directory") parent.nlink++
509509
else node.nlink++
510510
}
511+
engine.initializeCapacity()
511512
engine.nextObject = maxObject + one
512513
return engine
513514
})

0 commit comments

Comments
 (0)