Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81426 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37192 invoked from network); 30 Jan 2015 13:07:25 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Jan 2015 13:07:25 -0000 Authentication-Results: pb1.pair.com smtp.mail=lisachenko.it@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=lisachenko.it@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.52 as permitted sender) X-PHP-List-Original-Sender: lisachenko.it@gmail.com X-Host-Fingerprint: 74.125.82.52 mail-wg0-f52.google.com Received: from [74.125.82.52] ([74.125.82.52:58845] helo=mail-wg0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 86/30-35409-C028BC45 for ; Fri, 30 Jan 2015 08:07:25 -0500 Received: by mail-wg0-f52.google.com with SMTP id y19so26745568wgg.11 for ; Fri, 30 Jan 2015 05:07:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=SJ3NsOOHUomck/8cHdAElNxit9ynBjBHFy8XFyA/HcU=; b=y9b5ga6yhIPb55kLqOlUp9/8xFEMc1q7Z1koU7wNVBIizEgy1RbRqB6cPOnNVecovz iaWbdyHsjkSS8d6n/yJF/TIy+MsL++oE1DcJKda7/s2LpTOMaD9W9/1Wq6BrqC/g3NAE tWMPu6U5W36hLq3FyQFYvs1EY2PC0k+Wr5L7Aj2U6hxlf3EHQLFchR9qnN8+qEy5xp0h 7z6zJkvuxqSNQWMJ3WuxVZ6Zz78QJDobjHGz0l0h1DxyX4c+V2Jml5+i6yTHgNCD7bDr LYWvuBypuYDPrlGMWrSZssturxy21n5ctqvNOA7Oxe8Ti3zIvecFwrAO9quvoowvEerq umZw== MIME-Version: 1.0 X-Received: by 10.180.207.66 with SMTP id lu2mr4674950wic.13.1422623242226; Fri, 30 Jan 2015 05:07:22 -0800 (PST) Received: by 10.194.154.229 with HTTP; Fri, 30 Jan 2015 05:07:22 -0800 (PST) Date: Fri, 30 Jan 2015 16:07:22 +0300 Message-ID: To: PHP internals list Content-Type: multipart/alternative; boundary=001a11c3d8ee009413050dde4973 Subject: [RFC] Immutable variables and objects From: lisachenko.it@gmail.com (Alexander Lisachenko) --001a11c3d8ee009413050dde4973 Content-Type: text/plain; charset=UTF-8 Hello, internals! Today I was looking at PSR-7 and discovered this part of code: $body = new StringStream(json_encode(['tasks' => [ 'Code', 'Coffee', ]]));; $request = $baseRequest ->withUri($uri->withPath('/tasks/user/' . $userId)) ->withMethod('POST') ->withHeader('Content-Type' => 'application/json') ->withBody($body); $response = $client->send($request); What is wrong here? Emulated immutability. All methods will create a separate instance of request, so $baseRequest->withUri()->withMethod()->withHeader()->withBody() will create total 5 object instances. It's a memory overhead and time consumption for each method call. What I want to discuss is true immutability flag for variables and parameters. There are a lot of languages that use "final" or "const" keywords to prevent modification of variables. We can use this approach by extending language syntax as following: const $text = "Some message"; // defines an immutable variable that can not be modified. $text .= "foo"; // Exception: can not change the immutable variable class Test { public final $value; // final keyword defines immutable property, but it isn't initialized yet so allows only one assignment, then will be immutable public function __construct(const $value) // Argument is declared as immutable with "const" keyword, can not be changed in the function body { $this->value = $value; // $value = 456; // will throw an exception } } $obj = new Test(42); echo $obj->value; // Direct access to the property, result is 42 $obj->value = 100; // Exception: can not change the immutable (final) property Immutable variable can not be made mutable, but it can be assigned to the separate mutable variable (copy on write). Example: const $text = "Some message"; $anotherText = $text; $anotherText .= "modified" On the engine level this immutable variables and parameters can be effectively optimized by using IS_TYPE_IMMUTABLE for zval. This can result in achieving better performance and various JIT-optimizations. Thoughts? --001a11c3d8ee009413050dde4973--