Precompiled ShEx schemas
rudof can compile a ShEx schema to its internal representation (SchemaIR) once, and reuse the result for many validations. This avoids the cost of parsing and compilation on each run. This guide explains what the precompiled cache is, when to use it, and how to use it from the CLI.
Why precompile a schema?
Every rudof shex-validate invocation does two things before it can
touch a single triple of data:
- Parses the ShEx source (ShExC, ShExJ, Turtle, …) into an AST.
- Compiles the AST into
SchemaIR, the shape of the schema that the validator actually runs against.
For small schemas, both steps take almost no time. For large schemas (hundreds of shapes, many imports, deep inheritance), the compilation takes time. It is the same work each time you validate with the same schema. The precompiled cache does this work once, and later runs skip it.
The compile - validate loop
1. Compile the schema
Use the shex command with --compile-to:
rudof shex --schema examples/user.shex --compile-to user.ircache
rudof loads the schema as it does for validation, and writes the SchemaIR to user.ircache. The file is binary. It contains:
- The magic bytes
RSIR. - The version of the file envelope.
- A postcard header with a length prefix: the body format, the rudof version and the negation-cycle flag.
- The
SchemaIRbody, in postcard encoding.
The file is not text. Do not open it in editors that can change line endings or encoding.
You can also compile as a side-effect of a validation run:
rudof shex-validate \
--schema examples/user.shex \
--compile-to user.ircache \
--shapemap examples/user.sm \
examples/user.ttl
2. Validate against the cache
Point shex-validate at the cache with --compiled-schema instead
of --schema:
rudof shex-validate \
--compiled-schema user.ircache \
--shapemap examples/user.sm \
examples/user.ttl
3. Inspect a compiled cache with shex
shex also accepts --compiled-schema, mirroring shex-validate. This
is useful to inspect a cache produced elsewhere without needing the
original schema source file:
rudof shex --compiled-schema user.ircache -r internal --statistics true --show-dependencies true
This loads the SchemaIR straight from the cache (skipping parsing,
imports and AST-to-IR compilation) and prints its internal
representation, shape statistics, and dependency graph.
With a compiled schema, only -r internal works as a result format, together with the statistics and dependencies, which read the same SchemaIR. The other result formats (shexc, shexj, json, jsonld, turtle, plantuml and others) need the parsed schema (the AST). The precompiled cache keeps only the SchemaIR, so these formats fail with an error that names the limitation. To convert a schema to one of these formats, load the original source with --schema instead of --compiled-schema.
4. The unified alternative: binary as a ShExFormat
All of this also works without --compile-to and --compiled-schema. binary is a normal ShExFormat value, valid wherever rudof accepts a ShEx format. So the usual -s, -f, -r and -o options also compile and load the cache:
# Compile: same effect as --compile-to.
rudof shex -s examples/user.shex -r binary -o user.ircache
# Load the cache back: same effect as --compiled-schema.
rudof shex -f binary -s user.ircache -r internal --statistics true
# shex-validate accepts -f binary the same way.
rudof shex-validate -s user.ircache -f binary --shapemap examples/user.sm examples/user.ttl
Both forms give the same cache, byte for byte, and behave in the same way. --compile-to and --compiled-schema are shortcuts for ShExFormat::Binary, which are clearer in scripts. Use the form that fits your command. The limitation to -r internal applies with both forms.
Other Considerations
- The cache is bound to a version. Loading a cache produced by an incompatible version fails with an error message.
- Strict-mode compiles are already verified. If you compile the schema with
--reader-mode strict, rudof runs the negation-cycle check at compile time and records the result in the header. A load in strict mode does not run the check again. - No source schema options.
--compiled-schemaconflicts with--schema,--schema-formatand--base-schema. The cache already holds a fully resolved schema, so these options have nothing to do. - No external-shape resolvers.
--compiled-schemaconflicts with--external-resolver. rudof replaces someEXTERNALshape declarations during the compilation from AST to IR, so a resolver at validation time is too late. If your schema usesEXTERNALshapes, give the resolvers at compile time. - Semantic actions. The cache does not contain the implementations of semantic actions. If your schema uses a
SemActIRI, register the extension for that IRI in the rudof process that loads the cache.
Full end-to-end example
Starting from the examples/
folder in the repo:
# 1. Compile the schema once.
rudof shex --schema examples/user.shex --compile-to user.ircache
# 2. Validate any number of data sets against the same cache.
rudof shex-validate \
--compiled-schema user.ircache \
--shapemap examples/user.sm \
examples/user.ttl
rudof shex-validate \
--compiled-schema user.ircache \
--shapemap examples/user.sm \
examples/user_fail.ttl