Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to release the GVL while gumbo parsing, for better concurrency #2964

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
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
47 changes: 46 additions & 1 deletion ext/nokogiri/gumbo.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ rb_utf8_str_new_static(const char *str, long length)
#include <nokogiri.h>
#include <libxml/tree.h>
#include <libxml/HTMLtree.h>
#include <ruby/thread.h>

// URI = system id
// external id = public id
Expand All @@ -86,16 +87,36 @@ get_parent(xmlNodePtr node)
return node->parent;
}

struct gumbo_parse_args {
const GumboOptions *options;
VALUE input;
};

static void *
nogvl_gumbo_parse_with_options(void *ptr)
{
struct gumbo_parse_args *gpa = ptr;
return (void *)gumbo_parse_with_options(gpa->options, RSTRING_PTR(gpa->input), RSTRING_LEN(gpa->input));
}

static GumboOutput *
perform_parse(const GumboOptions *options, VALUE input)
{
assert(RTEST(input));
Check_Type(input, T_STRING);
/*
GumboOutput *output = gumbo_parse_with_options(
options,
RSTRING_PTR(input),
RSTRING_LEN(input)
);
*/
struct gumbo_parse_args gpa;
gpa.options = options;
gpa.input = input;
GumboOutput *output = rb_thread_call_without_gvl(
nogvl_gumbo_parse_with_options, &gpa,
RUBY_UBF_IO, 0);

const char *status_string = gumbo_status_to_string(output->status);
switch (output->status) {
Expand Down Expand Up @@ -140,7 +161,7 @@ set_line(xmlNodePtr node, size_t line)
// Construct an XML tree rooted at xml_output_node from the Gumbo tree rooted
// at gumbo_node.
static void
build_tree(
build_tree0(
xmlDocPtr doc,
xmlNodePtr xml_output_node,
const GumboNode *gumbo_node
Expand Down Expand Up @@ -256,6 +277,30 @@ build_tree(
}
}

struct build_tree_args {
xmlDocPtr doc;
xmlNodePtr xml_output_node;
const GumboNode *gumbo_node;
};

static void *
nogvl_build_tree(void *ptr)
{
struct build_tree_args *bta = ptr;
build_tree0(bta->doc, bta->xml_output_node, bta->gumbo_node);
}

static void
build_tree(xmlDocPtr doc, xmlNodePtr xml_output_node, const GumboNode *gumbo_node)
{
//return build_tree0(doc, xml_output_node, gumbo_node);
struct build_tree_args bt;
bt.doc = doc;
bt.xml_output_node = xml_output_node;
bt.gumbo_node = gumbo_node;
rb_thread_call_without_gvl(nogvl_build_tree, &bt, RUBY_UBF_IO, 0);
}

static void
add_errors(const GumboOutput *output, VALUE rdoc, VALUE input, VALUE url)
{
Expand Down