כלל ה־no-XS, והמבנים שתפגשו#

pperl cannot load a compiled extension. This is structural, not a setting: XS binds to perl5’s C-level ABI, and pperl is a Rust reimplementation of Perl 5 that does not have one to bind to. An XSLoader::load under pperl fails with perl’s own message for a compiled part that is not there:

Can't locate loadable object for module Foo::Bar in @INC (@INC contains: ...)

That is the message modules with a Perl fallback test for, so they switch to their Perl implementation on it, as they would under perl.

מה שזה אומר עבור התקנה תלוי לחלוטין באופן שבו ההפצה מורכבת. קיימים חמישה מבנים בשטח והם מתנהגים בצורה שונה מאוד, ולכן כדאי לדעת באיזה מהם אתם מסתכלים.

1. Perl טהור#

אין מה לומר. היא מותקנת ורצה בדיוק כפי שהיא עושה בכל מקום אחר. זהו הרוב הגדול של CPAN.

2. מודול Perl עם מאיץ XS אופציונלי#

המודול הוא Perl; הפצה נפרדת מאיצה אותו אם היא קיימת. Readonly ו־ReadonlyXS הם הזוג הקלאסי.

התקינו את זה של Perl והתעלמו מהמאיץ. המודול מתשאל אחר מודול הלוואי שלו, אינו מוצא אותו, ומשתמש במימוש שלו עצמו - שהוא המסלול המתועד והנתמך, לא מצב מוגבל.

pperl --cpan Readonly       # yes
pperl --cpan ReadonlyXS     # no, and not needed

זהו המבנה שכדאי להעדיף כשיש לכם בחירה של תלות. מודול ה־Perl הוא המודול, ו־XS הוא אופטימיזציה.

3. הפצה אחת, שני החצאים, XS אופציונלי בזמן הבנייה#

ההפצה כוללת C וגם Perl, וה־Makefile.PL שלה מתייחס להידור כאופציונלי: אם שלב המהדר נכשל או נדחה, היא מתקינה את חצי ה־Perl וממשיכה. בזמן ריצה המודול מבחין ש־XS נעדר ומשתמש במימוש הנפילה־חזרה שלו.

Cookie::Baker ו־WWW::Form::UrlEncoded הם מהמבנה הזה. הם מותקנים בצורה נקייה תחת pperl.

pperl --cpan asks every build for that Perl-only form by the toolchain’s own switches: PUREPERL_ONLY=1 for ExtUtils::MakeMaker and --pureperl-only for Module::Build. A distribution that honours them, such as Params::Validate, builds its Perl half instead of stopping for want of a C compiler.

4. הפצה אחת, שני החצאים, XS חובה בזמן הבנייה#

אותו רעיון, אך הבנייה מהדרת C ללא תנאי. ההידור נכשל, המתקין מבטל את פעולתו, ועם מתקין רגיל חצי ה־Perl - שהיה עובד - לעולם אינו מגיע לדיסק.

HTTP::Parser::XS הוא הדוגמה המעובדת. pperl מממש אותו נייטיבית כעת, ולכן לא תיתקלו בו בפועל - מתקין רואה ש־0.17 כבר קיים ולעולם אינו מוריד את ההפצה כלל. הוא עדיין ההמחשה הברורה ביותר של המבנה, משום שהכשל אינו במקום שהייתם מנחשים. לוגיקת זמן־הריצה שלו ללא דופי:

eval {
    require XSLoader;
    XSLoader::load(__PACKAGE__, $VERSION);
    $BACKEND = 'xs';
};
die $@ if $@ && $BACKEND =~ /\bxs\b/;    # only if you FORCED xs
if (not __PACKAGE__->can('parse_http_response')) {
    require HTTP::Parser::XS::PP;        # automatic fallback
    $BACKEND = 'pp';
}

בהינתן הקבצים על הדיסק, הוא נופל חזרה מעצמו, ללא התערבות. הבעיה היא אך ורק ש־Makefile.PL בונה את picohttpparser ללא תנאי, ולכן הקבצים לעולם אינם מגיעים לשם. המתג PERL_ONLY=1 שלו בוחר את ה־backend של זמן הריצה ומפעיל יעד בדיקה; הוא אינו גורם לבנייה לדלג על ה־C.

