Skip to content

Removing every name of one file in path order grows with their number, not its square: 80,000 names in 24 ms instead of 37 s - #308

Open
closer-claudio wants to merge 3 commits into
mainfrom
claudio/20261123-handover
Open

closer-claudio wants to merge 3 commits into
mainfrom
claudio/20261123-handover

Conversation

@closer-claudio

Copy link
Copy Markdown

This is the third layer of a stack: it sits on #305 and #307, whose commits show in the diff until they merge. Its own commit is "A file whose first name goes stays in its entry, which takes another name".

The defect

A file with several names is one entry holding the inode and one entry for each other name, pointing at it. When the holding name was removed, the inode was handed to another name's entry: the lowest path was looked for in a pass over all the names, the inode copied into its entry, and every other name pointed at it.

Removing the names of one file so that each is the holder when its turn comes, which path order is, cost the square of their number: 20,000 names took 1.3 s, 80,000 names 37 to 40 s.

The change

The file stays in the entry that holds it. That entry takes the name of the lowest path left and its place in the tree, and the name's own entry takes the name that is going, and is what is removed. No other name is touched: they point at the entry that holds the file still.

  • The lowest path comes from a heap of the package's own. A name linked waits in a list, at the cost of an append, until a first name is removed; only then are the names put in order. A file whose first name never goes pays nothing for it, in whatever order its names are linked.
  • Names that can take nothing, removed or under a removed directory, are left out as the others are put in order: with a directory going they are all of its names.
  • What a caller holds keeps its name. An entry's path was fixed for its life until now, and an entry of a listing read it when asked for its name. With the holding entry taking another name it would answer the new one, and a listing walked to remove each entry by its Name would ask twice for one name and never for another. A listing's entry now carries the name it was listed under, as an info and an open file carry theirs.

What a consumer sees

The same images, byte for byte, and the same answers from every call.

Shape Before After
every name of one file removed in path order, 20,000 names 1.35 s 4.7 ms
the same, 80,000 names 37 to 40 s 24 ms
80,000 later names removed with their directory 2.2 ms 2.1 to 2.3 ms
80,000 later names removed one at a time 7.7 to 7.9 ms 7.1 to 7.2 ms
every name of one file removed with its directory, 80,000 names 3.1 ms 3.0 to 3.3 ms
100,000 names linked to one file, rising or falling order 16.8 to 17.3 ms 16.2 to 16.5 ms
16,000 files of two names, the first name of each removed 1.02 to 1.04 ms 1.25 to 1.29 ms

The last row is what it costs: a handover is one write to the map of paths more than it was, about 15 ns a file.

Evidence

In contract_unlink_linked_test.go:

  • TestContractRemovingManyNamesOfOneFileGrowsWithTheirNumber has two more shapes: every name in path order (50,000 names within five seconds: 16 ms here, 120 ms under the race detector, about 14 s before the change), and the same with 20,000 names linked once the first are in order.
  • TestContractWhatACallerHoldsKeepsItsName: each entry of a listing removed by its name, each file the standard library's walk meets removed, and a listing, an info and an open file held while the first names go.
  • TestContractAFilesNamesComeAndGo: 400 random sequences of one file's names linked and removed, the image held to that of the file made under the name a model gives and linked to the others.
  • TestContractAFileHandedOnAgainIsUnderItsLowestName and TestContractTheLowestNameTakesAFileAfterOthersWent: the lowest path through names linked between two handovers, linked and removed by the dozen, and through the removed being dropped from names in order.

In contract_writer_removal_test.go:

  • TestContractAMarkerOverAFilesFirstNameLeavesItUnderItsOther: the first name taken by an opaque marker, a whiteout of it or of its directory, RemoveAll, a file where its directory was.
  • The heap test has four more shapes, where a file is handed on.

All of these pass before the change but the two bound shapes: what a call answers is the same.

Broken twenty-eight ways, each over the whole package: twenty-six fail a test. Two do not: a name under a removed directory allowed to take a file, which no sequence of calls reaches (a directory is removed with all it holds, in one call), and names that take nothing put in order all the same, which costs time alone.

BenchmarkPerfRemoveNamesOfOneInode has the path-order shape. just lint and just test pass.

For #296

closer-claudio and others added 3 commits November 23, 2026 01:05
…g of it

