r/perl • • 2h ago

Synthetic Scars: Giving Autonomous AI Agents the Battle-Tested Memory of a Senior Engineer · Zoom · Luma

Thumbnail
luma.com
2 Upvotes

r/perl • • 5h ago

Discord::Client – A Discord API client for Perl

Thumbnail
github.com
3 Upvotes

r/perl • • 17h ago

Affix: FFI that makes native code feel like Perl

22 Upvotes

Affix is an FFI for Perl with a simple set of goals:

  • calling native code and dealing with raw memory should feel like writing ordinary Perl
  • it should be incredibly fast
  • the name should describe the act of binding something to something else and have 'FFI' in the dead center so you don't forget what it is

I wanted an FFI that feels good. This is what I came up with:

use v5.40;
use Affix qw[:all];
affix libc(), 'strlen', [ String ] => Size_t; # This is a terrible example but...
say strlen('hello');    # ...five.

Or maybe you're on Windows?

use Affix qw[:all];
wrap( 'user32', 'MessageBoxA', [ UInt, String, String, UInt ] => Int )->( 0, 'JAPH,', 'Hello, World', 0 );

...okay, hardly groundbreaking so far but I'm starting slow.

Affix is a bundle of amazing stuff but the type system might be the secret sauce. Primitives, enums, unions, bitfields, structs (including packed and nested structs), pointers, callbacks, variadics, wide strings, file handles, 128bit numerics, C99 complex numbers, flexible and n-dimensional arrays, and SIMD vectors are all first-class. Cast C memory and you get native Perl hashes and arrays with magic underneath, so $p->{x} = 42; writes straight into the C struct. You can even pin a scalar to an exported symbol or address in memory and read and write through it. No generated accessor methods, no copies, no ceremony.

use v5.40;
use Affix qw[:memory :types :core];
use Affix::Build;
#
my $c = Affix::Build->new;
$c->add( \<<~'C', lang => 'c' );
    typedef struct { int x; int y; } Point;
    int get_x( Point * pt ) { return pt->x; }
    int get_y( Point * pt ) { return pt->y; }
    Point new_pt( int x, int y ) { Point pt = { .x = x, .y = y }; return pt; }
    C
#
typedef Point => Struct [ x => Int, y => Int ];
affix $c->link, 'get_x', [ Pointer [ Point() ] ] => Int;
affix $c->link, 'get_y',  [ Pointer [ Point() ] ] => Int;
affix $c->link, 'new_pt', [ Int , Int ] => Point();
#
my $p = cast( malloc( sizeof Point() ), Point() );    # Allocates memory and pins a scalar to it
$p->{x} = 42;                                         # Writes directly to memory
$p->{y} = 7;
#
say get_x($p) . ' x ' . get_y($p);                    # The pin works as a pointer arg (42 x 7)
say get_x( { y => 50, x => 100 } );                   # Hashes are coerced automatically (100)
#
free $p;                                              # Auto-freed at end of scope, or freed early manually
$p->{x} = 1000;                                       # No longer bound to C memory; just a normal hashref now
#
my $p2 = new_pt(30, 25);
say $p2->{x} . ' x ' . $p2->{y};                      # struct from C is treated as a hashref (30 x 25)
say get_x($p2);                                       # Which you can turn around and hand back to native code even as a pointer (30)

In that example, I used Affix::Build which wraps compilers and build systems from Assembly to Zig to create shared libs for Affix (or even FFI::Platypus!). Think of it as Inline::C for FFI.

use v5.40;
use Affix qw[affix Int];
use Affix::Build;
#
my $c = Affix::Build->new;
$c->add( \<<~'C', lang => 'c' );
    int plus(int a, int b) { return a + b; }
    C
#
affix $c->link, 'plus', [ Int, Int ] => Int;
say plus( 2, 3 ); # ...also five.

You can even compile and link multiple languages into a single lib as long as they share similar ABIs. I'm not sure why you'd want to do that but you can.

Affix::Wrap goes the other direction: hand it C/C++ headers and it parses them using Clang's AST (with a pure-Perl fallback if you don't have LLVM installed) to generate bindings for an entire library. Bindings are ready to run immediately, or you can export them as an early draft if your project is bound for CPAN.

So, we've made native code feel like Perl but is it fast?

FFI can come with a lot of overhead but I regularly benchmark Affix against hand-written XS, Inline::C, and FFI::Platypus. Here's a quick snapshot from the latest v1.4.3 dist:

Linux x86_64 (perl v5.42.0)

  • sin(double): Affix 14.45M/s | XS 14.15M/s | Inline::C 13.31M/s | FFI::Platypus 5.32M/s
  • abs(int): Affix 11.96M/s | XS 12.24M/s | Inline::C 12.19M/s | FFI::Platypus 4.96M/s
  • strlen(const char*): Affix 11.60M/s | XS 15.35M/s | Inline::C 16.59M/s | FFI::Platypus 5.39M/s

Windows x64 (perl v5.42.3)

  • sin(double): Affix 16.60M/s | XS 5.36M/s | Inline::C 5.39M/s | FFI::Platypus 2.42M/s
  • abs(int): Affix 12.26M/s | XS 5.84M/s | Inline::C 5.89M/s | FFI::Platypus 2.30M/s
  • strlen(const char*): Affix 11.03M/s | XS 6.59M/s | Inline::C 6.59M/s | FFI::Platypus 2.41M/s

(Full multi-platform results including aarch64 and macOS are in this workflow run.)

XS can still win on very well-optimized calls, and Inline::C and FFI::Platypus are battle-tested tools that I have a lot of respect for. The point of Affix is that you don't have to trade away runtime binding, rich types, or zero-copy structs to get near-XS (or better) call throughput.

I'll try to write more about the things I'm working on. I rarely see my own projects as complete or even far enough along for a public announcement like this. To demonstrate just how silly that is, Affix has been on CPAN for five years but I've never spoken or even written about it at length outside of the project's repo. I think the projects I pick up are all cool, and it'd be nice if others thought so too, which is hard to do if no one knows they exist. My current goal for Affix is to support popular compilers on every 64bit system I can buy or find a GitHub runner for: Windows, Linux, macOS, Haiku, Solaris, Android (Termux), GNU/Hurd, and several BSDs on x64, ARM, and RISC-V so far. Part of feeling like Perl is working (when it makes sense) where Perl works.


r/perl • • 2h ago

The Underbar, episode 12: Vienna.pm

Thumbnail
underbar.cpan.io
2 Upvotes