Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
d19d98f
perf(gc): mark reads record headers, not pack bodies (layerfs-project…
yifanxuaaa Nov 17, 2026
f33b44a
feat(gc): per-statement SQL profile and gc_profile example (layerfs-p…
yifanxuaaa Nov 17, 2026
962acd4
perf(gc): mark reads pack heads in one call, not per-pack statements …
yifanxuaaa Nov 17, 2026
7b2f9ac
perf(gc): size the SQLite rebuild's fresh file before the bulk load (…
yifanxuaaa Nov 17, 2026
d463879
perf(gc): copy packs in windows and read the sealed Store ahead (laye…
yifanxuaaa Nov 17, 2026
7f5bb75
perf(gc): drop the post-mark record rewrite and the post-build direct…
yifanxuaaa Nov 17, 2026
8e7acb3
perf(gc): a Disposable Store's compaction asks for no fsync (layerfs-…
yifanxuaaa Nov 17, 2026
c1c2f98
perf(gc): mark reads ranges for a whole wave in a few calls (layerfs-…
yifanxuaaa Nov 17, 2026
4753f7a
perf(gc): one statement where compaction repeated a scan or an open (…
yifanxuaaa Nov 17, 2026
24eec81
Merge branch 'claude/gc-opt-sqlite-rebuild': faster SQLite rebuild (l…
yifanxuaaa Nov 17, 2026
f24eadb
Revert "perf(gc): a Disposable Store's compaction issues no fsync"
yifanxuaaa Nov 17, 2026
cb0a97b
perf(gc): the windowed pack copy seeks only its own window's packs
yifanxuaaa Nov 17, 2026
c15826e
perf(gc): mark reads object locations without sorting them
yifanxuaaa Nov 17, 2026
c1d48b6
perf(gc): mark's first read of a pack stays on the pack row's own page
yifanxuaaa Nov 17, 2026
d600429
perf(gc): mark's walk looks objects up in constant time
yifanxuaaa Nov 17, 2026
97169f0
perf(gc): PostgreSQL Phase A reads each batch from an indexed copy se…
yifanxuaaa Nov 17, 2026
11242b2
perf(gc): PostgreSQL answers a wave's pack heads in one statement (la…
yifanxuaaa Nov 17, 2026
51944d2
perf(gc): PostgreSQL answers a wave's pack ranges in one statement (l…
yifanxuaaa Nov 17, 2026
0bb8989
perf(gc): PostgreSQL checks the scratch rows before the Store-wide lo…
yifanxuaaa Nov 17, 2026
dc6734b
perf(gc): PostgreSQL follows the history closure by primary key (laye…
yifanxuaaa Nov 17, 2026
305007f
fix(gc): PostgreSQL records a dead digest root once (layerfs-project#41)
yifanxuaaa Nov 17, 2026
86f5a4a
perf(gc): PostgreSQL deletes dead digest rows by primary key (layerfs…
yifanxuaaa Nov 17, 2026
9dcecc8
perf(gc): PostgreSQL loads the visited set without a second copy (lay…
yifanxuaaa Nov 17, 2026
9da6038
perf(gc): kept-waste reads pack directory prefixes a batch per call
yifanxuaaa Nov 17, 2026
f22faa7
Merge branch 'claude/gc-opt-pg-rebuild': faster PostgreSQL store.comp…
yifanxuaaa Nov 17, 2026
58aa062
docs(gc): record the PostgreSQL swap order as built
yifanxuaaa Nov 17, 2026
e180a41
feat(gc): pg_gc_profile, a counted mark or compaction on PostgreSQL
yifanxuaaa Nov 17, 2026
ba6cba3
perf(gc): PostgreSQL mark reads metadata packs and inode values ahead
yifanxuaaa Nov 17, 2026
ca82dbc
perf(gc): PostgreSQL loads the visited set as id arrays, not a row COPY
yifanxuaaa Nov 17, 2026
032b212
test(gc): the PostgreSQL closure text must follow the shared text's e…
yifanxuaaa Nov 17, 2026
9971d6e
Merge remote-tracking branch 'origin/layerfs_v2_0_0' into claude/gc-m…
yifanxuaaa Nov 17, 2026
4c359ef
fix(gc): the SQLite settle writes inside the writer lane
yifanxuaaa Nov 17, 2026
3190e6c
fix(sqlite): a refused seal returns instead of waiting for its own lane
yifanxuaaa Nov 17, 2026
c32a6dc
test(doctor): the PostgreSQL doctor fixture selects its commit mode
yifanxuaaa Nov 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions core/crates/layerfs-storage/src/encoding/delta/edge.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
//! A payload record's dependency edge from bounded reads of its pack.
//!
//! [`super::read::ChainBases::edge`] borrows the record's complete encoded
//! group, so a walk that wants only edges pays for every payload byte it
//! passes. An uncompressed group keeps everything an edge needs at offsets its
//! own framing names: the record count and directory at the group's head, then
//! the record's first bytes. [`sparse_directory`] and [`sparse_edge`] read
//! those from whatever the caller already holds and name the next range it
//! lacks, so the caller decides how to batch the reads. No payload byte is
//! read, and so none is authenticated here: a consumer that reconstructs the
//! record still does that.
//!
//! A group's directory is validated and parsed once into a
//! [`RecordDirectory`]; each record's range is then one step. Asking the
//! framing reader for every record of a group instead scans the directory once
//! per record.
use super::read::ChainEdge;
use super::record;
use crate::error::{StorageError, StorageResult};
use crate::location::ObjectLocation;
use crate::pack::layout::{record_range, GroupCodec, GroupView, PackLane};
use crate::policy::RECORD_COUNT_LIMIT;
use crate::port::PackRange;

/// Bytes at a group's head a walk asks for first: the record count, the
/// directory of a group of up to 100 records, and usually its first header.
pub const HEAD_BYTES: usize = 512;

/// One uncompressed group's record directory, validated whole exactly as
/// [`crate::encoding::framed_record`] validates it.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RecordDirectory {
/// Record `n` spans `bounds[n]..bounds[n + 1]` of the group.
bounds: Vec<usize>,
}

impl RecordDirectory {
/// The directory of a complete uncompressed group body.
pub fn of(group: &[u8]) -> StorageResult<Self> {
let count = count_of(group.get(..4), group.len())?;
let ends = group
.get(4..4 + 4 * count)
.ok_or(StorageError::Integrity("group record directory"))?;
Self::parse(count, ends, group.len())
}

fn parse(count: usize, ends: &[u8], group_length: usize) -> StorageResult<Self> {
// The one validator of a directory: counts, ordering and the exact
// record area. What follows only copies offsets it accepted.
record_range(count, ends, group_length, 0)?;
let area = 4 + ends.len();
let mut bounds = Vec::with_capacity(count + 1);
bounds.push(area);
for end in ends.chunks_exact(4) {
let mut field = [0; 4];
field.copy_from_slice(end);
bounds.push(area + u32::from_le_bytes(field) as usize);
}
Ok(Self { bounds })
}

/// The records the group holds.
pub fn count(&self) -> usize {
self.bounds.len() - 1
}

/// The group-relative range of record `number`.
pub fn range(&self, number: usize) -> StorageResult<(usize, usize)> {
match (self.bounds.get(number), self.bounds.get(number + 1)) {
(Some(start), Some(end)) => Ok((*start, *end)),
_ => Err(StorageError::Integrity("group record directory")),
}
}
}

fn count_of(field: Option<&[u8]>, group_length: usize) -> StorageResult<usize> {
let mut count = [0; 4];
count.copy_from_slice(field.ok_or(StorageError::Integrity("group framing"))?);
let count = u32::from_le_bytes(count) as usize;
if !(1..=RECORD_COUNT_LIMIT).contains(&count) || 4 + 4 * count > group_length {
return Err(StorageError::Integrity("group record directory"));
}
Ok(count)
}

/// What the bytes a caller holds answer.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Sparse<T> {
/// The answer.
Ready(T),
/// The pack range to acquire before asking again.
Need(PackRange),
}

/// Whether a record of the group `view` of a `lane` pack has an edge these
/// readers can report. A compressed group and the role-dependent `Ordinary`
/// lane take the decoding reader.
pub fn has_sparse_edges(lane: PackLane, view: GroupView) -> bool {
lane != PackLane::Ordinary && view.codec == GroupCodec::Raw
}

/// The record directory of the uncompressed group `view`, from the pack bytes
/// `held` returns.
///
/// `held(offset, length)` returns exactly `length` bytes at pack offset
/// `offset`, or `None` when the caller does not hold all of them.
pub fn sparse_directory<'a>(
view: GroupView,
held: impl Fn(usize, usize) -> Option<&'a [u8]>,
) -> StorageResult<Sparse<RecordDirectory>> {
let length = view
.end
.checked_sub(view.start)
.filter(|length| *length >= 4)
.ok_or(StorageError::Integrity("group framing"))?;
let need = |length: usize| {
Sparse::Need(PackRange {
offset: view.start,
length,
})
};
let Some(count) = held(view.start, 4) else {
return Ok(need(length.min(HEAD_BYTES)));
};
let count = count_of(Some(count), length)?;
let Some(ends) = held(view.start + 4, 4 * count) else {
return Ok(need((4 + 4 * count + record::HEADER_BYTES).min(length)));
};
RecordDirectory::parse(count, ends, length).map(Sparse::Ready)
}

/// The edge of the record `location` names in the group `view` of a `lane`
/// pack, exactly as [`super::read::ChainBases::edge`] reports it, from the
/// group's `directory` and the pack bytes `held` returns.
pub fn sparse_edge<'a>(
lane: PackLane,
view: GroupView,
directory: &RecordDirectory,
location: &ObjectLocation,
held: impl Fn(usize, usize) -> Option<&'a [u8]>,
) -> StorageResult<Sparse<ChainEdge>> {
if !has_sparse_edges(lane, view) {
return Err(StorageError::Integrity("encoded record edge"));
}
if lane == PackLane::Singleton && location.record_number != 0 {
return Err(StorageError::Integrity("singleton record ordinal"));
}
let (start, end) = directory.range(location.record_number)?;
let width = end - start;
let header = width.min(record::HEADER_BYTES);
let Some(bytes) = held(view.start + start, header) else {
return Ok(Sparse::Need(PackRange {
offset: view.start + start,
length: header,
}));
};
Ok(Sparse::Ready(ChainEdge {
base: record::header_base(lane, bytes, width, location.canonical_length)?,
width: width as u64,
}))
}
1 change: 1 addition & 0 deletions core/crates/layerfs-storage/src/encoding/delta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

pub mod branch;
pub mod candidates;
pub mod edge;
pub mod read;
pub mod record;
pub mod select;
42 changes: 36 additions & 6 deletions core/crates/layerfs-storage/src/encoding/delta/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,15 +440,45 @@ impl<'a> ChainBases<'a> {
workspace: &mut DecompressionWorkspace,
location: &ObjectLocation,
) -> StorageResult<ChainEdge> {
let base = self.base_of(connection, workspace, location)?;
let (selected, _) = group_of(self.packs, connection, location)?;
Ok(ChainEdge {
base,
width: selected_record_width(selected, location)?,
})
stored_edge(
connection,
self.packs,
&mut self.groups,
workspace,
&mut self.group_decodes,
location,
)
}
}

/// [`ChainBases::edge`] over caches the caller owns.
///
/// A walk that also decodes passes its own decoded-group cache, so a
/// compressed group it already decoded, or whose other records it asks about,
/// is decompressed once and not once per record.
pub fn stored_edge(
connection: &dyn Source,
packs: &mut crate::encoding::PackCache,
groups: &mut GroupCache,
workspace: &mut DecompressionWorkspace,
group_decodes: &mut u64,
location: &ObjectLocation,
) -> StorageResult<ChainEdge> {
let base = stored_base(
connection,
packs,
groups,
workspace,
group_decodes,
location,
)?;
let (selected, _) = group_of(packs, connection, location)?;
Ok(ChainEdge {
base,
width: selected_record_width(selected, location)?,
})
}

/// One stored record of a chain, as a cost walk meets it.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ChainEdge {
Expand Down
60 changes: 60 additions & 0 deletions core/crates/layerfs-storage/src/encoding/delta/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub const fn is_stored(tag: u8) -> bool {
/// Width of one canonical identity.
const ID_BYTES: usize = 32;

/// The widest record prefix that carries a payload lane's tag, length fields
/// and base identity: the framed lane's nine bytes and one identity.
pub const HEADER_BYTES: usize = 9 + ID_BYTES;

/// One parsed lane record.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ParsedRecord<'a> {
Expand Down Expand Up @@ -358,3 +362,59 @@ pub fn stored_base(
}
Ok(parse(lane, record, canonical_length)?.base)
}

/// The direct base a payload-lane record names, read from its first bytes.
///
/// `header` is the record's first `min(record_length, HEADER_BYTES)` bytes and
/// `record_length` its complete stored width, which the group directory gives.
/// This accepts and refuses exactly the records [`stored_base`] does on the
/// complete record: the tag, the base identity, the length fields and the
/// non-empty frame are all decided by the header and the width, so only the
/// frame's own bytes go unread. The `Ordinary` lane has no fixed header (its
/// groups are compressed and its grammar depends on the role) and is refused.
pub fn header_base(
lane: PackLane,
header: &[u8],
record_length: usize,
canonical_length: usize,
) -> StorageResult<Option<ObjectId>> {
let fixed = match lane {
PackLane::WholeFile => 1,
PackLane::Chunk => 5,
PackLane::Singleton => 9,
PackLane::Ordinary => return Err(StorageError::Integrity("ordinary record header")),
};
if header.len() != record_length.min(HEADER_BYTES) || header.len() < fixed {
return Err(StorageError::Integrity("record header width"));
}
let field = |at: usize| {
let mut bytes = [0; 4];
bytes.copy_from_slice(&header[at..at + 4]);
u32::from_le_bytes(bytes) as usize
};
let (base, start) = match header[0] {
FULL_TAG | STORED_TAG => (None, fixed),
PREFIX_TAG => (
Some(ObjectId::from_bytes(
header
.get(fixed..fixed + ID_BYTES)
.ok_or(StorageError::Integrity("record base identity"))?,
)?),
fixed + ID_BYTES,
),
_ => return Err(StorageError::Integrity("record tag")),
};
let frame = record_length
.checked_sub(start)
.filter(|frame| *frame > 0)
.ok_or(StorageError::Integrity("record frame"))?;
let raw_known = match lane {
// The compact lane drops its length fields; the locator carries them.
PackLane::WholeFile => canonical_length > crate::policy::WHOLE_FILE_CANONICAL_OVERHEAD,
_ => field(1) != 0,
};
if !raw_known || (lane == PackLane::Singleton && field(5) != frame) {
return Err(StorageError::Integrity("record framing"));
}
Ok(base)
}
Loading