Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98213 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 19515 invoked from network); 5 Feb 2017 22:25:38 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Feb 2017 22:25:38 -0000 Authentication-Results: pb1.pair.com header.from=bowersbros@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=bowersbros@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.49 as permitted sender) X-PHP-List-Original-Sender: bowersbros@gmail.com X-Host-Fingerprint: 74.125.82.49 mail-wm0-f49.google.com Received: from [74.125.82.49] ([74.125.82.49:34210] helo=mail-wm0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 70/92-30708-066A7985 for ; Sun, 05 Feb 2017 17:25:37 -0500 Received: by mail-wm0-f49.google.com with SMTP id 196so29698329wmm.1 for ; Sun, 05 Feb 2017 14:25:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=7/j64C/nEWKUDotKlHVOtdmdVKe2XdS5V+UXZcBZoOs=; b=e/FiV7r/KGobJDTs3GAhdrg3DhbnIiUbojMT9FqyZqaPn3sVW5e5XS/CHI1aFbc9EC R1iist0CQTjeEHHykAoSvgQsSs+Z4AhKlDX1bs/iwywTjpPKd3HfgvH++5K0MhtDkmf4 tA5rAu8WWt8mt1I+68qMlMS01x4rMwNQg4ZsMaea8JVOwrjk4MZr+b6OIlIftICUZqCs h4Rnk33L8zkJc2a+xc+Qqqd+OSn8IuqdyauFQU//cc9/yQ8SXF9giSORIfhhhkGl7SOJ EZxiinUAR95IAtmdKFJ5KoP42MQG6L3fMBkTVgGRjlexYA582DOztaIdI2rG2scNxKzX 0F1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=7/j64C/nEWKUDotKlHVOtdmdVKe2XdS5V+UXZcBZoOs=; b=FlKFMBcHA99uF1ES/wtIs4IGAnuTcu5aP5R+pA1A9lslPXh7RAPsrxBnWi8Y7LRzMa 0O2Qon8fJF0HJuVyTkUv2NllQ4GxH/QIv6gj/9zGja/O4SkaQi9V0YPxZ07FSQiSEbFy WUPM10tOB2ur5UVRjojp7nH5UJeg3KyUbjvtojjliCdwj/++VJyyQrEKMtw2hTbPJKKZ lmc7Lsxr6czIV97rvYh64hoynvE/DdcyDI37spcdLhlvmimWXTJftHku+Te1GlnwibLY 2PdynskznpFIHym6nkuNSwOEskfLQEas3Oon54lVOPIc9oz91XLxKkYpnYYKiNkdGcSP PGsQ== X-Gm-Message-State: AMke39nUMdjESwqgfw1OGQh+/+hb46qa0SrNB8IFjE/OhutIj+eHQlu+/OCOMq7WfSlad6dXGyubwhTyCEd6ww== X-Received: by 10.28.188.9 with SMTP id m9mr5854129wmf.79.1486333533624; Sun, 05 Feb 2017 14:25:33 -0800 (PST) MIME-Version: 1.0 Received: by 10.28.181.147 with HTTP; Sun, 5 Feb 2017 14:25:33 -0800 (PST) In-Reply-To: References: Date: Sun, 5 Feb 2017 22:25:33 +0000 Message-ID: To: PHP Content-Type: multipart/alternative; boundary=001a114b24d84a238a0547cffebb Subject: Re: [Discussion] FFI in PHP From: bowersbros@gmail.com (Alex Bowers) --001a114b24d84a238a0547cffebb Content-Type: text/plain; charset=UTF-8 And here is the previous messaging without borked formatting. Sorry folks. FFI RFC ====== There are many languages that support an FFI implementation. NodeJS Python C++ Ruby FFI allows you to call a native C function without requiring the boilerplate of an extension to be written. There are several benefits to having FFI - Performance - Shareability / Bundling - Common functionality between languages Performance === Although less pronounced than the 5.x versions, there is still a performance benefit to having native C code over PHP code. For example, you could utilise threading inside of your FFI methods, which PHP does not expose the ability to do. Shareability === If you wish to implement some of your source code in C, the current way to share it is to build it as an extension. This is cumbersome, and restricts use-cases such as shared hosting, where the ability to install your own extensions is probably restricted. However, with FFI, the shared object can be loaded from any location, and so that restriction is no longer in place. They could even be distributed via composer. Common functionality between languages === If you have some complex logic that needs to be replicated in several languages for whatever reason; implementing it several times over would lead to uncertain bugs and technical debt increasing heavily. If you could share the same logic amongst them all using FFI, then this is no longer an issue. Example === Take an example of a rust program that takes two numbers in and gives you the sum of them. ```rust #[no_mangle] pub extern fn add(a: i32, b: i32) -> i32 { a + b } ``` with the Cargo.toml file containing: ``` [package] name = "math" version = "0.1.0" authors = ["Alex Bowers "] [dependencies] [lib] name = "math" crate-type = ["dylib"] ``` `cargo build --release` will create `.so`, `.dylib`, or `.dll` files depending on your system. These should be usable within PHP using the exposed functions. ```php $math = ffi("/path/to/math.so"); $result = $math->add(1, 5); echo $result; // 6 ``` With the implementation at its most basic level, calling the `add` method with incorrect parameters would likely cause a segfault. A way around that could be that the methods are not immediately exposed, but have to be configured. Something like: ```php $math = ffi("/path/to/math.so"); $math->add(1, 5); // Throws RuntimeException, method not configured $math->configure('add', int $a, int $b); $math->add(1, 5); // 6 $math->add('a', 5); Fatal error: Uncaught TypeError: Argument 1 passed to add() must be of the type integer, string given ``` Prior art: === https://pecl.php.net/package/ffi - Last release > 13 yearshttps://github.com/mgdm/MFFI - Not stable, last commit > 1 year, no releases --001a114b24d84a238a0547cffebb--