Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111866 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 10262 invoked from network); 15 Sep 2020 14:55:22 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 15 Sep 2020 14:55:22 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 7F36B1804C3 for ; Tue, 15 Sep 2020 07:03:30 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ua1-f46.google.com (mail-ua1-f46.google.com [209.85.222.46]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 15 Sep 2020 07:03:29 -0700 (PDT) Received: by mail-ua1-f46.google.com with SMTP id u14so1072015uaq.1 for ; Tue, 15 Sep 2020 07:03:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=qrsLsvCzWRly7iC0VzGJsnw4Pc4XjHh+sJDesMDVKSk=; b=iS7Kgte4oExZ023S31rlV74MecSwSV797w40eucvG91pwqESQ4GlzsMNmGbRW95qot XRevqI8cujPcZFhomtq5eFo/NknWzD3Ic6iuvAaPWevNZXP7pDwb0n+nsrU/cRZSJenm pYzn4m1KRzXtdiRwesDHzsu4xVwtoabd+uAysaEZ6w6ZRcsIllhvbfZla2KxVQcgLTw1 0lBeBR4acFLO8+ERDaeli5Yf0hQYLZxEqBE/HM9wYsT4gRmm2nBvreqRuTPIQOv0K4cc /mTOpwIDIgY8c9vAPvOhIH0Nn+Yqsp5Sq0clfl28AwvyPLSFrMsTJ7o9wXwac9sgZPrR IysA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qrsLsvCzWRly7iC0VzGJsnw4Pc4XjHh+sJDesMDVKSk=; b=uLZOGJLf/UZSGtfqQMrUFXSr+5uCv8QNi0bIjyNUAX1tl9PYmnQTG7DzvBTHz7mLde BmNy6FnRfwthPD4+PpeyOZQa0I8U0iIgXZaE2agqonbnUFHbcFGay0kSf6TvDor7ikQf KxHUJzYIQviJIRRPNhcSQTSpXZB1jnIW8CfC5GEWimdrNqD7V8ex3YrkO+sIxuLe0EKB ZtQpyrFDf8iaLXxpgv56vEmZ2jKhbuLxVWS0QAksBV0G7mqeHOw49R/9m4IAoTiav/0n MHz7QlAtzKOnFy1/NerufYJI9aBAMT0N4T7Es1X+JpzqvExZjv7zJm47ViE3c4rPcFsf Yt7g== X-Gm-Message-State: AOAM532H1aTMQwdPikE1g7n5+qe+AQN8nhV6Q01OsdEfa+HjVJVuTDqS E3TTJCGD3MYRC5at0SJtYrxLyQsl77J1KaN/QGc= X-Google-Smtp-Source: ABdhPJzWvoUhqw0AFdLE6E3O3ZywoHsPLFtMGSMIieb5SkpVh35DhLYU2N+kCEGL2ZvV8SuAjRqEPH2IJ4nGLfdBPe0= X-Received: by 2002:ab0:6cf0:: with SMTP id l16mr9679121uai.95.1600178607479; Tue, 15 Sep 2020 07:03:27 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 15 Sep 2020 10:03:16 -0400 Message-ID: To: Benas IML Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000079dfad05af5a9d18" Subject: Re: [PHP-DEV] Compiler Optimizations From: chasepeeler@gmail.com (Chase Peeler) --00000000000079dfad05af5a9d18 Content-Type: text/plain; charset="UTF-8" On Tue, Sep 15, 2020 at 9:51 AM Benas IML wrote: > Ah, sorry! Misread your post. Anyways, the compiler doesn't transform > `\array_keys()` yet, so there's no optimization for that. > > As for other compiler optimizations, 2 that I know that the compiler does > is: > > 1. Binary OP evaluation i. e. `2 * 2` does not yield `ZEND_ADD` opcode but > is instead computed by the compiler directly. > > 2. There are special functions that have their own opcodes. You can learn > more about those here: > > https://phpinternals.net/articles/optimising_internal_functions_via_new_opcode_instructions > That's a good starting point, thanks. > > Best regards, > Benas > > On Tue, Sep 15, 2020, 4:44 PM Chase Peeler wrote: > >> I wasn't proposing that my example be supported. I'm totally okay with >> the fact that it doesn't. My question was about whether or not those kinds >> of optimizations are documented anywhere so that developers can make sure >> they don't miss out on them by not fitting the proper pattern. >> >> On Tue, Sep 15, 2020 at 9:40 AM Benas IML >> wrote: >> >>> Hey, >>> >>> During my free time, I'm implementing that specific `array_keys` >>> optimization. I'm not planning on supporting cases like yours (i. e. >>> indirection through a variable) since there's no point in doing that. And >>> also, it's not feasible to support every use case. Should we also support >>> cases like this? >>> >>> ```php >>> $a = 'array_keys'; >>> $b = $a(...); >>> $c = 'b'; >>> >>> foreach ($$c as $key) { >>> ... >>> } >>> ``` >>> >>> Obviously not. `\array_keys` optimization will work the same way as an >>> optimized `strlen` function works. >>> >>> That means that the optimization is only going to be applied if the >>> `array_keys` function is used directly in the `foreach` loop and only if a) >>> either the namespace is global b) or `\array_keys(...)`/`use function >>> array_keys` is used. >>> >>> >>> Best regards, >>> Benas >>> >>> On Tue, Sep 15, 2020, 4:23 PM Chase Peeler >>> wrote: >>> >>>> I brought this up on another thread, but it wasn't addressed (which is >>>> fine, since it was somewhat off-topic). I thought it might be >>>> worth bringing up in its own thread, though. >>>> >>>> In the other thread, someone had mentioned the following compiler >>>> optimization >>>> >>>> foreach(\array_keys($arr) as $key) { >>>> >>>> and quietly transform that into: >>>> >>>> foreach ($arr as $key => >>>> $_unusedVariableNameThatIsntEvenSpilledToTheScope) >>>> { >>>> >>>> I would be more likely to write: >>>> $keys = array_keys($arr); >>>> foreach($keys as $key){ >>>> Which would prevent me from being able to take advantage of the >>>> optimization. >>>> >>>> So, what I was wondering, is if there are other optimizations I might be >>>> missing out on, and if so, are they documented anywhere? >>>> >>>> >>>> -- >>>> Chase Peeler >>>> chasepeeler@gmail.com >>>> >>> >> >> -- >> Chase Peeler >> chasepeeler@gmail.com >> > -- Chase Peeler chasepeeler@gmail.com --00000000000079dfad05af5a9d18--