From 2afc6b5854f62bed92c0537ae9b9d3f4d45f4087 Mon Sep 17 00:00:00 2001 From: Nittin Murthi Date: Wed, 28 Oct 2026 12:26:12 -0700 Subject: [PATCH 1/5] perf(diskgraph): avoid temporary allocations in node_data --- .../diskgraph/diskgraph-index/src/layout.rs | 230 +++++++++++++++++- 1 file changed, 229 insertions(+), 1 deletion(-) diff --git a/packages/diskgraph/diskgraph-index/src/layout.rs b/packages/diskgraph/diskgraph-index/src/layout.rs index add4c41f6..9f417556c 100644 --- a/packages/diskgraph/diskgraph-index/src/layout.rs +++ b/packages/diskgraph/diskgraph-index/src/layout.rs @@ -91,7 +91,70 @@ impl<'a> EdgeListView<'a> { } pub fn node_data(&self) -> Result, std::io::Error> { - Ok(self.parse()?.1) + if self.raw.is_empty() { + return if self.has_node_data { + Err(std::io::ErrorKind::InvalidData.into()) + } else { + Ok(None) + }; + } + + // Validate the entire count prefix before lengths or edge sizes, as + // parse() does. Replaying this small prefix avoids allocating counts + // and relation segments that a feature-only read would discard. + let mut cursor = 0usize; + for _ in self.layouts.iter() { + let (_, consumed) = decode_varint(&self.raw[cursor..])?; + cursor = cursor + .checked_add(consumed) + .ok_or(std::io::ErrorKind::InvalidData)?; + } + let node_data_len = if self.has_node_data { + let len_bytes = self.node_data_len_bytes as usize; + if len_bytes == 0 || len_bytes > 4 { + return Err(std::io::ErrorKind::InvalidData.into()); + } + if cursor + len_bytes > self.raw.len() { + return Err(std::io::ErrorKind::UnexpectedEof.into()); + } + let len = read_len_le(&self.raw[cursor..cursor + len_bytes]); + cursor += len_bytes; + Some(len) + } else { + None + }; + + let mut count_cursor = 0usize; + for layout in self.layouts.iter() { + let (count, consumed) = decode_varint(&self.raw[count_cursor..])?; + count_cursor += consumed; // Validated by the first pass. + let bytes = count + .checked_mul(layout.entry_bytes as u32) + .ok_or(std::io::ErrorKind::InvalidData)? as usize; + cursor = cursor + .checked_add(bytes) + .ok_or(std::io::ErrorKind::InvalidData)?; + if cursor > self.raw.len() { + return Err(std::io::ErrorKind::UnexpectedEof.into()); + } + } + if let Some(len) = node_data_len { + let end = cursor + .checked_add(len) + .ok_or(std::io::ErrorKind::InvalidData)?; + if end > self.raw.len() { + return Err(std::io::ErrorKind::UnexpectedEof.into()); + } + if end != self.raw.len() { + return Err(std::io::ErrorKind::InvalidData.into()); + } + Ok(Some(&self.raw[cursor..end])) + } else { + if cursor != self.raw.len() { + return Err(std::io::ErrorKind::InvalidData.into()); + } + Ok(None) + } } pub fn pk_matches( @@ -495,3 +558,168 @@ mod pk_value_matches_tests { ); } } + +#[cfg(test)] +mod node_data_tests { + use super::*; + use std::io::ErrorKind; + use std::sync::Arc; + + fn layouts(widths: &[usize]) -> Arc> { + Arc::new( + widths + .iter() + .enumerate() + .map(|(i, &entry_bytes)| RelationLayout { + relation_id: i as u16, + dst_bits: 8, + dest_sort_key_bits: None, + entry_bytes, + dst_mask: 255, + dest_sort_key_mask: None, + }) + .collect(), + ) + } + + fn check(view: &EdgeListView<'_>) { + let expected = view.parse().map(|(_, data)| data).map_err(|e| e.kind()); + let actual = view.node_data().map_err(|e| e.kind()); + assert_eq!( + actual, expected, + "raw={:?}, length width={}", + view.raw, view.node_data_len_bytes + ); + if let Ok(Some(data)) = actual { + assert_eq!(data.as_ptr(), expected.unwrap().unwrap().as_ptr()); + } + } + + #[test] + fn node_data_matches_parse_for_valid_rows_and_all_truncations() { + for count_width in 1..=4 { + for len_width in 1..=4 { + for has_node_data in [false, true] { + // Non-minimal count widths are accepted by the existing parser. + let mut raw = Vec::new(); + for count in [2u32, 3] { + let encoded = count | (((count_width - 1) as u32) << (count_width * 8 - 2)); + raw.extend_from_slice(&encoded.to_be_bytes()[4 - count_width..]); + } + // Preserve the old one-byte length behavior (read_len_le returns 0). + let payload_len = if len_width == 1 { 0u32 } else { 7 }; + if has_node_data { + raw.extend_from_slice(&payload_len.to_le_bytes()[..len_width]); + } + raw.extend_from_slice(&[17; 2 + 3 * 8]); + if has_node_data { + raw.resize(raw.len() + payload_len as usize, 29); + } + let view = EdgeListView { + raw: &raw, + layouts: layouts(&[1, 8]), + has_node_data, + node_data_len_bytes: len_width as u8, + }; + let expected = if has_node_data { + Some(&raw[raw.len() - payload_len as usize..]) + } else { + None + }; + assert_eq!(view.node_data().unwrap(), expected); + check(&view); + for end in 0..raw.len() { + check(&EdgeListView { + raw: &raw[..end], + layouts: view.layouts.clone(), + has_node_data, + node_data_len_bytes: len_width as u8, + }); + } + raw.push(99); + check(&EdgeListView { + raw: &raw, + layouts: layouts(&[1, 8]), + has_node_data, + node_data_len_bytes: len_width as u8, + }); + } + } + } + } + + #[test] + fn node_data_preserves_error_precedence_and_empty_rows() { + type Case<'a> = (&'a [u8], &'a [usize], bool, u8, Option); + let cases: &[Case<'_>] = &[ + (&[], &[8], false, 255, None), + (&[], &[], true, 2, Some(ErrorKind::InvalidData)), + (&[0], &[], true, 1, None), + (&[0, 0], &[8], true, 0, Some(ErrorKind::InvalidData)), + (&[0, 0], &[8], true, 5, Some(ErrorKind::InvalidData)), + // Missing second count takes precedence over invalid length width. + (&[0], &[1, 8], true, 0, Some(ErrorKind::UnexpectedEof)), + // Missing node-data length takes precedence over edge-size overflow. + ( + &[255, 255, 255, 255], + &[8], + true, + 2, + Some(ErrorKind::UnexpectedEof), + ), + // Earlier segment EOF takes precedence over later count multiplication overflow. + ( + &[2, 255, 255, 255, 255, 0, 0], + &[1, 8], + true, + 2, + Some(ErrorKind::UnexpectedEof), + ), + ( + &[255, 255, 255, 255, 0, 0], + &[8], + true, + 2, + Some(ErrorKind::InvalidData), + ), + (&[0, 0, 7], &[], true, 2, Some(ErrorKind::InvalidData)), + (&[7], &[], false, 2, Some(ErrorKind::InvalidData)), + ]; + for &(raw, widths, has_node_data, node_data_len_bytes, error) in cases { + let view = EdgeListView { + raw, + layouts: layouts(widths), + has_node_data, + node_data_len_bytes, + }; + check(&view); + assert_eq!(view.node_data().err().map(|e| e.kind()), error); + } + } + + #[test] + fn node_data_matches_parse_for_arbitrary_buffers() { + let mut seed = 42u64; + let mut next = || { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + seed + }; + for _ in 0..20_000 { + let len = (next() % 128) as usize; + let raw: Vec<_> = (0..len).map(|_| next() as u8).collect(); + let relation_count = (next() % 9) as usize; + let widths: Vec<_> = (0..relation_count) + .map(|_| [0, 1, 8, 64, u32::MAX as usize, usize::MAX][(next() % 6) as usize]) + .collect(); + let view = EdgeListView { + raw: &raw, + layouts: layouts(&widths), + has_node_data: next() & 1 != 0, + node_data_len_bytes: (next() % 7) as u8, + }; + check(&view); + } + } +} From d7b7ed2a54564b82fec5ab5bef7db7dbbbcc6424 Mon Sep 17 00:00:00 2001 From: Nittin Murthi Date: Tue, 10 Nov 2026 16:26:11 -0800 Subject: [PATCH 2/5] test(diskgraph): reduce repeated node-data test setup --- .../diskgraph/diskgraph-index/src/layout.rs | 47 +++++++------------ 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/packages/diskgraph/diskgraph-index/src/layout.rs b/packages/diskgraph/diskgraph-index/src/layout.rs index 9f417556c..e36adb104 100644 --- a/packages/diskgraph/diskgraph-index/src/layout.rs +++ b/packages/diskgraph/diskgraph-index/src/layout.rs @@ -562,7 +562,7 @@ mod pk_value_matches_tests { #[cfg(test)] mod node_data_tests { use super::*; - use std::io::ErrorKind; + use std::io::ErrorKind::{self, InvalidData, UnexpectedEof}; use std::sync::Arc; fn layouts(widths: &[usize]) -> Arc> { @@ -615,7 +615,7 @@ mod node_data_tests { if has_node_data { raw.resize(raw.len() + payload_len as usize, 29); } - let view = EdgeListView { + let mut view = EdgeListView { raw: &raw, layouts: layouts(&[1, 8]), has_node_data, @@ -629,20 +629,13 @@ mod node_data_tests { assert_eq!(view.node_data().unwrap(), expected); check(&view); for end in 0..raw.len() { - check(&EdgeListView { - raw: &raw[..end], - layouts: view.layouts.clone(), - has_node_data, - node_data_len_bytes: len_width as u8, - }); + view.raw = &raw[..end]; + check(&view); } - raw.push(99); - check(&EdgeListView { - raw: &raw, - layouts: layouts(&[1, 8]), - has_node_data, - node_data_len_bytes: len_width as u8, - }); + let mut trailing = raw.clone(); + trailing.push(99); + view.raw = &trailing; + check(&view); } } } @@ -653,37 +646,31 @@ mod node_data_tests { type Case<'a> = (&'a [u8], &'a [usize], bool, u8, Option); let cases: &[Case<'_>] = &[ (&[], &[8], false, 255, None), - (&[], &[], true, 2, Some(ErrorKind::InvalidData)), + (&[], &[], true, 2, Some(InvalidData)), (&[0], &[], true, 1, None), - (&[0, 0], &[8], true, 0, Some(ErrorKind::InvalidData)), - (&[0, 0], &[8], true, 5, Some(ErrorKind::InvalidData)), + (&[0, 0], &[8], true, 0, Some(InvalidData)), + (&[0, 0], &[8], true, 5, Some(InvalidData)), // Missing second count takes precedence over invalid length width. - (&[0], &[1, 8], true, 0, Some(ErrorKind::UnexpectedEof)), + (&[0], &[1, 8], true, 0, Some(UnexpectedEof)), // Missing node-data length takes precedence over edge-size overflow. - ( - &[255, 255, 255, 255], - &[8], - true, - 2, - Some(ErrorKind::UnexpectedEof), - ), + (&[255, 255, 255, 255], &[8], true, 2, Some(UnexpectedEof)), // Earlier segment EOF takes precedence over later count multiplication overflow. ( &[2, 255, 255, 255, 255, 0, 0], &[1, 8], true, 2, - Some(ErrorKind::UnexpectedEof), + Some(UnexpectedEof), ), ( &[255, 255, 255, 255, 0, 0], &[8], true, 2, - Some(ErrorKind::InvalidData), + Some(InvalidData), ), - (&[0, 0, 7], &[], true, 2, Some(ErrorKind::InvalidData)), - (&[7], &[], false, 2, Some(ErrorKind::InvalidData)), + (&[0, 0, 7], &[], true, 2, Some(InvalidData)), + (&[7], &[], false, 2, Some(InvalidData)), ]; for &(raw, widths, has_node_data, node_data_len_bytes, error) in cases { let view = EdgeListView { From 7cc8a91a7df0b7340675fb4367662b2ed8a64816 Mon Sep 17 00:00:00 2001 From: Nittin Murthi Date: Thu, 12 Nov 2026 13:15:48 -0800 Subject: [PATCH 3/5] refactor(diskgraph): share one allocation-free record walker --- .../diskgraph/diskgraph-index/src/layout.rs | 381 +++++------------- 1 file changed, 108 insertions(+), 273 deletions(-) diff --git a/packages/diskgraph/diskgraph-index/src/layout.rs b/packages/diskgraph/diskgraph-index/src/layout.rs index e36adb104..c7f45ee1c 100644 --- a/packages/diskgraph/diskgraph-index/src/layout.rs +++ b/packages/diskgraph/diskgraph-index/src/layout.rs @@ -87,74 +87,22 @@ impl<'a> EdgeListView<'a> { /// Returns the relation segments for this node. Each entry contains the /// edge count and raw slice for that relation's edges. pub fn relation_segments(&self) -> Result>, std::io::Error> { - Ok(self.parse()?.0) + let mut segments = Vec::with_capacity(self.layouts.len()); + let (edges, _) = self.walk_record(|layout, count, range| { + segments.push((layout.clone(), count, range)); + })?; + Ok(segments + .into_iter() + .map(|(layout, count, range)| RelationSegment { + layout, + count, + data: &edges[range], + }) + .collect()) } pub fn node_data(&self) -> Result, std::io::Error> { - if self.raw.is_empty() { - return if self.has_node_data { - Err(std::io::ErrorKind::InvalidData.into()) - } else { - Ok(None) - }; - } - - // Validate the entire count prefix before lengths or edge sizes, as - // parse() does. Replaying this small prefix avoids allocating counts - // and relation segments that a feature-only read would discard. - let mut cursor = 0usize; - for _ in self.layouts.iter() { - let (_, consumed) = decode_varint(&self.raw[cursor..])?; - cursor = cursor - .checked_add(consumed) - .ok_or(std::io::ErrorKind::InvalidData)?; - } - let node_data_len = if self.has_node_data { - let len_bytes = self.node_data_len_bytes as usize; - if len_bytes == 0 || len_bytes > 4 { - return Err(std::io::ErrorKind::InvalidData.into()); - } - if cursor + len_bytes > self.raw.len() { - return Err(std::io::ErrorKind::UnexpectedEof.into()); - } - let len = read_len_le(&self.raw[cursor..cursor + len_bytes]); - cursor += len_bytes; - Some(len) - } else { - None - }; - - let mut count_cursor = 0usize; - for layout in self.layouts.iter() { - let (count, consumed) = decode_varint(&self.raw[count_cursor..])?; - count_cursor += consumed; // Validated by the first pass. - let bytes = count - .checked_mul(layout.entry_bytes as u32) - .ok_or(std::io::ErrorKind::InvalidData)? as usize; - cursor = cursor - .checked_add(bytes) - .ok_or(std::io::ErrorKind::InvalidData)?; - if cursor > self.raw.len() { - return Err(std::io::ErrorKind::UnexpectedEof.into()); - } - } - if let Some(len) = node_data_len { - let end = cursor - .checked_add(len) - .ok_or(std::io::ErrorKind::InvalidData)?; - if end > self.raw.len() { - return Err(std::io::ErrorKind::UnexpectedEof.into()); - } - if end != self.raw.len() { - return Err(std::io::ErrorKind::InvalidData.into()); - } - Ok(Some(&self.raw[cursor..end])) - } else { - if cursor != self.raw.len() { - return Err(std::io::ErrorKind::InvalidData.into()); - } - Ok(None) - } + Ok(self.walk_record(|_, _, _| {})?.1) } pub fn pk_matches( @@ -188,85 +136,58 @@ impl<'a> EdgeListView<'a> { Ok(pk_value_matches(key, &value, &ctx.columns[col_idx])) } - fn parse(&self) -> Result<(Vec>, Option<&'a [u8]>), std::io::Error> { + /// Decode each count once, reporting segment ranges relative to the edge payload. + /// The caller may collect them; feature-only reads use an allocation-free no-op. + fn walk_record( + &self, + mut segment: impl FnMut(&RelationLayout, u32, std::ops::Range), + ) -> Result<(&'a [u8], Option<&'a [u8]>), std::io::Error> { + use std::io::ErrorKind::{InvalidData, UnexpectedEof}; if self.raw.is_empty() { if self.has_node_data { - return Err(std::io::ErrorKind::InvalidData.into()); + return Err(InvalidData.into()); + } + for layout in self.layouts.iter() { + segment(layout, 0, 0..0); } - // Node has no edges; return empty segments for each relation. - let segments = self - .layouts - .iter() - .map(|layout| RelationSegment { - layout: layout.clone(), - count: 0, - data: &[], - }) - .collect(); - return Ok((segments, None)); + return Ok((&[], None)); } - let mut segments = Vec::with_capacity(self.layouts.len()); - // First read all counts up front (prefix) to allow quick validation. let mut cursor = 0usize; - let mut counts = Vec::with_capacity(self.layouts.len()); + let mut edge_bytes = 0usize; for layout in self.layouts.iter() { let (count, consumed) = decode_varint(&self.raw[cursor..])?; - cursor = cursor - .checked_add(consumed) - .ok_or(std::io::ErrorKind::InvalidData)?; - counts.push((layout.clone(), count)); - } - let node_data_len = if self.has_node_data { - let len_bytes = self.node_data_len_bytes as usize; - if len_bytes == 0 || len_bytes > 4 { - return Err(std::io::ErrorKind::InvalidData.into()); - } - if cursor + len_bytes > self.raw.len() { - return Err(std::io::ErrorKind::UnexpectedEof.into()); - } - let len = read_len_le(&self.raw[cursor..cursor + len_bytes]); - cursor += len_bytes; - Some(len) - } else { - None - }; - // Now slice the data segments in the same order. - for (layout, count) in counts { + cursor += consumed; let bytes = count .checked_mul(layout.entry_bytes as u32) - .ok_or(std::io::ErrorKind::InvalidData)? as usize; - let end = cursor - .checked_add(bytes) - .ok_or(std::io::ErrorKind::InvalidData)?; - if end > self.raw.len() { - return Err(std::io::ErrorKind::UnexpectedEof.into()); - } - let data = &self.raw[cursor..end]; - segments.push(RelationSegment { - layout, - count, - data, - }); - cursor = end; + .ok_or(InvalidData)? as usize; + let end = edge_bytes.checked_add(bytes).ok_or(InvalidData)?; + segment(layout, count, edge_bytes..end); + edge_bytes = end; } - let node_data = if let Some(len) = node_data_len { - let end = cursor - .checked_add(len) - .ok_or(std::io::ErrorKind::InvalidData)?; - if end > self.raw.len() { - return Err(std::io::ErrorKind::UnexpectedEof.into()); - } - if end != self.raw.len() { - return Err(std::io::ErrorKind::InvalidData.into()); + let node_data_len = if self.has_node_data { + let width = self.node_data_len_bytes as usize; + if width == 0 || width > 4 { + return Err(InvalidData.into()); } - Some(&self.raw[cursor..end]) + let end = cursor.checked_add(width).ok_or(InvalidData)?; + let len = read_len_le(self.raw.get(cursor..end).ok_or(UnexpectedEof)?); + cursor = end; + len } else { - if cursor != self.raw.len() { - return Err(std::io::ErrorKind::InvalidData.into()); - } - None + 0 }; - Ok((segments, node_data)) + let edge_end = cursor.checked_add(edge_bytes).ok_or(InvalidData)?; + let end = edge_end.checked_add(node_data_len).ok_or(InvalidData)?; + if end > self.raw.len() { + return Err(UnexpectedEof.into()); + } + if end != self.raw.len() { + return Err(InvalidData.into()); + } + Ok(( + &self.raw[cursor..edge_end], + self.has_node_data.then_some(&self.raw[edge_end..end]), + )) } } @@ -560,153 +481,67 @@ mod pk_value_matches_tests { } #[cfg(test)] -mod node_data_tests { +mod record_walk_tests { use super::*; - use std::io::ErrorKind::{self, InvalidData, UnexpectedEof}; - use std::sync::Arc; - - fn layouts(widths: &[usize]) -> Arc> { - Arc::new( - widths - .iter() - .enumerate() - .map(|(i, &entry_bytes)| RelationLayout { - relation_id: i as u16, - dst_bits: 8, - dest_sort_key_bits: None, - entry_bytes, - dst_mask: 255, - dest_sort_key_mask: None, - }) - .collect(), - ) - } - - fn check(view: &EdgeListView<'_>) { - let expected = view.parse().map(|(_, data)| data).map_err(|e| e.kind()); - let actual = view.node_data().map_err(|e| e.kind()); - assert_eq!( - actual, expected, - "raw={:?}, length width={}", - view.raw, view.node_data_len_bytes - ); - if let Ok(Some(data)) = actual { - assert_eq!(data.as_ptr(), expected.unwrap().unwrap().as_ptr()); - } - } - - #[test] - fn node_data_matches_parse_for_valid_rows_and_all_truncations() { - for count_width in 1..=4 { - for len_width in 1..=4 { - for has_node_data in [false, true] { - // Non-minimal count widths are accepted by the existing parser. - let mut raw = Vec::new(); - for count in [2u32, 3] { - let encoded = count | (((count_width - 1) as u32) << (count_width * 8 - 2)); - raw.extend_from_slice(&encoded.to_be_bytes()[4 - count_width..]); - } - // Preserve the old one-byte length behavior (read_len_le returns 0). - let payload_len = if len_width == 1 { 0u32 } else { 7 }; - if has_node_data { - raw.extend_from_slice(&payload_len.to_le_bytes()[..len_width]); - } - raw.extend_from_slice(&[17; 2 + 3 * 8]); - if has_node_data { - raw.resize(raw.len() + payload_len as usize, 29); - } - let mut view = EdgeListView { - raw: &raw, - layouts: layouts(&[1, 8]), - has_node_data, - node_data_len_bytes: len_width as u8, - }; - let expected = if has_node_data { - Some(&raw[raw.len() - payload_len as usize..]) - } else { - None - }; - assert_eq!(view.node_data().unwrap(), expected); - check(&view); - for end in 0..raw.len() { - view.raw = &raw[..end]; - check(&view); - } - let mut trailing = raw.clone(); - trailing.push(99); - view.raw = &trailing; - check(&view); - } - } - } - } #[test] - fn node_data_preserves_error_precedence_and_empty_rows() { - type Case<'a> = (&'a [u8], &'a [usize], bool, u8, Option); - let cases: &[Case<'_>] = &[ - (&[], &[8], false, 255, None), - (&[], &[], true, 2, Some(InvalidData)), - (&[0], &[], true, 1, None), - (&[0, 0], &[8], true, 0, Some(InvalidData)), - (&[0, 0], &[8], true, 5, Some(InvalidData)), - // Missing second count takes precedence over invalid length width. - (&[0], &[1, 8], true, 0, Some(UnexpectedEof)), - // Missing node-data length takes precedence over edge-size overflow. - (&[255, 255, 255, 255], &[8], true, 2, Some(UnexpectedEof)), - // Earlier segment EOF takes precedence over later count multiplication overflow. - ( - &[2, 255, 255, 255, 255, 0, 0], - &[1, 8], - true, - 2, - Some(UnexpectedEof), + fn segments_and_features_share_record_validation() { + let mut view = EdgeListView { + raw: &[], + layouts: std::sync::Arc::new( + [1, 2] + .into_iter() + .enumerate() + .map(|(i, entry_bytes)| RelationLayout { + relation_id: i as u16, + dst_bits: 8, + dest_sort_key_bits: None, + entry_bytes, + dst_mask: 255, + dest_sort_key_mask: None, + }) + .collect(), ), - ( - &[255, 255, 255, 255, 0, 0], - &[8], - true, - 2, - Some(InvalidData), - ), - (&[0, 0, 7], &[], true, 2, Some(InvalidData)), - (&[7], &[], false, 2, Some(InvalidData)), - ]; - for &(raw, widths, has_node_data, node_data_len_bytes, error) in cases { - let view = EdgeListView { - raw, - layouts: layouts(widths), - has_node_data, - node_data_len_bytes, - }; - check(&view); - assert_eq!(view.node_data().err().map(|e| e.kind()), error); - } - } - - #[test] - fn node_data_matches_parse_for_arbitrary_buffers() { - let mut seed = 42u64; - let mut next = || { - seed ^= seed << 13; - seed ^= seed >> 7; - seed ^= seed << 17; - seed + has_node_data: true, + node_data_len_bytes: 2, }; - for _ in 0..20_000 { - let len = (next() % 128) as usize; - let raw: Vec<_> = (0..len).map(|_| next() as u8).collect(); - let relation_count = (next() % 9) as usize; - let widths: Vec<_> = (0..relation_count) - .map(|_| [0, 1, 8, 64, u32::MAX as usize, usize::MAX][(next() % 6) as usize]) - .collect(); - let view = EdgeListView { - raw: &raw, - layouts: layouts(&widths), - has_node_data: next() & 1 != 0, - node_data_len_bytes: (next() % 7) as u8, - }; - check(&view); + // Two counts, feature length, two relation payloads, then feature bytes. + let raw = [2, 1, 3, 0, 10, 11, 20, 21, 30, 31, 32]; + view.raw = &raw; + let segments = view.relation_segments().unwrap(); + assert_eq!((segments[0].count, segments[0].data), (2, &raw[4..6])); + assert_eq!((segments[1].count, segments[1].data), (1, &raw[6..8])); + let data = view.node_data().unwrap().unwrap(); + assert_eq!(data, &raw[8..]); + assert_eq!(data.as_ptr(), raw[8..].as_ptr()); + for end in 0..raw.len() { + view.raw = &raw[..end]; + assert!(view.relation_segments().is_err()); + assert!(view.node_data().is_err()); + } + for raw in [ + &[2, 1, 0, 0, 10, 11, 20, 21, 99][..], + &[255, 255, 255, 255, 0, 0, 0][..], + ] { + view.raw = raw; + assert!(view.relation_segments().is_err()); + assert!(view.node_data().is_err()); + } + view.raw = &raw; + for width in [0, 5] { + view.node_data_len_bytes = width; + assert!(view.node_data().is_err()); + assert!(view.relation_segments().is_err()); } + view.node_data_len_bytes = 2; + view.raw = &[]; + view.has_node_data = false; + assert_eq!(view.node_data().unwrap(), None); + let segments = view.relation_segments().unwrap(); + assert_eq!(segments.len(), 2); + assert!(segments.iter().all(|s| s.count == 0 && s.data.is_empty())); + view.raw = &[2, 1, 10, 11, 20, 21]; + assert_eq!(view.node_data().unwrap(), None); + assert_eq!(view.relation_segments().unwrap()[1].data, &[20, 21]); } } From 0bd4f8d9d4be436c3b187c7a22787d7f0b82b73e Mon Sep 17 00:00:00 2001 From: Nittin Murthi Date: Thu, 12 Nov 2026 16:54:26 -0800 Subject: [PATCH 4/5] perf(diskgraph): parse fused sampling records once --- .../diskgraph/diskgraph-engine/src/engine.rs | 43 +++++++++---------- .../diskgraph-engine/src/tests_unit.rs | 30 ++++++++++++- .../diskgraph/diskgraph-index/src/layout.rs | 15 +++++-- 3 files changed, 60 insertions(+), 28 deletions(-) diff --git a/packages/diskgraph/diskgraph-engine/src/engine.rs b/packages/diskgraph/diskgraph-engine/src/engine.rs index fa4deee73..66d8f099e 100644 --- a/packages/diskgraph/diskgraph-engine/src/engine.rs +++ b/packages/diskgraph/diskgraph-engine/src/engine.rs @@ -12,7 +12,7 @@ use diskgraph_core::sampling::*; use diskgraph_core::sortkey::SortKeyDecoder; use diskgraph_core::tt_builder::{Value, WEIGHT_COL}; use diskgraph_core::time::{abs_from_unit, Seconds, TimeUnit}; -use diskgraph_index::layout::{EdgeListView, RelationSegment}; +use diskgraph_index::layout::RelationSegment; use diskgraph_index::reader::ShardReader; use gxhash::HashMap as GxHashMap; use rayon::prelude::*; @@ -2290,13 +2290,13 @@ impl DiskGraphEngine { Ok(()) } - /// Feature fetching from an EdgeListView, used in fused mode - fn accumulate_features_from_view( + /// Accumulate feature bytes from the record already parsed for fused sampling. + fn accumulate_features_from_data( &self, type_id: usize, type_state: &mut crate::state::TypeState, scratch: &mut RowPackDecodeScratch, - edges_view: &EdgeListView, + node_data: Option<&[u8]>, output: &mut impl OutputHandler, ) -> Result<()> { let decoder = self @@ -2305,27 +2305,22 @@ impl DiskGraphEngine { .and_then(|d| d.as_ref()) .ok_or_else(|| EngineError::Internal(format!("no decoder for type {}", type_id)))?; - self.accumulate_features_from_view_internal( - type_id, type_state, scratch, decoder, edges_view, output, + self.accumulate_features_from_data_internal( + type_id, type_state, scratch, decoder, node_data, output, ) } - /// Reused for fused mode feature fetching for both non-leaf and leaf nodes. - /// Non-leaf nodes EdgeListView is accessed as part of sampling. Leaf nodes - /// requires a separate access to EdgeListView given their IDs. - fn accumulate_features_from_view_internal( + /// Decode feature bytes for both frontier and leaf nodes in fused mode. + fn accumulate_features_from_data_internal( &self, type_id: usize, type_state: &mut crate::state::TypeState, scratch: &mut RowPackDecodeScratch, decoder: &RowPackDecoder, - edges_view: &EdgeListView, + node_data: Option<&[u8]>, output: &mut impl OutputHandler, ) -> Result<()> { - if let Some(data) = edges_view - .node_data() - .map_err(|e| EngineError::DataCorruption(e.to_string()))? - { + if let Some(data) = node_data { let context = decoder.context(); // Drop reserved columns (e.g. WEIGHT) from features via a per-column // mask; `flush_fused_features` projects the flush schema to the same @@ -2399,12 +2394,14 @@ impl DiskGraphEngine { .edges_for_node(local_id) .map_err(|e| EngineError::DataCorruption(e.to_string()))?; - self.accumulate_features_from_view_internal( + self.accumulate_features_from_data_internal( type_id, type_state, scratch, decoder, - &edges_view, + edges_view + .node_data() + .map_err(|e| EngineError::DataCorruption(e.to_string()))?, output, )?; } @@ -2557,7 +2554,7 @@ impl DiskGraphEngine { })?; // Reserved columns (WEIGHT) were dropped from the builders/mask in - // `accumulate_features_from_view_internal`, so project the schema to + // `accumulate_features_from_data_internal`, so project the schema to // the same non-reserved columns to keep field count == array count. let keep: Vec = full_schema .fields() @@ -2904,21 +2901,23 @@ impl DiskGraphEngine { .edges_for_node(local_id) .map_err(|e| EngineError::DataCorruption(e.to_string()))?; + let (segments, node_data) = edges_view + .parse() + .map_err(|e| EngineError::DataCorruption(e.to_string()))?; + // FETCH SOURCE FEATURES (Locality) { let scratch = &mut state.decode_scratch; let type_state = state.type_states.get_mut(&src_type_id).unwrap(); - self.accumulate_features_from_view( + self.accumulate_features_from_data( src_type_id, type_state, scratch, - &edges_view, + node_data, output, )?; } - let segments = edges_view.relation_segments().unwrap_or_default(); - self.process_node_relations( layer_idx, src_type_id, diff --git a/packages/diskgraph/diskgraph-engine/src/tests_unit.rs b/packages/diskgraph/diskgraph-engine/src/tests_unit.rs index 12f474989..20aa86c36 100644 --- a/packages/diskgraph/diskgraph-engine/src/tests_unit.rs +++ b/packages/diskgraph/diskgraph-engine/src/tests_unit.rs @@ -412,8 +412,7 @@ fn test_fused_sampling() { }], }; - let req = BatchSamplingRequest { - + let mut req = BatchSamplingRequest { dataset_id: DatasetId::nil(), seed_nodes: [("User".to_string(), vec![0])].into(), seed_timestamps: None, @@ -443,6 +442,33 @@ fn test_fused_sampling() { // Batch should contain seed (0) and its unique neighbors (1..20) = 21 rows assert_eq!(batch.num_rows(), 21); + + // Compare the combined record read for the source and feature-only reads + // for its leaf neighbors with standalone sampling plus feature fetching. + req.flags.fused_feature_fetching = false; + let mut standalone_state = SamplingState::default(); + let mut standalone = MockOutput { + edges: vec![], + feature_batches: vec![], + schema: None, + fused_batches: vec![], + }; + graph + .sampler + .sample(&req, &mut standalone_state, &mut standalone) + .unwrap(); + assert_eq!(output.edges, standalone.edges); + let ids: Vec<_> = standalone_state.type_states[&0] + .nodes + .iter() + .map(|node| node.global_id) + .collect(); + graph + .sampler + .fetch_features_for_type(0, &ids, &[], &mut standalone) + .unwrap(); + assert_eq!(standalone.feature_batches.len(), 1); + assert_eq!(batch, &standalone.feature_batches[0]); } #[test] diff --git a/packages/diskgraph/diskgraph-index/src/layout.rs b/packages/diskgraph/diskgraph-index/src/layout.rs index c7f45ee1c..406a1516e 100644 --- a/packages/diskgraph/diskgraph-index/src/layout.rs +++ b/packages/diskgraph/diskgraph-index/src/layout.rs @@ -87,18 +87,24 @@ impl<'a> EdgeListView<'a> { /// Returns the relation segments for this node. Each entry contains the /// edge count and raw slice for that relation's edges. pub fn relation_segments(&self) -> Result>, std::io::Error> { + Ok(self.parse()?.0) + } + + /// Return relation segments and feature bytes with a single record walk. + pub fn parse(&self) -> Result<(Vec>, Option<&'a [u8]>), std::io::Error> { let mut segments = Vec::with_capacity(self.layouts.len()); - let (edges, _) = self.walk_record(|layout, count, range| { + let (edges, node_data) = self.walk_record(|layout, count, range| { segments.push((layout.clone(), count, range)); })?; - Ok(segments + let segments = segments .into_iter() .map(|(layout, count, range)| RelationSegment { layout, count, data: &edges[range], }) - .collect()) + .collect(); + Ok((segments, node_data)) } pub fn node_data(&self) -> Result, std::io::Error> { @@ -508,7 +514,8 @@ mod record_walk_tests { // Two counts, feature length, two relation payloads, then feature bytes. let raw = [2, 1, 3, 0, 10, 11, 20, 21, 30, 31, 32]; view.raw = &raw; - let segments = view.relation_segments().unwrap(); + let (segments, combined_data) = view.parse().unwrap(); + assert_eq!(combined_data, Some(&raw[8..])); assert_eq!((segments[0].count, segments[0].data), (2, &raw[4..6])); assert_eq!((segments[1].count, segments[1].data), (1, &raw[6..8])); let data = view.node_data().unwrap().unwrap(); From a2e2259f9acc0939c171c631b26bc004c79bbeac Mon Sep 17 00:00:00 2001 From: Nittin Murthi Date: Thu, 12 Nov 2026 17:09:43 -0800 Subject: [PATCH 5/5] refactor(diskgraph): use one feature accumulation helper --- .../diskgraph/diskgraph-engine/src/engine.rs | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) diff --git a/packages/diskgraph/diskgraph-engine/src/engine.rs b/packages/diskgraph/diskgraph-engine/src/engine.rs index 66d8f099e..b0ff23c20 100644 --- a/packages/diskgraph/diskgraph-engine/src/engine.rs +++ b/packages/diskgraph/diskgraph-engine/src/engine.rs @@ -2290,28 +2290,8 @@ impl DiskGraphEngine { Ok(()) } - /// Accumulate feature bytes from the record already parsed for fused sampling. - fn accumulate_features_from_data( - &self, - type_id: usize, - type_state: &mut crate::state::TypeState, - scratch: &mut RowPackDecodeScratch, - node_data: Option<&[u8]>, - output: &mut impl OutputHandler, - ) -> Result<()> { - let decoder = self - .node_decoders - .get(type_id) - .and_then(|d| d.as_ref()) - .ok_or_else(|| EngineError::Internal(format!("no decoder for type {}", type_id)))?; - - self.accumulate_features_from_data_internal( - type_id, type_state, scratch, decoder, node_data, output, - ) - } - /// Decode feature bytes for both frontier and leaf nodes in fused mode. - fn accumulate_features_from_data_internal( + fn accumulate_features_from_data( &self, type_id: usize, type_state: &mut crate::state::TypeState, @@ -2394,7 +2374,7 @@ impl DiskGraphEngine { .edges_for_node(local_id) .map_err(|e| EngineError::DataCorruption(e.to_string()))?; - self.accumulate_features_from_data_internal( + self.accumulate_features_from_data( type_id, type_state, scratch, @@ -2554,7 +2534,7 @@ impl DiskGraphEngine { })?; // Reserved columns (WEIGHT) were dropped from the builders/mask in - // `accumulate_features_from_data_internal`, so project the schema to + // `accumulate_features_from_data`, so project the schema to // the same non-reserved columns to keep field count == array count. let keep: Vec = full_schema .fields() @@ -2891,6 +2871,14 @@ impl DiskGraphEngine { continue; } + let decoder = self + .node_decoders + .get(src_type_id) + .and_then(|d| d.as_ref()) + .ok_or_else(|| { + EngineError::Internal(format!("no decoder for type {}", src_type_id)) + })?; + for node in &frontier { let shard_idx = self.find_shard(shards, node.global_id)?; let shard = &shards[shard_idx]; @@ -2913,6 +2901,7 @@ impl DiskGraphEngine { src_type_id, type_state, scratch, + decoder, node_data, output, )?;