Skip to content

Commit

Permalink
chore(lint): remove manual AST field counter (#27449)
Browse files Browse the repository at this point in the history
Addresses the review feedback in
#27416 .

- Hoist the buffer max size variable to make it less confusing
- Remove manual AST field counter in favour of an explicit "commit
schema" step which writes the actual field count.
  • Loading branch information
marvinhagemeister authored and dsherret committed Jan 9, 2025
1 parent 68287ab commit 4d95005
Show file tree
Hide file tree
Showing 3 changed files with 354 additions and 217 deletions.
45 changes: 31 additions & 14 deletions cli/tools/lint/ast_buffer/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ impl StringTable {
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NodeRef(pub usize);

/// Represents an offset to a node whose schema hasn't been committed yet
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PendingNodeRef(pub NodeRef);

#[derive(Debug)]
pub struct BoolPos(pub usize);
#[derive(Debug)]
Expand Down Expand Up @@ -152,20 +156,16 @@ where
K: Into<u8> + Display,
P: Into<u8> + Display,
{
fn header(
&mut self,
kind: K,
parent: NodeRef,
span: &Span,
prop_count: usize,
) -> NodeRef;
fn header(&mut self, kind: K, parent: NodeRef, span: &Span)
-> PendingNodeRef;
fn ref_field(&mut self, prop: P) -> FieldPos;
fn ref_vec_field(&mut self, prop: P, len: usize) -> FieldArrPos;
fn str_field(&mut self, prop: P) -> StrPos;
fn bool_field(&mut self, prop: P) -> BoolPos;
fn undefined_field(&mut self, prop: P) -> UndefPos;
#[allow(dead_code)]
fn null_field(&mut self, prop: P) -> NullPos;
fn commit_schema(&mut self, offset: PendingNodeRef) -> NodeRef;

fn write_ref(&mut self, pos: FieldPos, value: NodeRef);
fn write_maybe_ref(&mut self, pos: FieldPos, value: Option<NodeRef>);
Expand All @@ -183,6 +183,7 @@ pub struct SerializeCtx {
str_table: StringTable,
kind_map: Vec<usize>,
prop_map: Vec<usize>,
field_count: u8,
}

/// This is the internal context used to allocate and fill the buffer. The point
Expand All @@ -200,8 +201,9 @@ impl SerializeCtx {
start_buf: NodeRef(0),
buf: vec![],
str_table: StringTable::new(),
kind_map: vec![0; kind_size + 1],
prop_map: vec![0; prop_size + 1],
kind_map: vec![0; kind_size],
prop_map: vec![0; prop_size],
field_count: 0,
};

let empty_str = ctx.str_table.insert("");
Expand Down Expand Up @@ -232,6 +234,8 @@ impl SerializeCtx {
where
P: Into<u8> + Display + Clone,
{
self.field_count += 1;

let offset = self.buf.len();

let n: u8 = prop.clone().into();
Expand Down Expand Up @@ -268,7 +272,7 @@ impl SerializeCtx {
parent: NodeRef,
span: &Span,
prop_count: usize,
) -> NodeRef {
) -> PendingNodeRef {
let offset = self.buf.len();

// Node type fits in a u8
Expand All @@ -285,7 +289,19 @@ impl SerializeCtx {
debug_assert!(prop_count < 10);
self.buf.push(prop_count as u8);

NodeRef(offset)
PendingNodeRef(NodeRef(offset))
}

pub fn commit_schema(&mut self, node_ref: PendingNodeRef) -> NodeRef {
let mut offset = node_ref.0 .0;

// type + parentId + span lo + span hi
offset += 1 + 4 + 4 + 4;

self.buf[offset] = self.field_count;
self.field_count = 0;

node_ref.0
}

/// Allocate the node header. It's always the same for every node.
Expand All @@ -299,8 +315,7 @@ impl SerializeCtx {
kind: N,
parent: NodeRef,
span: &Span,
prop_count: usize,
) -> NodeRef
) -> PendingNodeRef
where
N: Into<u8> + Display + Clone,
{
Expand All @@ -313,7 +328,9 @@ impl SerializeCtx {
}
}

self.append_node(n, parent, span, prop_count)
// Prop count will be filled with the actual value when the
// schema is committed.
self.append_node(n, parent, span, 0)
}

/// Allocate a reference property that will hold the offset of
Expand Down
Loading

0 comments on commit 4d95005

Please sign in to comment.