Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119577 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 49032 invoked from network); 19 Feb 2023 10:56:26 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Feb 2023 10:56:26 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EAEBA180083 for ; Sun, 19 Feb 2023 02:56:24 -0800 (PST) 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,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f49.google.com (mail-wr1-f49.google.com [209.85.221.49]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 19 Feb 2023 02:56:23 -0800 (PST) Received: by mail-wr1-f49.google.com with SMTP id t6so355587wrz.7 for ; Sun, 19 Feb 2023 02:56:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=4WPKVk5B/cMlxyQUQIDS/pHRx0bzdU/0PIHnzIEBiuM=; b=YK8Sb8s2LEBXv+0gchbNvrCMjjMq7lujxf5Ur7eMksA9dHVZ3BZeJUCXPtIKTnk6W/ O6e76CCWPd/haE6KVPqrWXu3c2NvkBMBUxWJcfMAf6VhNke1tejpYhpa8N9e7Wf3S0yY brwNM7uZMACQP4I3vug/kyr1Z7Xzv6uVpoMoTTpn0QhpZWtMPidHhpcYZ4SIqTfYdTd9 +o9yth+gPLw89n7xwnN2gEpTVqi1IIOa5jz9R8tMS5OzuTnpW2QQO85oRxhLZcR5j+3l QZdzzqzesZ2hjAmQZEqByaf/DBdSoH6q3zP2FZCTnU80iS9WxQXzDDWVX5F6vbaj2kb2 4YAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4WPKVk5B/cMlxyQUQIDS/pHRx0bzdU/0PIHnzIEBiuM=; b=J6L97VzBQWijZ/Z5b8V7P9WBLiZXE5zRd4vaNxHwlhgDS/NnF/e8STeCUhL+z4TTYg 8DDS3sPiRBU6A9bpE8QP1vDeVrt/n8C6bgs7UofyX7AWcdVyQWZrYEV9e8VgFpgdM4aX GCnrl3oe2BjjNPER3mOAKzhxLq3aTfz/L66bguKwwk91DIOZhA+KQQc5J9t2QJCO5zDL UDu02SzfSLRpgzMuDOfFr5CL0M+nNGFTIYhjhtq4l8ZhiByoN/I6jyVbvQOCPi+1F3Pj 94sOWxo9wU2Z/Z9o7reTSBVdPTHjvZy0wNZUyJvOtP6BxRZmKyE1JCuoUUzeIzZjm/gc Ja9A== X-Gm-Message-State: AO0yUKUoZcbXKLddP0gdBQI4eA0/KxFdQfHoPabckmIMghM0EFRUpC4g LEdSQ31OdOYBJhfyttvHZVI= X-Google-Smtp-Source: AK7set9A2o+wrJZPnPSwq1E8Jzr/Jpf9ePWJiXI5t++4d8NT3gr7B4Jn1SXbeU0BbyvnUJPca3og1A== X-Received: by 2002:a5d:5646:0:b0:2c5:58aa:35cb with SMTP id j6-20020a5d5646000000b002c558aa35cbmr1826442wrw.47.1676804182223; Sun, 19 Feb 2023 02:56:22 -0800 (PST) Received: from [192.168.0.59] (178-117-137-225.access.telenet.be. [178.117.137.225]) by smtp.gmail.com with ESMTPSA id f18-20020adff992000000b002c5a6f4aa53sm7383532wrr.105.2023.02.19.02.56.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 19 Feb 2023 02:56:21 -0800 (PST) Message-ID: Date: Sun, 19 Feb 2023 11:56:20 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0 Content-Language: en-US To: Max Kellermann , Nikita Popov Cc: internals@lists.php.net References: <478d639f-9f09-4747-a21a-eb4bceb5c053@app.fastmail.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] What's the purpose of zend_result? From: dossche.niels@gmail.com (Niels Dossche) On 2/19/23 11:32, Max Kellermann wrote: > On 2023/02/19 09:45, Nikita Popov wrote: >> I expect that there are two main reasons for that: >> - There are probably some places that return a (non-negative) value or FAILURE. >> - There are probably some places that check for success/failure using >= 0 and < 0. Another POSIX-ism. >> >> I don't think we endorse such usage, but it likely exists. > > Oh. That's .... bad. > > (Again, with C++'s strictly-typed enums, this would be easy to find: > if it compiles, it's correct.) > >> Let me turn the question around: Is there some reason to change the value of FAILURE at this point? > > Right now, I'm trying to understand this choice of number and the > obscure code comment NOT explaining it. A comprehensive understanding > of a design is an important first step for any other decision. > > And indeed there would be a good practical reason to change this. For > example, on x86, the check for -1 compiles to: > > 83 f8 ff cmp $0xffffffff,%eax > > While the check for false (or 0) compiles to: > > 85 c0 test %eax,%eax > > On Aarch64, check for -1 is: > > 3100041f cmn w0, #0x1 > 54000060 b.eq 1c > > While false/0 compile to just one conditional branch: > > 35000060 cbnz w0, 3c > > For such a basic definition used everywhere, this does make a > difference Just chiming in real quickly here for two small notes... We could work around this issue by checking if (function() != SUCCESS) instead of if (function() == FAILURE). Not sure if it gives any measurable improvement though. It's also worth noting that there's a couple of places where there's just a check for if (function()) { failure code }. i.e. there is no check for == FAILURE, it's just "implied". So if there would be changes to their values, this needs to be taken into account or fixed. Although there might be 3rd party extensions doing this kind of code as well. > > It was rather clumsy to choose -1 for FAILURE because it leads to > less-than-optimal machine code. Larger machine code means slower > execution. For one central basic definition as zend_result that gets > used everywhere, it can make a measurable difference. > > (Note that using "bool" would still be faster than using an enum with > 0 and 1, but everything's better than -1.) > >>> If I understand the guideline correctly, then those examples (and >>> countless others) are defective and should be fixed, correct? >> >> At least in principle, yes. Of course, actually doing a change may not always be worthwhile, especially as this might result in a silent behavior change for extensions (as putting the return value into an "if" would invert behavior now). > > (... and yet again, C++ would have saved us from this pain by not > allowing implicit bool casts - sorry for annoying you with my C++ > fanboyism, but any pain that stems from choosing C is self-inflicted.) > > The zend_fibers thing is a fairly recent addition, yet nobody spotted > this API mistake befor merging it. I've submitted a PR to fix it: > https://github.com/php/php-src/pull/10622 > > Surprisingly, CODING_STANDARDS.md doesn't mention this API convention. > Maybe it should? > > Max >