הערה

מימוש נייטיבי הוא פתח המילוט, והוא זה שנעשה בו שימוש כאן. אך הוא מחזיק רק כל עוד הגרסה של pperl מספקת את מה שהתלויים בו מבקשים: pperl מדווח על הגרסה הנייטיבית, ולכן תלות ב־HTTP::Parser::XS >= 0.18 הייתה שולחת את המתקין אחרי ההפצה שוב והביטול היה חוזר.

A shape-4 distribution pperl does not implement natively installs all the same, because pperl’s ExtUtils::MakeMaker never compiles XS. Under pperl a Makefile.PL produces a Makefile for the Perl half only: no perl header files and no xsubpp are looked for, so the build cannot fail at the C. Two things then decide whether the distribution is installed:

  • every module in it that names an XS loader is loaded once. One that falls back to Perl, as above, loads. One that does not is named, and stays: a module can switch to its Perl half by an environment variable or a setting, as Params::Util does under PERL_PARAMS_UTIL_PP, which its own tests set;

  • the distribution’s own tests run, as for any other module, and they decide.

pperl: does not load without the compiled part, which pperl cannot build: Some/Dist/Fast.pm

A distribution built with Module::Build that does not honour --pureperl-only still attempts its compile and fails. For those, --cpan installs the Perl files the build had already copied into its blib directory. Each file that names an XS loader is loaded once first; one that does not load is installed all the same, for the same reason as above, and named. It says so:

pperl: Some-Dist-1.23-0: XS build failed; installed its 4 pure-Perl file(s)
pperl: Some-Dist-1.23-0: installed, but without the compiled part it does not load: Some/Dist/Fast.pm

ההרצה הזו מסתיימת בסטטוס כישלון, משום שהבדיקות של ההפצה מעולם לא רצו: בדקו את מה שהותקן לפני שאתם מסתמכים עליו.

5. XS בלבד#

No Perl implementation exists anywhere in the distribution. There is nothing to fall back to and nothing pperl can do with it. The load check names the module:

pperl: does not load without the compiled part, which pperl cannot build: String/CRC32.pm

Tests that load the module then fail with the missing-object message above, and a distribution whose tests fail is not installed.

The exception is a distribution pperl publishes a substitute for: a patch that replaces the compiled part with Perl, which --cpan fetches on every run, so it reaches a pperl released before the substitute existed. Text::CSV_XS installs that way, running on the Perl engine of Text::CSV. ”XS substitutes“ in installing and finding modules has the details.

כשאתם באמת זקוקים למהירות#

הסיבה לקיומו של XS היא ביצועים, ולכן הוגן לשאול מה מחליף אותו. לתשובה של pperl יש שלושה חלקים, ואף אחד מהם אינו ״לכתוב את זה ב־Perl ולקוות״.

  • מודולים נייטיביים. כמאה מהמודולים שאנשים פונים ל־XS כדי לקבל הם כבר Rust בתוך הבינארי, במהירות Rust, ללא שלב בנייה. ראו מה ש־pperl כבר מכיל.

  • ה־JIT. לולאות חמות מהודרות, ולכן גרסת ה־Perl הטהור של אלגוריתם אינה אוטומטית הגרסה האיטית. ראו את מדריך הביצועים.

  • Calling C directly. When you genuinely need a C library, call it rather than wrapping it in XS. Peta::FFI is pperl’s own interface; see the FFI primer. FFI::Platypus, the interface CPAN’s FFI modules are written against, is built in as well, and adds structs passed by value and callbacks with typed signatures. A distribution that bundles its own C for it (an ffi/ directory, built by FFI::Build) builds and installs with pperl when a C compiler is on PATH: that C becomes a library called through FFI, not an XS extension.

If you hit a module in shape 4 or 5 that you cannot work around, that is worth reporting, since a substitute can follow without a new pperl release: petamem/pperl-bugtracker#issues