Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98214 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 41438 invoked from network); 6 Feb 2017 09:04:39 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Feb 2017 09:04:39 -0000 Authentication-Results: pb1.pair.com header.from=mike.php.net@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=mike.php.net@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.65 as permitted sender) X-PHP-List-Original-Sender: mike.php.net@gmail.com X-Host-Fingerprint: 74.125.82.65 mail-wm0-f65.google.com Received: from [74.125.82.65] ([74.125.82.65:35419] helo=mail-wm0-f65.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E1/93-30708-52C38985 for ; Mon, 06 Feb 2017 04:04:37 -0500 Received: by mail-wm0-f65.google.com with SMTP id u63so20561767wmu.2 for ; Mon, 06 Feb 2017 01:04:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:subject:to:references:from:message-id:date:user-agent :mime-version:in-reply-to; bh=xusl36sfEaF920eRKgX8ezMLUxdM7mDmJJFJcnxGCLI=; b=bYN3PLBbWhESNHBS4XQ2MLm0VxcXLvJ4+3bQwiNQeriZBGidYySW5gsEWzB2NggMH0 coCUCukP4jHoci7/c918VX3nnzEzhWsWDhjWAafIGyx0IyTed1MzWLX1arlrusOW78YI 3Eek8MCXJlsF5KwQ9NYbq7uibWkXobkehc3LhVhkColKU8qKZ6P4382SEy8Nq1pmRq/x uQv7KI+zf/MygUMIB7U5dA/NWnRlt7hrDMAoMNP24r2y3gnUzAl1KMNet9Q+3jQdH3DK cl3iJUhePmAkIoEL7V70UhhZsRlLfwsN7zCcPcrM/qw+SrLj1PvFu9PmjweCxIVs62ry gdtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:subject:to:references:from:message-id :date:user-agent:mime-version:in-reply-to; bh=xusl36sfEaF920eRKgX8ezMLUxdM7mDmJJFJcnxGCLI=; b=FGxn9hKTa0V4ErCvUn6LEOPsnrT7mUgQCbQnHw3KmZ++afmhwdeha6/oKIctCqAGlr gjZD/BPnmMR9dWZ1GB/9WTJxG9nBwxWej8XD5BU2iI9vD5FvPJ+mzgSLFEY6XmUXRT6I BeSqLYPSNi572mulP3lRYjdlL2JUx895JAQLWpEhPNQSMGO/jrU0cmmhc9aqeGqwaFeZ pMT79QDJdD3gUNtfaLO8FxxkgIBtR5cnlY7P9fgOK/S9QgpM4saPmDIgxbEdyOU0Oznz C2S8/oTNK04SrbUjNVoSccfeWjJrtjEz1uV8PfF2varagEowacakbibatfqVDrD2muj6 7VIw== X-Gm-Message-State: AMke39n2EK5TuMMGF1G7snWqGBtmSWMMYTsuvt7jP8scXtNclNwK1f9/zQIk0O7jAT1iOg== X-Received: by 10.28.49.70 with SMTP id x67mr7990220wmx.69.1486371874183; Mon, 06 Feb 2017 01:04:34 -0800 (PST) Received: from [192.168.2.207] (89-104-28-113.customer.bnet.at. [89.104.28.113]) by smtp.googlemail.com with ESMTPSA id t103sm169034wrc.43.2017.02.06.01.04.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 06 Feb 2017 01:04:33 -0800 (PST) Sender: Michael Wallner To: Alex Bowers , PHP References: Message-ID: Date: Mon, 6 Feb 2017 10:04:32 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="US3UCo7sc1jce8HcWpE0Ru6PwU42WC2uE" Subject: Re: [PHP-DEV] [Discussion] FFI in PHP From: mike@php.net (Michael Wallner) --US3UCo7sc1jce8HcWpE0Ru6PwU42WC2uE Content-Type: multipart/mixed; boundary="wVCvsVx4ekTGe2KocRechQewwn4p1oPlW"; protected-headers="v1" From: Michael Wallner To: Alex Bowers , PHP Message-ID: Subject: Re: [PHP-DEV] [Discussion] FFI in PHP References: In-Reply-To: --wVCvsVx4ekTGe2KocRechQewwn4p1oPlW Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 05/02/17 23:25, Alex Bowers wrote: > And here is the previous messaging without borked formatting. Sorry fol= ks. >=20 >=20 > FFI RFC > =3D=3D=3D=3D=3D=3D >=20 > There are many languages that support an FFI implementation. >=20 > NodeJS > Python > C++ > Ruby >=20 > FFI allows you to call a native C function without requiring the > boilerplate of an extension to be written. >=20 > There are several benefits to having FFI >=20 > - Performance > - Shareability / Bundling > - Common functionality between languages >=20 > Performance > =3D=3D=3D > 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. >=20 > Shareability > =3D=3D=3D >=20 > 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. >=20 > They could even be distributed via composer. >=20 > Common functionality between languages > =3D=3D=3D >=20 > 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. >=20 > Example > =3D=3D=3D >=20 > Take an example of a rust program that takes two numbers in and gives > you the sum of them. >=20 > ```rust > #[no_mangle] > pub extern fn add(a: i32, b: i32) -> i32 { > a + b > } >=20 > ``` >=20 > with the Cargo.toml file containing: >=20 > ``` > [package] > name =3D "math" > version =3D "0.1.0" > authors =3D ["Alex Bowers "] >=20 > [dependencies] >=20 > [lib] > name =3D "math" > crate-type =3D ["dylib"] > ``` >=20 > `cargo build --release` will create `.so`, `.dylib`, or `.dll` files > depending on your system. >=20 > These should be usable within PHP using the exposed functions. >=20 > ```php > $math =3D ffi("/path/to/math.so"); > $result =3D $math->add(1, 5); >=20 > echo $result; // 6 > ``` >=20 > With the implementation at its most basic level, calling the `add` > method with incorrect parameters would likely cause a segfault. >=20 > A way around that could be that the methods are not immediately > exposed, but have to be configured. >=20 > Something like: >=20 > ```php > $math =3D ffi("/path/to/math.so"); > $math->add(1, 5); // Throws RuntimeException, method not configured >=20 > $math->configure('add', int $a, int $b); >=20 > $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 > ``` >=20 > Prior art: > =3D=3D=3D > https://pecl.php.net/package/ffi - Last release > 13 > yearshttps://github.com/mgdm/MFFI - Not stable, last commit > 1 year, > no releases >=20 Trying to add answers later today, in the meantime, please have a look at https://github.com/m6w6/ext-psi --=20 Regards, Mike --wVCvsVx4ekTGe2KocRechQewwn4p1oPlW-- --US3UCo7sc1jce8HcWpE0Ru6PwU42WC2uE Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCgAdFiEEHsPHHd1jWDGjN9aESA4+FLCkx8cFAliYPCAACgkQSA4+FLCk x8c93wf/Y/nKoepyk1SW6/2IRChVbXolrXM/IXoehI8MA/bDUx3opFHVzTQF7Emh NYMNevgCzUFVMjEQwDw+KM+3Wb1mVBB5bRE+3x2XicPUU1RdDf3JNyYP/icjD28k 16Yejv24hohu1nZH5Pl/JYCFUpqiltHlSwPotp9wcHS+LXfmKZJTBLeJPSDNF7rS hBxI5mc6XMY/BXyHK9CtNSKtZBe5igMDE0LNwby8fYyO/OkfZE0WeJOiuaPgJX0Y sFf6WCZANBQQ29azX/1sbuLn1YbVFKwVdGC70YqeGcTQsf5x6/4VtXCTyqw6Qm8G /zczYGchTDA5DBf54WH5Kmdr7Mj6pw== =C1Vk -----END PGP SIGNATURE----- --US3UCo7sc1jce8HcWpE0Ru6PwU42WC2uE--