[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"docs:adr\u002F0008-c-library-experimental":3},{"versions":4,"version":29,"nav":32,"path":124,"page":129},[5,10,13,17,20,23,26],{"name":6,"label":7,"prefix":8,"channel":9},"1.3","1.3.0 (latest)","","latest",{"name":11,"label":12,"prefix":11,"channel":11},"next","next (main)",{"name":14,"label":15,"prefix":14,"channel":16},"1.2","1.2.1","old",{"name":18,"label":19,"prefix":18,"channel":16},"1.1","1.1.1",{"name":21,"label":22,"prefix":21,"channel":16},"1.0","1.0.0",{"name":24,"label":25,"prefix":24,"channel":16},"0.2","0.2.0",{"name":27,"label":28,"prefix":27,"channel":16},"0.1","0.1.0",{"name":6,"label":7,"prefix":8,"channel":9,"base":30,"ref":31},"\u002Fdocs","v1.3.0",[33,37,40,43,46,49,52,55,58,61,64,68,71,74,77,80,83,86,89,93,96,99,102,105,108,111,114,117,120,123,126],{"slug":34,"title":35,"group":36},"index","Overview","Guide",{"slug":38,"title":39,"group":36},"getting-started","Getting started",{"slug":41,"title":42,"group":36},"php","PHP API",{"slug":44,"title":45,"group":36},"laravel","Laravel",{"slug":47,"title":48,"group":36},"resources","Resources",{"slug":50,"title":51,"group":36},"deployment","Deployment",{"slug":53,"title":54,"group":36},"troubleshooting","Troubleshooting",{"slug":56,"title":57,"group":36},"from-wkhtmltopdf","Coming from wkhtmltopdf",{"slug":59,"title":60,"group":36},"stability","Stability and upgrades",{"slug":62,"title":63,"group":36},"ai-assistants","Docs for AI assistants",{"slug":65,"title":66,"group":67},"css","CSS reference","Reference",{"slug":69,"title":70,"group":67},"paged-media","Paged media",{"slug":72,"title":73,"group":67},"fonts","Fonts",{"slug":75,"title":76,"group":67},"extensions","Extensions",{"slug":78,"title":79,"group":67},"compatibility","Compatibility",{"slug":81,"title":82,"group":67},"pdf","The PDF",{"slug":84,"title":85,"group":67},"cli","Command line",{"slug":87,"title":88,"group":67},"rust","Rust library",{"slug":90,"title":91,"group":92},"ffi","C library (experimental)","Internals",{"slug":94,"title":95,"group":92},"protocol","Worker protocol",{"slug":97,"title":98,"group":92},"releasing","Releasing",{"slug":100,"title":101,"group":92},"adr","Architecture decisions",{"slug":103,"title":104,"group":92,"parent":101},"adr\u002F0001-record-architecture-decisions","1. Record architecture decisions",{"slug":106,"title":107,"group":92,"parent":101},"adr\u002F0002-rust-and-fragmentation-in-layout","2. Rust core with fragmentation inside layout",{"slug":109,"title":110,"group":92,"parent":101},"adr\u002F0003-css-parsing-and-own-selector-matching","3. lightningcss for parsing, own selector matching and cascade",{"slug":112,"title":113,"group":92,"parent":101},"adr\u002F0004-extensions-only-where-css-has-no-way","4. Extensions only where CSS has no way",{"slug":115,"title":116,"group":92,"parent":101},"adr\u002F0005-public-api-and-deprecations","5. Public API, semantic versioning and deprecations",{"slug":118,"title":119,"group":92,"parent":101},"adr\u002F0006-pages-that-grow-with-their-content","6. Pages that grow with their content",{"slug":121,"title":122,"group":92,"parent":101},"adr\u002F0007-layers-for-screen-and-print","7. Layers for screen and print, and form fields as an option",{"slug":124,"title":125,"group":92,"parent":101},"adr\u002F0008-c-library-experimental","8. An experimental C library beside the worker",{"slug":127,"title":128,"group":92,"parent":101},"adr\u002F0009-resources-in-batches","9. Resources in batches, fetching stays with the caller",{"title":125,"seoTitle":130,"seoDescription":131,"html":132,"headings":133,"editUrl":144,"sourceUrl":145,"markdownUrl":146,"prev":147,"next":148},"Architecture decisions: 8. An experimental C library beside the worker · Fagus docs","The PHP package runs Fagus as a worker process: it starts fagus worker with the first document, sends requests through a pipe in the frames of the worker protocol and keeps the process for the next documents.","\u003Ch1 id=\"8-an-experimental-c-library-beside-the-worker\">8. An experimental C library beside the worker\u003C\u002Fh1>\n\u003Cul>\n\u003Cli>Status: accepted\u003C\u002Fli>\n\u003Cli>Date: 2026-11-20\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Ch2 id=\"context\">Context\u003Ca class=\"f-doc-anchor\" href=\"#context\" aria-label=\"Link to this section\">#\u003C\u002Fa>\u003C\u002Fh2>\n\u003Cp>The PHP package runs Fagus as a worker process: it starts \u003Ccode>fagus worker\u003C\u002Fcode> with the first document,\nsends requests through a pipe in the frames of the \u003Ca href=\"\u002Fdocs\u002Fprotocol\">worker protocol\u003C\u002Fa> and keeps\nthe process for the next documents. \u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fnforst\u002Ffagus\u002Fissues\u002F3\">Issue #3\u003C\u002Fa> asks for a\nC shared library instead (a \u003Ccode>cdylib\u003C\u002Fcode> with something like \u003Ccode>fagus_render\u003C\u002Fcode> and \u003Ccode>fagus_free\u003C\u002Fcode>), so that\nPHP calls Fagus in its own process through \u003Ca href=\"https:\u002F\u002Fwww.php.net\u002Fmanual\u002Fen\u002Fbook.ffi.php\">FFI\u003C\u002Fa>,\nwithout the extra process, the pipe and the framing, and so that other languages can use it the\nsame way.\u003C\u002Fp>\n\u003Cp>What the worker costs was measured on 2026-11-20 on an Apple M2 Pro (macOS 27, PHP 8.5, a busy\nmachine), with the invoices of \u003Ccode>tools\u002Fgen.py\u003C\u002Fcode>, in Rust, from PHP through the package and its\nworker, and from PHP through a C library built for the purpose with the same request\n(\u003Ca href=\"https:\u002F\u002Fgithub.com\u002Fnforst\u002Ffagus\u002Fblob\u002Fv1.3.0\u002Fbench\u002FREADME.md#the-worker-and-the-c-library\">bench\u002FREADME.md\u003C\u002Fa>), in milliseconds:\u003C\u002Fp>\n\u003Ctable class=\"f-doc-table f-stack-table\">\n\u003Cthead>\n\u003Ctr>\n\u003Cth>Invoice\u003C\u002Fth>\n\u003Cth style=\"text-align:right\">Rust, in-process\u003C\u002Fth>\n\u003Cth style=\"text-align:right\">PHP, worker\u003C\u002Fth>\n\u003Cth style=\"text-align:right\">PHP, library\u003C\u002Fth>\n\u003Cth style=\"text-align:right\">New worker and its first document\u003C\u002Fth>\n\u003Cth style=\"text-align:right\">New library engine and its first document\u003C\u002Fth>\n\u003C\u002Ftr>\n\u003C\u002Fthead>\n\u003Ctbody>\n\u003Ctr>\n\u003Ctd>1 page\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"Rust, in-process\">3.08\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"PHP, worker\">3.12\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"PHP, library\">3.12\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"New worker and its first document\">11.1\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"New library engine and its first document\">5.7\u003C\u002Ftd>\n\u003C\u002Ftr>\n\u003Ctr>\n\u003Ctd>10 pages\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"Rust, in-process\">20.6\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"PHP, worker\">21.0\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"PHP, library\">21.6\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"New worker and its first document\">32.0\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"New library engine and its first document\">26.3\u003C\u002Ftd>\n\u003C\u002Ftr>\n\u003Ctr>\n\u003Ctd>100 pages\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"Rust, in-process\">213\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"PHP, worker\">214\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"PHP, library\">225\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"New worker and its first document\">234\u003C\u002Ftd>\n\u003Ctd style=\"text-align:right\" data-label=\"New library engine and its first document\">239\u003C\u002Ftd>\n\u003C\u002Ftr>\n\u003C\u002Ftbody>\n\u003C\u002Ftable>\n\u003Cul>\n\u003Cli>Once the worker runs, the process, the pipe and the framing cost 0.04 ms for 1 page, 0.3 ms\nfor 10 and 1.4 ms for 100 (0.6 to 1.6 %) against the render in Rust. That is all a library could\nsave per document, and from PHP it did not: it was as fast at 1 page and 3 to 5 % slower at 10\nand 100 pages, in every round (from a C program it was within the noise of the binary in three\nruns, so this seems to be about rendering inside PHP's process; it was not looked into\nfurther).\u003C\u002Fli>\n\u003Cli>What a library does save is the start of the worker, about 5.5 ms in each PHP-FPM request that\nrenders, where the worker lives as long as the request. In queue workers, under Octane and in\nlong-running scripts the worker starts once.\u003C\u002Fli>\n\u003Cli>Memory: one process instead of two saves 3 to 7 MB of a peak of 23 MB (1 page) or 133 MB (100\npages). The engine's memory moves into the PHP process.\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>So the overhead the issue names is real but small; the arguments for a library are the start of\nthe worker under PHP-FPM, setups where PHP may not start processes, and languages that would\nrather call a function than speak the protocol.\u003C\u002Fp>\n\u003Cp>The worker gives things that a library in the caller's process cannot:\u003C\u002Fp>\n\u003Cul>\n\u003Cli>\u003Cstrong>Crash isolation.\u003C\u002Fstrong> Panics are caught in both: the engine catches them around every document.\nBut what ends a process (a stack overflow in a deep document, an allocation that fails, on\nwhich Rust aborts, a fault in the unsafe code of a dependency such as an image decoder or the\ncompression) ends the worker, and the PHP package answers with an \u003Ccode>EngineException\u003C\u002Fcode> and starts\na new one. In-process it ends the PHP-FPM worker, or the Octane or queue worker, with the\nrequest and whatever else it was doing.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>The sandbox.\u003C\u002Fstrong> On Linux, \u003Ccode>fagus worker\u003C\u002Fcode> gives up the network, starting programs and writing\nfiles (Landlock, seccomp) and can be held to some directories (\u003Ccode>--allow-read\u003C\u002Fcode>). A library\ncannot do that to the process that loads it, which needs all of these.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Memory.\u003C\u002Fstrong> The worker hands freed memory back after each document on Linux (\u003Ccode>malloc_trim\u003C\u002Fcode>),\nand the PHP package replaces a worker that has grown (\u003Ccode>maxMemory\u003C\u002Fcode>, \u003Ccode>maxDocuments\u003C\u002Fcode>). In-process,\nwhat the allocator keeps after a large document stays in the PHP process until it ends\n(\u003Ccode>pm.max_requests\u003C\u002Fcode>). \u003Ccode>--memory-limit\u003C\u002Fcode> and \u003Ccode>--cpu-limit\u003C\u002Fcode> limit a worker, not PHP. The memory\nbudget and the time limit of each document work in both, since the engine checks them itself.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Threads.\u003C\u002Fstrong> The engine renders on a thread of its own with a large stack and compresses the\nPDF on two more. In-process these are threads in every PHP process that renders, and threads do\nnot survive a \u003Ccode>fork()\u003C\u002Fcode>: PHP-FPM forks its workers from the process that runs the preload script.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Callbacks.\u003C\u002Fstrong> The protocol lets the worker ask its caller for resources (\u003Ccode>?\u003C\u002Fcode>), signatures\n(\u003Ccode>S\u003C\u002Fcode>), timestamps (\u003Ccode>T\u003C\u002Fcode>) and revocation data (\u003Ccode>V\u003C\u002Fcode>) while it renders. Through FFI these would be\nC function pointers into PHP closures, called on the engine's threads; PHP, outside its\nthread-safe builds, cannot run PHP code on another thread.\u003C\u002Fli>\n\u003Cli>\u003Cstrong>Version skew.\u003C\u002Fstrong> The PHP package installs the binary of its own release and checks the\nprotocol and the version in the worker's hello. A library is whatever file the caller loads.\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Cp>On the PHP side, PHP's default \u003Ccode>ffi.enable=preload\u003C\u002Fcode> allows \u003Ccode>FFI::cdef()\u003C\u002Fcode> on the command line only.\nIn web requests the declarations must come from the preload script (\u003Ccode>opcache.preload\u003C\u002Fcode> with\n\u003Ccode>FFI::load()\u003C\u002Fcode>, taken with \u003Ccode>FFI::scope()\u003C\u002Fcode>), or \u003Ccode>ffi.enable=1\u003C\u002Fcode> allows FFI everywhere, which PHP\nadvises against. Shared hosting, where a library would help most, seldom lets users set either,\nand the FFI extension is not always there: the official Docker images build it only on request\n(\u003Ccode>docker-php-ext-install ffi\u003C\u002Fcode>). Hosts that forbid \u003Ccode>proc_open()\u003C\u002Fcode> tend to forbid FFI as well. The\nissue's author is on Laravel 9; the package needs PHP 8.3 and Laravel 13\n(\u003Ca href=\"\u002Fdocs\u002Fadr\u002F0005-public-api-and-deprecations\">ADR 5\u003C\u002Fa>), and a library would let older PHP call Fagus\ndirectly, without the package.\u003C\u002Fp>\n\u003Cp>On the Rust side, every library crate forbids \u003Ccode>unsafe\u003C\u002Fcode> (\u003Ccode>#![forbid(unsafe_code)]\u003C\u002Fcode>) and the\nworkspace denies it; the one exception is the call of \u003Ccode>malloc_trim\u003C\u002Fcode> in\n\u003Ccode>crates\u002Ffagus-cli\u002Fsrc\u002Fmemory.rs\u003C\u002Fcode>. A C interface cannot be written without unsafe code: exported\nsymbols (\u003Ccode>#[unsafe(no_mangle)]\u003C\u002Fcode>), raw pointers from the caller, memory handed out and given back.\u003C\u002Fp>\n\u003Cp>A C interface would also be public API in the sense of ADR 5: the signatures of its functions,\nthe layout of whatever is passed by value, who frees what. Once 1.0 promises stability, every\nmistake in it stays for the whole major.\u003C\u002Fp>\n\u003Ch2 id=\"decision\">Decision\u003Ca class=\"f-doc-anchor\" href=\"#decision\" aria-label=\"Link to this section\">#\u003C\u002Fa>\u003C\u002Fh2>\n\u003Cul>\n\u003Cli>Fagus offers a C library from 1.0.0 on, \u003Cstrong>experimental\u003C\u002Fstrong>: the crate \u003Ccode>crates\u002Ffagus-ffi\u003C\u002Fcode>, a shared\nlibrary (\u003Ccode>cdylib\u003C\u002Fcode>) and a static one (\u003Ccode>staticlib\u003C\u002Fcode>) with the header\n\u003Ccode>crates\u002Ffagus-ffi\u002Finclude\u002Ffagus.h\u003C\u002Fcode>, written by hand and checked against the code by a test. It\nis not covered by ADR 5 and the 1.x stability promise: \u003Ccode>fagus_abi()\u003C\u002Fcode> returns 0, and any\nrelease may change it. A later record makes it stable (and \u003Ccode>fagus_abi()\u003C\u002Fcode> 1), if people use it\nand its shape holds up.\u003C\u002Fli>\n\u003Cli>The release workflow builds the shared library for Linux (x64 and arm64, glibc 2.17), macOS\n(x64 and arm64) and Windows (x64), as \u003Ccode>fagus-ffi-&lt;target&gt;.tar.gz\u003C\u002Fcode> with \u003Ccode>fagus.h\u003C\u002Fcode> and the\nlicenses, with an SBOM, a sum in \u003Ccode>SHA256SUMS\u003C\u002Fcode> and the attestations of the binaries. Not for\nmusl: its static targets make no shared library, and there it is built from source with\n\u003Ccode>-crt-static\u003C\u002Fcode> off.\u003C\u002Fli>\n\u003Cli>The PHP package does not use it. The worker stays the way the package runs Fagus: warm, the two\nrender as fast, and the worker's isolation, sandbox and memory handling are worth more than the\nstart it costs under PHP-FPM, which a queue worker or Octane avoids.\u003C\u002Fli>\n\u003Cli>The interface takes the render request of the worker protocol: the JSON header of \u003Ccode>R\u003C\u002Fcode> and its\ndata blocks one after another, and answers with the header of \u003Ccode>P\u003C\u002Fcode> or \u003Ccode>E\u003C\u002Fcode> as JSON and the PDF.\nThe options are the protocol's, kept in one place: \u003Ccode>request.rs\u003C\u002Fcode> of \u003Ccode>fagus-cli\u003C\u002Fcode> turns a request\ninto a document and the result into an answer, for the worker and, included as it is, for the\nlibrary (as the fuzz target includes the worker). A new protocol field reaches both at once.\nThe engine takes the options of \u003Ccode>fagus worker\u003C\u002Fcode>, as JSON; one it does not know is refused.\u003C\u002Fli>\n\u003Cli>The surface is small and opaque: \u003Ccode>fagus_version\u003C\u002Fcode>, \u003Ccode>fagus_abi\u003C\u002Fcode>, \u003Ccode>fagus_protocol\u003C\u002Fcode>,\n\u003Ccode>fagus_engine_new\u003C\u002Fcode>, \u003Ccode>fagus_engine_free\u003C\u002Fcode>, \u003Ccode>fagus_render\u003C\u002Fcode>, \u003Ccode>fagus_result_ok\u003C\u002Fcode>,\n\u003Ccode>fagus_result_header\u003C\u002Fcode>, \u003Ccode>fagus_result_pdf\u003C\u002Fcode>, \u003Ccode>fagus_result_free\u003C\u002Fcode>, \u003Ccode>fagus_string_free\u003C\u002Fcode>. Engines and\nanswers are opaque pointers read through functions, never structs whose layout would be API.\nWhat the library hands out goes back to it through the function for it. Every function checks\nfor null and catches panics (\u003Ccode>catch_unwind\u003C\u002Fcode>), so that none unwinds into C; a render never\nreturns null, only an answer that is an error.\u003C\u002Fli>\n\u003Cli>No callbacks in this version. \u003Ccode>ask\u003C\u002Fcode>, and signatures with \u003Ccode>certificates\u003C\u002Fcode>, \u003Ccode>timestamp\u003C\u002Fcode> or\n\u003Ccode>revocation\u003C\u002Fcode>, are answered with an error. Resources come with the request (\u003Ccode>resources\u003C\u002Fcode>, \u003Ccode>data:\u003C\u002Fcode>\nURLs) or from the files of \u003Ccode>base\u003C\u002Fcode> and \u003Ccode>root\u003C\u002Fcode>; signatures with a PKCS#12 file work.\u003C\u002Fli>\n\u003Cli>The unsafe code is in one file, \u003Ccode>crates\u002Ffagus-ffi\u002Fsrc\u002Fabi.rs\u003C\u002Fcode>, allowed there with a reason. The\ncrate denies it everywhere else, every other crate keeps forbidding it, and each unsafe block\nsays what makes it sound. A crate of its own keeps the exception out of the engine, which stays\nfree of unsafe code for every user of the \u003Ccode>fagus\u003C\u002Fcode> crate.\u003C\u002Fli>\n\u003Cli>The library starts its thread pool with the first document, not with the first engine, so that\nan engine made before a \u003Ccode>fork()\u003C\u002Fcode> does not leave the children a pool without threads.\u003C\u002Fli>\n\u003Cli>Tests call the interface through raw pointers from Rust and build and run a C program against\nthe header; \u003Ccode>tools\u002Fffi.php\u003C\u002Fcode> calls every function through PHP's FFI where PHP has it, which a CI\njob requires. The release workflow runs the C program against each library its runners can\nrun.\u003C\u002Fli>\n\u003C\u002Ful>\n\u003Ch2 id=\"consequences\">Consequences\u003Ca class=\"f-doc-anchor\" href=\"#consequences\" aria-label=\"Link to this section\">#\u003C\u002Fa>\u003C\u002Fh2>\n\u003Cp>People who want Fagus in-process, from PHP or from another language, can download it and try it,\nand what they report decides what it becomes. Under PHP-FPM it saves the start of the worker in\neach request; nothing else measured favours it.\u003C\u002Fp>\n\u003Cp>The unsafe code of the workspace grows from one call to one file, which a change to \u003Ccode>abi.rs\u003C\u002Fcode>\ngets read for with care. The protocol code of the worker is shared now: the worker implements\nits \u003Ccode>Caller\u003C\u002Fcode> with its connection, the library has none.\u003C\u002Fp>\n\u003Cp>Each release carries five more archives, about the size of the binary each (28 MB for the\nlibrary on macOS arm64). The static library (220 MB on macOS, every dependency in full) is not\nreleased; C programs that want it build it.\u003C\u002Fp>\n\u003Cp>What would make it more than experimental: users who build on it; callbacks a caller can answer\nsafely, which needs the engine to render on the thread that called \u003Ccode>fagus_render\u003C\u002Fcode> so that a C\nfunction pointer is called there; a file of declarations that PHP's \u003Ccode>FFI::load()\u003C\u002Fcode> reads (it takes\nno \u003Ccode>#include\u003C\u002Fcode> or \u003Ccode>#ifndef\u003C\u002Fcode>); and, if the PHP package should use it, an opt-in engine with the same\n\u003Ccode>Document\u003C\u002Fcode> and \u003Ccode>Pdf\u003C\u002Fcode>, which would make the interface API of the package and needs its own record.\u003C\u002Fp>\n",[134,138,141],{"id":135,"text":136,"level":137},"context","Context",2,{"id":139,"text":140,"level":137},"decision","Decision",{"id":142,"text":143,"level":137},"consequences","Consequences","https:\u002F\u002Fgithub.com\u002Fnforst\u002Ffagus\u002Fedit\u002Fmain\u002Fdocs\u002Fadr\u002F0008-c-library-experimental.md","https:\u002F\u002Fgithub.com\u002Fnforst\u002Ffagus\u002Fblob\u002Fv1.3.0\u002Fdocs\u002Fadr\u002F0008-c-library-experimental.md","\u002Fdocs\u002Fadr\u002F0008-c-library-experimental.md",{"slug":121,"title":122},{"slug":127,"title":128}]