A name removed from a Writer stayed in its directory's list of children,
marked removed, for the Writer's life: nothing took it out, and every
reader of the list stepped over it. A name made and removed over and
over grew the Writer by an entry each time (300,000 rounds: about
90 MiB), and every pass over the directory walked them all.

Now an entry knows its place among its parent's children and leaves the
list when its name is removed: the last child takes its place, the
list's order being no one's (ReadDir and Close sort). A children list
holds no removed entry, so the readers that stepped over them no longer
look.

Two walks remove entries of the list they are walking, where taking one
out would move the last child under the walk. They drop the names and
settle the list themselves once done: RemoveAll and a directory that a
copy turns into a file empty it; a marker of a merged layer sifts out
what it removed and gives the entries it spared their new places.

An entry written over in place by a later copy, and the name that takes
over a removed first name's file, are copies of another entry: each
keeps its own place.

The list of a file's other names keeps a removed name until the file's
last other name goes or a handover passes over it, and was then all
that held the entry: it drops the removed once they outnumber the
live, at a cost each removal has paid for.

What is not given back: the capacity a map and a list keep of the most
they held at once (300,000 names made and then removed: 16 MiB, where
the names themselves were 108).

Removal costs what it did: BenchmarkPerfRemoveLinkedNames and
BenchmarkPerfRemoveNamesOfOneInode on the commit before and here, two
runs each, alternated: every pair of ranges overlaps (16,000 linked
files 1.2 to 1.4 ms and 1.1 to 2.0 ms; 80,000 names of one file removed
one at a time 7.1 to 10.1 ms and 9.9 to 11.5 ms).

Tests, in contract_writer_removal_test.go.
TestContractWriterHoldsNothingOfARemovedName: the heap in use after
300,000 rounds of a directory made and removed, of a file's other name
made and removed beside one that stays, of a directory removed with
what it holds, each under 8 MiB, and after 300,000 names made and then
each removed, under 32. All four fail before the change (89, 92, 270
and 108 MiB).
TestContractWriterLeavesWhatItsCallsLeave: 600 random sequences of
Create, Mkdir, Link, Remove and RemoveAll against a model of the tree:
the image holds the model's names, each file's content, one inode for
the names that share one, the link counts that follow. It passes before
the change: the behaviour is the same.
TestContractRemovingWhatACopyPlaced: a name removed after a copy wrote
over it, after its layer's opaque marker spared it, a spared directory
whose earlier entries the marker took, and a name that stopped being a
directory.
Broken twelve ways, each fails a test: the heir or the overwritten
entry losing its place, the moved sibling keeping its old one, no new
place after the sift, no sift, a removed directory keeping its list, no
detach, a new child without a place, the freed slot not cleared, the
alias list unbounded, a marker dropping its own layer, a directory
with entries removed.

For #297

Signed-off-by: closer-claudio <claudio@farcloser.world>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…name

A file's second name is an entry of its own in a Writer, pointing at
the entry that holds the file and carrying only a mode, the one the
file had when the name was linked. Stat and Open followed the pointer
and handed out the holding entry, whose path is the first name's: Stat
of a second name, and the info of a file opened under it, were named
after the first. A listing handed out the second name's own entry: its
info said no bytes, the mode of the day it was linked, and no time.

For /d/a of 12 bytes linked as /d/b and then made 0600: the listing's
b said 0 bytes and 0644 where Stat("/d/b") said 12 and 0600, and
Stat("/d/b").Name() was "a". The image written at Close was right, and
so was what a reader of it answers; the Writer's documentation says an
entry reads back before Close as it will after.

An info now carries the name it was asked or listed under, and
describes the file: for a second name, the entry it points at. A file
opened under a name is named so, in its info and in the error of a
Close that fails.

TestContractAFilesSecondNameReadsAsTheFile: a file under two names, its
mode, owner, time and an attribute set after the second name was
linked, through either name: the listing's entry, Stat and the open
file's info of each name say the same, each under its own name, before
Close and from the image after it. Before the change the three ways
disagree for the second name. With the second name's own fields kept,
with the open file named as the first name, or with the name read from
the holding entry, it fails.

For #306

Signed-off-by: closer-claudio <claudio@farcloser.world>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…name

