Aug 03rd #Affix.pm Hardware Registers and Bitfield Packing In embedded systems, binary file formats, and network protocols, space is at a premium. Instead of using a whole int for a boolean flag, programmers o...
Mar 05th #Affix.pm 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 05th #Affix.pm 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 05th #Affix.pm 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 20th #infix 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 11th #Affix.pm Absolute Power with Assembly and Perl C is often called "portable assembly," but sometimes you need the real thing....
Jan 11th #Affix.pm Scientific Speed with Fortran and Perl If you are doing heavy scientific computing, you will eventually encounter Fortran (BLAS, LAPACK)....
Jan 11th #Affix.pm Oxidizing Perl (Binding to Rust) Perl is great for glue; Rust is great for logic....
Jan 11th #Affix.pm 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 08th #Affix.pm 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 07th #Affix.pm 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 04th #Affix.pm 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 04th #Affix.pm SIMD and Matrix Benchmarks So, you read Chapters 22 and 23, but the ultimate question remains: "Is it fast?"...
Jan 04th #Affix.pm 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 04th #Affix.pm 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 04th #Affix.pm 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 04th #Affix.pm 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 03rd #Affix.pm 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 30th #Affix.pm 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 30th #Affix.pm 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 29th #Affix.pm 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 29th #Affix.pm 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 29th #Affix.pm Automatic Resource Management (RAII) Manual free() is error-prone. We can use Perl's DESTROY phase to automate it....
Dec 29th #Affix.pm 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 29th #Affix.pm 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 29th #Affix.pm 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 28th #Affix.pm 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 27th #Affix.pm 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 27th #Affix.pm 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 27th #Affix.pm 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 26th #Affix.pm 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 26th #Affix.pm 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 24th #Affix.pm 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 24th #Affix.pm 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 24th #Affix.pm 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 24th #Affix.pm 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 24th #Affix.pm 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 24th #Affix.pm 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 23rd #Affix.pm Strings and Static State Dealing with C strings usually involves asking "Who owns this memory?"...
Dec 23rd #Affix.pm 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 23rd #Affix.pm 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 22nd #Affix.pm 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 22nd #Affix.pm 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...
Dec 13th #Affix.pm Stable enough Full Changelog: https://github.com/sanko/Affix.pm/compare/v0.12.0...v1.0.0...
Nov 23rd #infix 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 16th #infix Language Specific Trampolines This is an explanation and expansion of discussion #26 now that I've started actually implemented and designed my idea....
Oct 28th #infix 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 11th #infix 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,...
Jul 05th #Affix.pm Todo list Features I'd love to include when I have the time will find their way here....