Skip to content

ext/dom: Add Dom\XMLSchema to reuse a compiled XML Schema. - #25437

Draft
ArtUkrainskiy wants to merge 1 commit into
php:masterfrom
ArtUkrainskiy:dom-xml-schema
Draft

ArtUkrainskiy wants to merge 1 commit into
php:masterfrom
ArtUkrainskiy:dom-xml-schema

Conversation

@ArtUkrainskiy

Copy link
Copy Markdown
Contributor

This is a proof of concept. I'd like feedback on the API before writing an RFC.

schemaValidate() compiles the XSD on every call, validates, and frees the schema. For a real schema with imports, that compile is almost the whole cost. Apps that check many invoices or orders against one schema pay it on every document.

This adds Dom\XMLSchema, which owns the compiled xmlSchemaPtr:

$schema = Dom\XMLSchema::createFromFile('/app/schemas/invoice.xsd'); // once per worker
$schema->validate($doc); // Dom\XMLDocument|DOMDocument, fresh validation context per call

schemaValidate() now uses the same compile and validate helpers. Its behaviour doesn't change.

The test schema is UBL 2.1 Invoice, an OASIS standard for e-invoices. Its main file imports shared type definitions, so compiling it reads 14 XSD files, 2.8 MB in total. The schema and the 1.2 KB and 20 KB invoices are the official ones from https://docs.oasis-open.org/ubl/os-UBL-2.1/UBL-2.1.zip. The larger invoices are the 20 KB example with its 5 item lines copied over and over until the invoice has 100, 1,000 or 10,000 lines. They still pass the schema.

Time to load one document and validate it:

document load + schemaValidate() load + Dom\XMLSchema::validate()
UBL Invoice, 1.2 KB 50.8 ms 0.034 ms
UBL Invoice, 20 KB 51.0 ms 0.31 ms
UBL Invoice, 200 KB 54.2 ms 3.7 ms
UBL Invoice, 1.9 MB 88.9 ms 37.2 ms
UBL Invoice, 18.7 MB 421 ms 367 ms

Execution time, median of 9 alternating runs. Under callgrind one 20 KB validation is 433M instructions with schemaValidate() and 1.07M with validate(). schemaValidate() itself stays within noise after the refactor, at −0.06% instructions.

Tests for ext/dom, ext/libxml and ext/xmlreader pass, and valgrind is clean on the new tests.

A few things I'm not sure about.

  • On a broken schema it emits libxml warnings and then throws. Should it throw one exception that carries the errors instead?
  • createFromString() has no base URI, so relative imports resolve against the CWD, same as schemaValidateSource(). Should it take a ?string $baseUri?
  • Should it accept DOMDocument, or only Dom\XMLDocument?

RelaxNG and XMLReader::setSchema() could follow the same pattern later.

I haven't tried other compilers, other real-world schemas or an ASan build yet.

Dom\XMLSchema::createFromFile()/createFromString() compile the schema once,
validate() checks a Dom\XMLDocument or DOMDocument with a fresh validation
context. schemaValidate() now shares the compile and validate helpers.
@ArtUkrainskiy

Copy link
Copy Markdown
Contributor Author

@devnexen you're the code owner for ext/dom, so I'd love your take before I write an RFC. Mostly the three API questions in the description. Does a separate Dom\XMLSchema class look like the right direction to you?

@devnexen

Copy link
Copy Markdown
Member

I think it s a viable feature.

A few things I'm not sure about.

On a broken schema it emits libxml warnings and then throws. Should it throw one exception that carries the errors instead?

yes

createFromString() has no base URI, so relative imports resolve against the CWD, same as schemaValidateSource(). Should it take a ?string $baseUri?

yes

Should it accept DOMDocument, or only Dom\XMLDocument?

and yes.

See you in the RFC ;)

Comment thread ext/dom/document.c
docp = (xmlDocPtr) dom_object_get_node(intern);
bool is_valid = dom_schema_validate_doc(sptr, docp, flags);
xmlSchemaFree(sptr);
if (EG(exception)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need this check, the VM/JIT handles return values in presence of exceptions properly already

Comment thread ext/dom/document.c

static inline dom_xml_schema_object *dom_xml_schema_from_obj(zend_object *obj)
{
return (dom_xml_schema_object *) ((char *) obj - offsetof(dom_xml_schema_object, std));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ZEND_CONTAINER_OF

Comment thread ext/dom/document.c
}
/* }}} end dom_document_relaxNG_validate_xml */

/* Dom\XMLSchema: a compiled XML Schema that validates many documents. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible, moving this to a new file would be cleaner

Comment thread ext/dom/document.c
ZEND_ASSERT(schema->schema != NULL);

bool is_valid = dom_schema_validate_doc(schema->schema, docp, flags);
if (EG(exception)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto about exceptions&return values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants