Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107941 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 63889 invoked from network); 23 Dec 2019 09:57:44 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 23 Dec 2019 09:57:44 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id 3FC192C04EB for ; Sun, 22 Dec 2019 23:59:02 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS8560 212.227.0.0/16 X-Spam-Virus: No Received: from mout.gmx.net (mout.gmx.net [212.227.17.21]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Sun, 22 Dec 2019 23:59:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1577087935; bh=3/xiOszg9BWQFk0JfIxGwz1WgeXyEYnBAC/yUqb3SEc=; h=X-UI-Sender-Class:Subject:To:References:From:Date:In-Reply-To; b=e1dmoG1UNpABjtZfLFD/DtavhEtA14PuhQeonqNAo6uPYo2GOMTJvFDYK+8k8g0Uy i1qVvXPjP94O5b2DsrvZDfDs2n6ygFUh8uj4JX5pe0ivzgrCosTf2/1ESEJNSm4Jr1 fU9uo6bnHBTTDrqbxxii5Dpn/dW6EC56j8LRJ59s= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [192.168.2.130] ([84.179.229.4]) by mail.gmx.com (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1N1wpt-1jlPa20WQT-012LBA; Mon, 23 Dec 2019 08:58:55 +0100 To: Larry Garfield , php internals References: <2c1288a6-11f3-4c1b-8787-384d33c5b236@www.fastmail.com> Message-ID: <608344ee-3a47-3b0e-d5df-e32e6f4fb92c@gmx.de> Date: Mon, 23 Dec 2019 08:58:55 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 MIME-Version: 1.0 In-Reply-To: <2c1288a6-11f3-4c1b-8787-384d33c5b236@www.fastmail.com> Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:yGSoE9pTPUH20r5GeaqilA3ORjVjpz8+DpczfaYFbTyFtVhRFx/ 1zpwLmH0R1GDF4UmlY2wLrVOjS7xsFtNcWQyLqFt2cKYxhM/csW6dQZ40kivNHy+pdsTQB6 rXIFtbTXLmYxReOXrUOV7C+rLBUj0dP0bykEtKGkLHxk/jzJ/aF197WmzITle6Rs5MBnP2j nwG8GjehMbaLjMI+WDT9g== X-UI-Out-Filterresults: notjunk:1;V03:K0:qf1BEo77mm4=:xkpEx/rx1te3i6zT8YHtqe T+aiIYNtrvaz/dhS23iQEOmUVXY8Ykk2WX4lxNE3DLpCKjQfNmB7AVUFAByj7My9Zm3mw90ik J9jQbMXMo+ULa2FlC/yBQDf+5vtGotkWDvHfjhr40gxBgUezQUOaMRPVoMRGXfPlMoRBzs+fi V1idlneDFoU8CNSOoZli4vfNzhFVIbgDTKcSNWk0YFIy64xPCHiFZS7jAS4QISgSDxS8oEN4H JHgzBn1PNi2n/B42YbXh23Fq3jMS4x4uzHlNiRlf/ywmKwwQbmQIpc0Ur3GshCr3FpD7TqiLM bqfurkds3cKkjkxzUglV5pOqFiol2YYKVGsVJYfGJmaOPVOVi1VBOcmYCMrRzXI1H6EMLWStY 223XM9/gmIKwfTgDjDeS/WxZlFHhRo2je6biwP+n949T/8kdF45gfktxFOPNLK+X9riJDD/Nt rVf/w52XaGSy3FL/tG6wKKWVkmWIsSlC4v7rXmxal/s6ZhM+y3LEAuiTX43L0nvo4+GBBEvlR WmgfZolkbgTUIlcCVVjZ2LH+8GjiEbkZ1zqIRR76h8j98dg9hRJGXfkF1RZJ5O/sYXZ25eIMU I8LWCYEfoHugy/+jJvBlP/x8JR9pNQV+bTFM/ZODbur7j+OKmGuAvmlIdC+2z1p3EZSN1oxAh bgKk1GnsFd5xv/zHrABXJRDrOEE59xO3p/bU+sg04/NxYP9xS5WYuFq+rP6zPgo+1BakHOikB FfezThYjEJuIRamRo/Dmx3bdWZULfgv2lp9EazI5meuXmc9xdkGuhIMpJDVxLUFoJfcZ/ELzG x1pHK1bo1CAOCTW2TASYuJwEgcu8f7JTed8ITZu34BuVFl7/V/5vhRrjhvvWaB2ryo1ucKKAK gFfn4DK0MsU6PNzn2BiMMjz0W/IEPNqCVPpgd1UMTkcrWWtJbCbeTKcamuvV6TClP7tE1tXWC LKdWjmn4rx07xjrIpMuU4Kdw9D6hq27N+Tj8z3t4X/qUEUo58eE2k2w5feYNsL023XlwBJ9tK RRJoy/mqeMMRCtz21WGtc13lhEJDpQtuB8GdwWnOJo3QydBonIAuMlLLEhepe6FJ8jCIAWF6C 6NvEmkr6SsCmJVhE8yrGgpt20k9nGIiTewK5hao8iQCfwX1ywzd1Y4E60mJ+NHcdCJw/UviO5 3aEIxQKRuwusAdNW+bUYdzTQV/t3vd1++FTUeamMzwJXEpDFZVCD4AQLqGMbLTIuBTzpQ3C/Y YF8NIem0Hme3Q/HoBN4mtQkbw2iIGIQV4bqtEN1T+lCNvdbDNLJGwK6aDYLI= X-Envelope-From: Subject: Re: FFI, Preload, and performance From: cmbecker69@gmx.de ("Christoph M. Becker") On 20.12.2019 at 23:08, Larry Garfield wrote: > The documentation doesn't really cover this, so I'm hoping it's OK to as= k here. > > The FFI docs say that for web requests, you really really ought to use p= reloading and then load/scope to manage an FFI library because it's going = to be too slow to link up otherwise. See the example here: > > https://www.php.net/manual/en/ffi.examples-complete.php > > What is unclear, though, is the performance impact of calling scope(). = It happens at runtime in the example so I presume it's "fast enough" at ru= ntime. However, is it expected that it's only ever called once for a give= n scope? Or would it be safe to call multiple times in different places j= ust to get an unshared reference to it? Basically, would this be safe or = a bad idea, and why? > > > final class PrintProxy { > private static $ffi =3D null; > function __construct() { > if (is_null(self::$ffi)) { > self::$ffi =3D FFI::scope("DUMMY"); > } > } > function printf($format, ...$args) { > return (int)self::$ffi->printf($format, ...$args); > } > } > > final class SprintfProxy { > private static $ffi =3D null; > function __construct() { > if (is_null(self::$ffi)) { > self::$ffi =3D FFI::scope("DUMMY"); > } > } > function printf($format, ...$args) { > return (string)self::$ffi->sprintf($format, ...$args); > } > } > > > The alternative being to have a single object that wraps scope("DUMMY") = just once, and then inject that into both of those classes. Is that worth= the extra hassle or no, and why? =46rom looking at the implementation[1], FFI::scope() looks like a pretty cheap operation; basically it's just looking up the scope from a HashTable, and constructing a thin wrapper object around that. But as always: when in doubt, measure! :) [1] =2D- Christoph M. Becker