Mar 05 Chapter 42: Cooperative Fibers in FFI As you likely know, Perl 5 is fundamentally single threaded. While threads.pm allows for concurrency, it creates "heavy" threads that clone the entire...
Mar 05 Chapter 41: Performance Optimization with Memory Arenas Frequent calls to malloc() and free() (or Perl's new and garbage collection) can become a significant bottleneck in performance-critical code. Each al...
Mar 05 Chapter 40: FFI in a Multithreaded System Perl's threads.pm implements "heavy" threads, where each thread is a complete clone of the interpreter. While this model provides excellent isolation,...
Jan 20 C++ Exception Unwinding I've put it off for months but now have unit tests that sorta kinda demonstrates it. [Edit: I have no roadmap or timeline but] I'll need to come back...
Jan 11 Chapter 39: Absolute Power with Assembly and Perl C is often called "portable assembly," but sometimes you need the real thing....
Jan 11 Chapter 38: Scientific Speed with Fortran and Perl If you are doing heavy scientific computing, you will eventually encounter Fortran (BLAS, LAPACK)....
Jan 11 Chapter 37: Oxidizing Perl (Binding to Rust) Perl is great for glue; Rust is great for logic....
Jan 11 Chapter 36: Unlocking the C/C++ Ecosystem with Affix::Wrap and Alien::Xrepo In previous chapters, we compiled code directly or used Alien::Base to find system libraries. But sometimes you want a library that isn't installed on...
Jan 08 Chapter 35: Packaging an Affix::Build Module for CPAN with Module::Build In the previous chapter, we built a script that compiles its own dependencies. To share this on CPAN, we want a standard module structure....
Jan 07 Chapter 34: The Zero-Dependency Image Processor For our next trick, we'll tackle the most common headache in the C ecosystem: Dependency Hell. Usually, if you want to manipulate images, you need lib...
Jan 04 Chapter 33: SIMD Vectors for Number Crunching Standard Perl scalars are designed for flexibility, not raw math throughput. When you need to process millions of coordinates, pixels, or audio sample...
Jan 04 Chapter 32: SIMD and Matrix Benchmarks So, you read Chapters 22 and 23, but the ultimate question remains: "Is it fast?"...
Jan 04 Chapter 31: Wrapping C++ Classes by Hacking Vtables In Chapter 30, we wrapped C++ classes using extern "C" helper functions in a shim. That is the 'Right Way.' But what if you are stuck with a pre-compi...
Jan 04 Chapter 30: Wrapping C++ Classes with C Shims C++ libraries are notoriously difficult for FFI systems to bind. Unlike C, which uses standard symbol names, C++ "mangles" function names (for example...
Jan 04 Chapter 29: An Affix Module Factory While runtime wrapping is convenient, it has a startup cost (parsing headers every time). For a distributable CPAN module, you want the parsing to hap...
Jan 04 Chapter 28: Instant Runtime Wrappers The fastest way to use Affix::Wrap is Runtime Wrapping. In this workflow, you parse the headers and bind the functions immediately when your script st...
Jan 03 Chapter 27: Automated Introspection with Affix::Wrap Writing affix signatures manually is great for control, but it becomes tedious when wrapping a library with hundreds of functions and structs. Instead...
Dec 30 Chapter 26: The "Kitchen Sink" Polyglot Library Sometimes, the best tool for the job involves three different tools. You might have legacy math models in Fortran, performance-critical loops in Assem...
Dec 30 Chapter 25: The Instant Assembly/C#/D/Fortran/Go/Rust/Zig Library C might be the lingua franca of system programming, but Affix::Build is a true polyglot. We've covered building shared libraries in C and C++ but Affi...
Dec 29 Chapter 24: The Instant C++ Library Throughout this cookbook, you've seen me use Affix::Build to generate shared libraries on the fly from C. However, building shared libraries isn't a c...
Dec 29 Chapter 21: Perl Callbacks in C Structs (vtables) In C, OOP is often simulated using structs containing function pointers. This pattern is commonly known as a vtable and allows a library to call diffe...
Dec 29 Chapter 20: Automatic Resource Management (RAII) Manual free() is error-prone. We can use Perl's DESTROY phase to automate it....
Dec 29 Chapter 19: Troubleshooting & Debugging When things go wrong in Perl, you probably get an error message. When they go wrong in C, you get a segfault, a frozen terminal, or data corruption th...
Dec 29 Chapter 18: Error Handling System calls fail. In C, you check the global errno variable (or GetLastError() on Windows). In Affix, we expose this via errno()....
Dec 29 Chapter 17: Advanced File Handling Bridging I/O between languages is historically one of the most fragile aspects of FFI. While handling file descriptors manually as opaque pointers may...
Dec 28 Chapter 16: Direct Marshalling Trampolines Speed is the reason I wrote infix and Affix and I think I've achieved a good balance of features vs. speed. I've done a lot of work to reduce overhead...
Dec 27 Chapter 15: Smart Enums C headers are full of enum definitions. To the C compiler, these are just integers. To the programmer, they have semantic meaning. When binding these...
Dec 27 Chapter 14: Callbacks, Context, & Closures In Chapter 12, we passed a simple callback. But real-world C libraries often take a callback and an opaque pointer (usually something like void userd...
Dec 27 Chapter 13: Transparent Structs Sometimes you need to peek inside the oven. If a function expects a C style struct to be passed by value or if you want to read struct members without...
Dec 26 Chapter 12: Callbacks C libraries often use callback functions to delegate logic back to the user. The standard library's qsort is the classic example: it knows how to sort...
Dec 26 Chapter 11: Variadic Functions Some C functions don't have a fixed number of arguments. The most famous example is printf, which takes a format string and... almost everything else....
Dec 24 Chapter 10: Mutable Arrays In C, arrays passed to functions are often used as output buffers. The function reads data from the array, modifies it in place, and expects the calle...
Dec 24 Chapter 9: Arrays vs. Pointers In C, arrays and pointers are cousins. In function arguments, they are twins. Declaring void func(int a[]) is exactly the same as void func(int *a). H...
Dec 24 Chapter 8: System Libraries on Linux Perl scripts often live in the terminal or deep inside a server handing out webpages or whatnot, but they don't have to stay there. Earlier, in chapte...
Dec 24 Chapter 7: Manual Memory Management Perl handles memory for you. C does not. When you start allocating raw memory buffers in Affix, you are stepping into the C world. This recipe demonst...
Dec 24 Chapter 6: Opaque Pointers Affix cannot directly instantiate a C++ class or read a C struct unless we define every single field with the correct type and in the proper order. Of...
Dec 24 Chapter 5: The Standard Library Every operating system comes with a "C standard library" or, simply, libc. It contains the fundamental building blocks of C programming: memory alloca...
Dec 23 Chapter 4: Strings and Static State Dealing with C strings usually involves asking "Who owns this memory?"...
Dec 23 Chapter 3: Binary Data and the Null Byte Trap C strings are simple: they start at a memory address and end at the first zero/null byte (\0). But what if your data contains nulls? If you use standa...
Dec 23 Chapter 2: Speaking Windows When interacting with the Windows API (Win32), you will inevitably face two facts: libraries are named things like user32.dll, and text is almost alwa...
Dec 22 Chapter 1: The Instant C Library Sometimes, you don't have a pre-existing .dll or .so file. Sometimes, you just have an idea, a heavy mathematical loop, or a snippet of C code you fou...
Dec 22 Welcome to the Affix Cookbook For decades, the barrier between Perl and the raw speed of C (or Rust, or C++, or Fortran) has been guarded by the complex, arcane rituals of XS. You...
Nov 23 Packed Trampolines We have nearly reached the end of the current roadmap. infix is fast, secure, and portable. But optimization is an addiction, and I am already looking...
Nov 16 Language Specific Trampolines This is an explanation and expansion of discussion #26 now that I've started actually implemented and designed my idea....
Oct 28 System calls via FFI I've had this idea for a while now and might put effort into it next. I understand that this would be a massive task but the first 30% of it is alread...
Oct 11 Coercion of arguments into a solid block of memory Shower thought time... Internally, would it be faster to coerce args that would end up on the stack into a single block of malloc'd memory? Currently,...
JUN 14 A Tour of SSA Form Construction How static single assignment form simplifies and enables modern compiler optimizations.
MAY 02 Intermediate Representations On the design space of IRs: from abstract syntax trees to control flow graphs to sea-of-nodes architectures.
APR 18 Building a Register Allocator A practical walkthrough of graph coloring and linear scan approaches for JIT compilation.
DEC 04 A Minimal C Frontend in 500 Lines Building a lexer, recursive descent parser, and AST generator from scratch.