Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115282 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 99459 invoked from network); 3 Jul 2021 13:42:40 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jul 2021 13:42:40 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CEEC918055F for ; Sat, 3 Jul 2021 07:03:40 -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=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, 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-lf1-f50.google.com (mail-lf1-f50.google.com [209.85.167.50]) (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 ; Sat, 3 Jul 2021 07:03:40 -0700 (PDT) Received: by mail-lf1-f50.google.com with SMTP id c28so224026lfp.11 for ; Sat, 03 Jul 2021 07:03:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:content-transfer-encoding:mime-version:subject:date:references :to:in-reply-to:message-id; bh=1nviXGQp8Wg1R2RBODjckwLtGwLuQFm/o6o0Y/shIsU=; b=tTvWM7G7mLoIqmQAc4id1wIP/a9FrgBIcFticn5V1/fJdBt9sxtR6ZBQQwo9XYh7TZ 5r/jqMRIrsSzF2YQxHUEHSfrQkvuhgF7QRJIM+Zod0YbQT2nKQWwWPSoKf46et5DLhDP tj+AJ49Uua6P8JzA1uRrTR10zRNaI9FrtwIDqeRGh13hdkF0syFltXwD0LLryyq9S+4b b6th+uzT4b2PtHDbkmqBpXbM4jpYlKCgV9WmieZUEWLkT6/oHsz8iQRDlh+1k6IjlzQN gSrbkr1OrL67Lg5MhulzV5K63VFSGreM3FReqoDr0oWpvxVRMA9SFGgBRztWhz1mnXTQ K6dQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:content-transfer-encoding:mime-version :subject:date:references:to:in-reply-to:message-id; bh=1nviXGQp8Wg1R2RBODjckwLtGwLuQFm/o6o0Y/shIsU=; b=db2RHjNCQlXthVDYqk//6sf0HjV2uj3eYnh/Cee/TWkkJlk1JTVzYueasHKJ5hKjFy P+wbldHhvQHzFTFONh5NqVP3bUuAk8/Vo/Ljk1zCin/DixQCjTigjRejDrkD3C9HD0sR qRLUzcCaBP7lISCziZwepYnA56RLOl0GJ4q8lKm46lsqhqUzRpGOKY6Ka3bxvG4nO7j1 0Eo5svNc5NKYUKVpUQh1UNFPMC0e98PlZp1mcs+wU0ExSZiHvUoHIQgklFSgknV2a/cE oRtdE3Z+zq1+qaclswXz8/Rz0SqX6nqO/Whtwh8d08t68Gp7xwVx+EavYcYFQoMJStuL qHLw== X-Gm-Message-State: AOAM5323hG3D8cjbnL8FREpJVvU+lgJTxvdCsJF+9GlV0+Kltr78hD/m 8epJcK+8Dfc+kZCHHYUajONUA+cabw+Fnw== X-Google-Smtp-Source: ABdhPJyHfD4tuCzGb3eDnVwBoYHD77q2NprfdFYFsNfwad/ssVnEZGOGJy6IKMQ6cATBzpfAm4JiLg== X-Received: by 2002:a05:6512:1393:: with SMTP id p19mr3547886lfa.570.1625321016416; Sat, 03 Jul 2021 07:03:36 -0700 (PDT) Received: from [192.168.2.137] (81-233-163-178-no84.tbcn.telia.com. [81.233.163.178]) by smtp.gmail.com with ESMTPSA id z8sm553273lfh.119.2021.07.03.07.03.35 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 03 Jul 2021 07:03:36 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 11.5 \(3445.9.7\)) Date: Sat, 3 Jul 2021 16:03:34 +0200 References: <0BBAA2F7-02D7-4AEE-B48A-97436A0D8E5D@gmail.com> To: "internals@lists.php.net" In-Reply-To: Message-ID: X-Mailer: Apple Mail (2.3445.9.7) Subject: Re: [PHP-DEV] [RFC] clamp From: hallbergkim@gmail.com (Kim Hallberg) > On 24 Jun 2021, at 1:50 AM, tyson andre = wrote: >=20 > So I think the major objections are that: >=20 > 1. This is easy to implement in userland and there's negligible = performance benefit > (in code that is a performance sensitive loop, it may still be worse = compared to `$num < $min ? $min : ($num > $max : $max : $num)` when = validity of types and min/max are known, especially with the JIT) > 2. People not being sure if they'd ever use it personally, especially = with the ability to customize parameter order and permitted argument = types in userland > 3. It's inconsistent with min() and max(), which support any = comparable type such as GMP(https://www.php.net/manual/en/class.gmp) > (arbitrary precision numbers), DateTime, etc., and that may lead to = surprises. > Although PHP's comparison operator and = https://www.php.net/manual/en/function.min.php have many, many = inconsistencies, already >=20 > (Though special casing GMP is probably a bad idea due to it being = optional and having an incompatible license with core for packagers) > 4. I'm not sure what this is meant to do with the float NAN (Not A = Number) from the RFC description, but that's solvable > On 24 Jun 2021, at 2:15 AM, tyson andre = wrote: >=20 > I'd strongly prefer an actual benchmark for context and accuracy - is = it actually faster or slower than the most efficient userland = implementation and by how much? > E.g. in an optimized NTS build with `CFLAGS=3D-O2`, opcache = enabled(zend_extension=3Dopcache, = opcache.enable=3D1,opcache.enable_cli=3D1), > and no debug configure flags, how many calls per second can be made on = variable values of $num for both? > (I'd assume over twice as fast as calling both min/max from another = function, but possibly slower than efficient_clamp, but haven't run = this) >=20 > For userland implementations that did use min/max, they probably = weren't performance sensitive for the application. >=20 > ```php > function efficient_clamp(int|float $num, int|float $min, int|float = $max): int|float { > return $num < $min ? $min : ($num > $max ? $max : $num); > } > ``` Will combine both emails into one here. Will tackle your first point = since that has to with performance as your second email also does. Haven=E2=80=99t tested it with opcache enabled so cannot comment on the = performance, but for my general testing the internal version is more = than 50% faster than the min/max version. Slower than the = =E2=80=9Cefficient=E2=80=9D version in some cases and faster in others. = That is to be expected though since the efficient version doesn=E2=80=99t = check for a valid range, if a user mixes up the min and max values the = efficient clamp function doesn=E2=80=99t work as intended and will = always return either the min/max version and never the clamped value. For your second point, that could be said for most internal functions, = `str_contains`, `str_starts_with` and `str_ends_with` could all three = be implemented in userland with customises parameter orders, as can most = other internals function. Frankly I don=E2=80=99t see this as much of an = objection or issue as a phrase used to gate keep what can become a new = standardised internal function. Thirdly, yes I will agree it=E2=80=99s inconsistent with min/max = functions. That is mainly - as you stated, min/max takes mixed arguments = and works with all comparable types. Though it technically isn=E2=80=99t = inconsistent with most userland implementations as they mostly accept = numerical values such as ints or floats into their clamp function, = meaning only ints and floats are used anyway. Though I could see a version of clamping being useful in the = `DatePeriod` class to check if a `DateTime` is in range of a certain = date range, but that=E2=80=99s OT for this RFC so. As for your last and forth point, I=E2=80=99ll be updating the RFC with = NaN behaviour, sticking with what Nikita suggested to throw when NaN is = added to min or max, and return NaN is it=E2=80=99s added to value. Thanks, Kim.