A file with several names is one entry holding the inode and one entry
for each other name, pointing at it. When the holding name was removed
the inode was handed to another name's entry: the lowest path was
looked for in a pass over all the names, the inode copied into its
entry, and every other name pointed at it. Removing the names of one
file so that each is the holder when its turn comes, which path order
is, cost the square of their number: 20,000 names 1.3 s, 80,000 names
37 to 40 s.

Now the file stays in the entry that holds it. That entry takes the
name of the lowest path left and its place in the tree, and the name's
own entry takes the name that is going, and is what is removed. No
other name is touched: they point at the entry that holds the file
still. The image is the same: the entry that held the inode after a
handover was a copy of the one before, under the same name.

An entry's path was fixed for its life until now, and an entry of a
listing read it when asked for its name. With the holding entry taking
another name it would answer the new one, and a listing walked to
remove each entry by its Name would ask twice for one name and never
for another. A listing's entry now carries the name it was listed
under, as an info and an open file carry theirs.

The lowest path comes from a heap of the package's own. A name linked
waits in a list, at the cost of an append, until a first name is
removed; only then are the names put in order, so a file whose first
name never goes pays nothing for it, in whatever order its names are
linked. Names that can take nothing, removed or under a removed
directory, are left out as the others are put in order: with a
directory going they are all of its names, and ordering them to find
none cost five times what the pass did (80,000 names 3.1 ms and 19 ms,
measured on a draft of this change). A name removed while it waits or
is in order stays until it comes out or the removed outnumber the live,
as before.

An entry is marked removed in one place, and it is the entry that is
gone: the one of the name the file went on under, when it went on.

Measured, the commit before and this one, three runs each, alternated,
on a quiet machine (BenchmarkPerfRemoveNamesOfOneInode, a shape added;
BenchmarkPerfRemoveLinkedNames):
- every name of one file removed in path order: 20,000 names 1.35 s
  and 4.7 ms; 80,000 names 37 to 40 s and 24 ms.
- beside it, unchanged: 80,000 later names removed with their directory
  2.2 ms and 2.1 to 2.3 ms, one at a time 7.7 to 7.9 ms and 7.1 to
  7.2 ms; every name with its directory 3.1 ms and 3.0 to 3.3 ms;
  100,000 names linked to one file, in rising and in falling order,
  16.8 to 17.3 ms and 16.2 to 16.5 ms.
- what it costs: a handover is one write to the map of paths more than
  it was. 16,000 files of two names, the first name of each removed:
  1.02 to 1.04 ms and 1.25 to 1.29 ms, about 15 ns a file.

Tests.
contract_unlink_linked_test.go: the bound test has the path-order
shape, 50,000 names in five seconds (16 ms here, 120 ms under the race
detector, some 14 s on the commit before), and the same with 20,000
names linked once the first are in order, which holds that the names
that waited join the others once. Each shape of that test now has its
own number of names.
TestContractWhatACallerHoldsKeepsItsName: each entry of a listing
removed by its name, each file the standard library's walk meets
removed, and a listing, an info and an open file held while the first
names go: the names they answer do not change.
TestContractAFilesNamesComeAndGo: 400 random sequences of one file's
names linked and removed, the image held to that of the file made under
the name the model says and linked to the others.
TestContractAFileHandedOnAgainIsUnderItsLowestName and
TestContractTheLowestNameTakesAFileAfterOthersWent: the lowest path
through names linked between two handovers, linked and removed by the
dozen, and through the removed being dropped from names in order.
contract_writer_removal_test.go:
TestContractAMarkerOverAFilesFirstNameLeavesItUnderItsOther: the first
name taken by an opaque marker, a whiteout of it or of its directory,
RemoveAll, a file where its directory was. The heap test has four more
shapes, where a file is handed on. All of these pass on the commit
before but the bound: what a call answers is the same.
Broken twenty-eight ways, each over the whole package: twenty-six fail
a test, the four that read a held name late among them. Two do not: a name under a removed directory allowed to take a
file, which no sequence of calls reaches (a directory is removed with
all it holds, in one call); and names that take nothing put in order
all the same, which costs time alone. The handover's answer for a file
with no record of other names is reached by no call either: a count
above zero has a record.

For #296

Signed-off-by: closer-claudio <claudio@farcloser.world>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@closer-claudio

Copy link
Copy Markdown
Author

Reviewed at 1eb9581

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant