Lightpanda: the headless browser designed for AI and automation
perf: access parsenode directly from sink
Karl Seguin6285e939b854
diff --git a/src/browser/parser/Parser.zig b/src/browser/parser/Parser.zigindex 93b9c7adb..e9778bb49 100644--- a/src/browser/parser/Parser.zig+++ b/src/browser/parser/Parser.zig@@ -34,13 +34,13 @@ const Allocator = std.mem.Allocator;const CHECKPOINT_INTERVAL = 1024;const TERMINATE_CHECK_INTERVAL = 1024;-pub const ParsedNode = struct {+// Mirrored in src/rust/html5ever/sink+pub const ParsedNode = extern struct {node: *Node,// Data associated with this element to be passed back to html5ever as needed// We only have this for Elements. For other types, like comments, it's null.- // html5ever should never ask us for this data on a non-element, and we'll- // assert that, with this optional, to make sure our assumption is correct.+ // html5ever should never ask us for this data on a non-element.data: ?*anyopaque,// Set once html5ever has put the node in the tree or given it children.@@ -229,7 +229,6 @@ pub fn parse(self: *Parser, html: []const u8) void {&self.container,self,createElementCallback,- getDataCallback,appendCallback,parseErrorCallback,popCallback,@@ -260,7 +259,6 @@ pub fn parseWithEncoding(self: *Parser, html: []const u8, charset: []const u8) v&self.container,self,createElementCallback,- getDataCallback,appendCallback,parseErrorCallback,popCallback,@@ -288,7 +286,6 @@ pub fn parseXML(self: *Parser, xml: []const u8) void {&self.container,self,createXMLElementCallback,- getDataCallback,appendCallback,xmlParseErrorCallback,popCallback,@@ -324,7 +321,6 @@ pub fn parseFragment(self: *Parser, html: []const u8) void {self,createElementCallback,createContextElementCallback,- getDataCallback,appendCallback,parseErrorCallback,popCallback,@@ -380,7 +376,6 @@ pub const Streaming = struct {&self.parser.container,&self.parser,createElementCallback,- getDataCallback,appendCallback,parseErrorCallback,popCallback,@@ -717,14 +712,6 @@ fn _attachDeclarativeShadowCallback(self: *Parser, host_node: *Node, template_noreturn 1;}-fn getDataCallback(ctx: *anyopaque) callconv(.c) *anyopaque {- const pn: *ParsedNode = @ptrCast(@alignCast(ctx));- // For non-elements, data is null. But, we expect this to only ever- // be called for elements.- lp.assert(pn.data != null, "Parser.getDataCallback null data", .{});- return pn.data.?;-}-fn appendCallback(ctx: *anyopaque, parent_ref: *anyopaque, node_or_text: h5e.NodeOrText) callconv(.c) void {const self: *Parser = @ptrCast(@alignCast(ctx));if (self.pollTerminate()) {diff --git a/src/browser/parser/html5ever.zig b/src/browser/parser/html5ever.zigindex 4ccbe73b0..797faf0e1 100644--- a/src/browser/parser/html5ever.zig+++ b/src/browser/parser/html5ever.zig@@ -24,7 +24,6 @@ pub extern "c" fn html5ever_parse_document(doc: *anyopaque,ctx: *anyopaque,createElementCallback: *const fn (ctx: *anyopaque, data: *anyopaque, QualName, AttributeIterator) callconv(.c) ?*anyopaque,- elemNameCallback: *const fn (node_ref: *anyopaque) callconv(.c) *anyopaque,appendCallback: *const fn (ctx: *anyopaque, parent_ref: *anyopaque, NodeOrText) callconv(.c) void,parseErrorCallback: *const fn (ctx: *anyopaque, StringSlice) callconv(.c) void,popCallback: *const fn (ctx: *anyopaque, node_ref: *anyopaque) callconv(.c) void,@@ -50,7 +49,6 @@ pub extern "c" fn html5ever_parse_document_with_encoding(doc: *anyopaque,ctx: *anyopaque,createElementCallback: *const fn (ctx: *anyopaque, data: *anyopaque, QualName, AttributeIterator) callconv(.c) ?*anyopaque,- elemNameCallback: *const fn (node_ref: *anyopaque) callconv(.c) *anyopaque,appendCallback: *const fn (ctx: *anyopaque, parent_ref: *anyopaque, NodeOrText) callconv(.c) void,parseErrorCallback: *const fn (ctx: *anyopaque, StringSlice) callconv(.c) void,popCallback: *const fn (ctx: *anyopaque, node_ref: *anyopaque) callconv(.c) void,@@ -76,7 +74,6 @@ pub extern "c" fn html5ever_parse_fragment(ctx: *anyopaque,createElementCallback: *const fn (ctx: *anyopaque, data: *anyopaque, QualName, AttributeIterator) callconv(.c) ?*anyopaque,createContextElementCallback: *const fn (ctx: *anyopaque, data: *anyopaque, QualName, AttributeIterator) callconv(.c) ?*anyopaque,- elemNameCallback: *const fn (node_ref: *anyopaque) callconv(.c) *anyopaque,appendCallback: *const fn (ctx: *anyopaque, parent_ref: *anyopaque, NodeOrText) callconv(.c) void,parseErrorCallback: *const fn (ctx: *anyopaque, StringSlice) callconv(.c) void,popCallback: *const fn (ctx: *anyopaque, node_ref: *anyopaque) callconv(.c) void,@@ -101,7 +98,6 @@ pub extern "c" fn html5ever_streaming_parser_create(doc: *anyopaque,ctx: *anyopaque,createElementCallback: *const fn (ctx: *anyopaque, data: *anyopaque, QualName, AttributeIterator) callconv(.c) ?*anyopaque,- elemNameCallback: *const fn (node_ref: *anyopaque) callconv(.c) *anyopaque,appendCallback: *const fn (ctx: *anyopaque, parent_ref: *anyopaque, NodeOrText) callconv(.c) void,parseErrorCallback: *const fn (ctx: *anyopaque, StringSlice) callconv(.c) void,popCallback: *const fn (ctx: *anyopaque, node_ref: *anyopaque) callconv(.c) void,@@ -211,7 +207,6 @@ pub extern "c" fn xml5ever_parse_document(doc: *anyopaque,ctx: *anyopaque,createElementCallback: *const fn (ctx: *anyopaque, data: *anyopaque, QualName, AttributeIterator) callconv(.c) ?*anyopaque,- elemNameCallback: *const fn (node_ref: *anyopaque) callconv(.c) *anyopaque,appendCallback: *const fn (ctx: *anyopaque, parent_ref: *anyopaque, NodeOrText) callconv(.c) void,parseErrorCallback: *const fn (ctx: *anyopaque, StringSlice) callconv(.c) void,popCallback: *const fn (ctx: *anyopaque, node_ref: *anyopaque) callconv(.c) void,diff --git a/src/rust/html5ever/lib.rs b/src/rust/html5ever/lib.rsindex a0ee8df1c..bb9bbd8e2 100644--- a/src/rust/html5ever/lib.rs+++ b/src/rust/html5ever/lib.rs@@ -38,7 +38,6 @@ pub extern "C" fn html5ever_parse_document(document: Ref,ctx: Ref,create_element_callback: CreateElementCallback,- get_data_callback: GetDataCallback,append_callback: AppendCallback,parse_error_callback: ParseErrorCallback,pop_callback: PopCallback,@@ -67,7 +66,6 @@ pub extern "C" fn html5ever_parse_document(quirks_mode: Cell::new(QuirksMode::NoQuirks),pop_callback: pop_callback,append_callback: append_callback,- get_data_callback: get_data_callback,parse_error_callback: parse_error_callback,create_element_callback: create_element_callback,create_comment_callback: create_comment_callback,@@ -101,7 +99,6 @@ pub extern "C" fn html5ever_parse_document_with_encoding(document: Ref,ctx: Ref,create_element_callback: CreateElementCallback,- get_data_callback: GetDataCallback,append_callback: AppendCallback,parse_error_callback: ParseErrorCallback,pop_callback: PopCallback,@@ -141,7 +138,6 @@ pub extern "C" fn html5ever_parse_document_with_encoding(quirks_mode: Cell::new(QuirksMode::NoQuirks),pop_callback: pop_callback,append_callback: append_callback,- get_data_callback: get_data_callback,parse_error_callback: parse_error_callback,create_element_callback: create_element_callback,create_comment_callback: create_comment_callback,@@ -465,7 +461,6 @@ pub extern "C" fn html5ever_parse_fragment(ctx: Ref,create_element_callback: CreateElementCallback,create_context_element_callback: CreateElementCallback,- get_data_callback: GetDataCallback,append_callback: AppendCallback,parse_error_callback: ParseErrorCallback,pop_callback: PopCallback,@@ -494,7 +489,6 @@ pub extern "C" fn html5ever_parse_fragment(quirks_mode: Cell::new(QuirksMode::NoQuirks),pop_callback: pop_callback,append_callback: append_callback,- get_data_callback: get_data_callback,parse_error_callback: parse_error_callback,create_element_callback: create_element_callback,create_comment_callback: create_comment_callback,@@ -606,7 +600,6 @@ pub extern "C" fn html5ever_streaming_parser_create(document: Ref,ctx: Ref,create_element_callback: CreateElementCallback,- get_data_callback: GetDataCallback,append_callback: AppendCallback,parse_error_callback: ParseErrorCallback,pop_callback: PopCallback,@@ -637,7 +630,6 @@ pub extern "C" fn html5ever_streaming_parser_create(quirks_mode: Cell::new(QuirksMode::NoQuirks),pop_callback: pop_callback,append_callback: append_callback,- get_data_callback: get_data_callback,parse_error_callback: parse_error_callback,create_element_callback: create_element_callback,create_comment_callback: create_comment_callback,@@ -739,7 +731,6 @@ pub extern "C" fn xml5ever_parse_document(document: Ref,ctx: Ref,create_element_callback: CreateElementCallback,- get_data_callback: GetDataCallback,append_callback: AppendCallback,parse_error_callback: ParseErrorCallback,pop_callback: PopCallback,@@ -768,7 +759,6 @@ pub extern "C" fn xml5ever_parse_document(quirks_mode: Cell::new(QuirksMode::NoQuirks),pop_callback: pop_callback,append_callback: append_callback,- get_data_callback: get_data_callback,parse_error_callback: parse_error_callback,create_element_callback: create_element_callback,create_comment_callback: create_comment_callback,diff --git a/src/rust/html5ever/sink.rs b/src/rust/html5ever/sink.rsindex 957ed6744..746be9de7 100644--- a/src/rust/html5ever/sink.rs+++ b/src/rust/html5ever/sink.rs@@ -43,6 +43,21 @@ impl ElementData {}}+// Mirrors Parser.ParsedNode (an extern struct). Every element Ref the tree+// builder hands back is one of these, created by create_element_callback.+#[repr(C)]+struct ParsedNode {+ _node: *const c_void,+ data: *const ElementData,+ _placed: bool,+}++fn element_data<'a>(target: Ref) -> &'a ElementData {+ let data = unsafe { (*(target as *const ParsedNode)).data };+ debug_assert!(!data.is_null(), "element data requested for a non-element");+ return unsafe { &*data };+}+pub struct Sink<'arena> {pub ctx: Ref,pub document: Ref,@@ -50,7 +65,6 @@ pub struct Sink<'arena> {pub quirks_mode: Cell<QuirksMode>,pub pop_callback: PopCallback,pub append_callback: AppendCallback,- pub get_data_callback: GetDataCallback,pub parse_error_callback: ParseErrorCallback,pub create_element_callback: CreateElementCallback,pub create_comment_callback: CreateCommentCallback,@@ -103,9 +117,7 @@ impl<'arena> TreeSink for Sink<'arena> {}fn elem_name(&self, target: &Ref) -> Self::ElemName<'_> {- let opaque = unsafe { (self.get_data_callback)(*target) };- let data = opaque as *mut ElementData;- return unsafe { &(*data).qname };+ return &element_data(*target).qname;}fn get_template_contents(&self, target: &Ref) -> Ref {@@ -115,9 +127,7 @@ impl<'arena> TreeSink for Sink<'arena> {}fn is_mathml_annotation_xml_integration_point(&self, target: &Ref) -> bool {- let opaque = unsafe { (self.get_data_callback)(*target) };- let data = opaque as *mut ElementData;- return unsafe { (*data).mathml_annotation_xml_integration_point };+ return element_data(*target).mathml_annotation_xml_integration_point;}fn pop(&self, node: &Ref) {diff --git a/src/rust/html5ever/types.rs b/src/rust/html5ever/types.rsindex e18c08cf0..93b7c9af2 100644--- a/src/rust/html5ever/types.rs+++ b/src/rust/html5ever/types.rs@@ -39,8 +39,6 @@ pub type AppendDoctypeToDocumentCallback = unsafe extern "C" fn(pub type CreateProcessingInstruction =unsafe extern "C" fn(ctx: Ref, target: StringSlice, data: StringSlice) -> Ref;-pub type GetDataCallback = unsafe extern "C" fn(ctx: Ref) -> *mut c_void;-pub type AppendCallback =unsafe extern "C" fn(ctx: Ref, parent: Ref, node_or_text: CNodeOrText) -> ();