Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:128532 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 823EE1A00BC for ; Fri, 22 Aug 2025 17:30:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1755883706; bh=BjmUyrSJTmgQYIVsfzjWRkDSNzp4iGS7Ov8T9babsso=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=ZINnFADEnDTuxR9OJe8SFmCycFteUGYuOzWmA4+/6n9bbhR6Rsk/5nuMzP1ZiTwJ2 a7Qf+i+P78Aj6vZTkVJ2cKhsOwTHomslNY7tM1bfkHXOqwLQvvRmFGEhjMp4xiE20Y tGavl3sKV6F7s2dJAPvCzcZ6cUnXKy34r54akjQquoT64hh4IA8wjJnyjT01r89YR6 wxulN2ecgttfZLtVmGUWk3zgrxE9vZchTpH4PXREAFCBBk3MDenYXDUyE1kBghD5hd 4DzvbAtC3X4P+YQwuMf+E3I3M+0PRw9igDZUuQs/Q5l4wJJeg5ZY/l7qtbDxhk1ryU 1SBKQRubBKhOQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 384A218005D for ; Fri, 22 Aug 2025 17:28:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) 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,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 22 Aug 2025 17:28:24 +0000 (UTC) Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-245fd2b63f8so23720915ad.3 for ; Fri, 22 Aug 2025 10:29:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1755883798; x=1756488598; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=vnCStFaeI3wy1J+Y1V2ZYWgtQefwp/eoDCJzG+cwpes=; b=nOVdTE0GEQyBWcsk8FyagJCjxoBmNmVh1lDfEQtmJzHK0H0M7wmaQjdDuYGuRC7TX8 WdQCaBZNaso5NQlT75oR09o6yk3f/Z2r9KbfJHU0qIlLzYlY+Lc9aBkc/kVggcrknSSk FEFeGZNoq+DOdxbZ2kh/bkWP+kxxAlRAD0mI0YtJUNF8Xnu3FZabM3vFPZpb7vulcoHe m3MnsgRQMBcHlCw3PyB3nURSF0HaWdJLFzKlmuwqjh2WSBFoAwSKB0YwZeUMGTOvPzfW do/HNNCvQLiYp3MiiTu3bR+71PABtgOXhGSETtnOjPlUQYmDARO+gqq6bHP36WZfCo2C a2Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755883798; x=1756488598; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=vnCStFaeI3wy1J+Y1V2ZYWgtQefwp/eoDCJzG+cwpes=; b=olAahDG4zCP3VmGVbivf43ClpBFYUDL1dhrGRFmbo0P2W3cCEc4SnDif3bvl1eCUsA 1psLWiL9YPLSERVN2bkEHI2+2sTYqN5cPdci7YxeSNbGgOk7MY60c5zvyIHuZBvgFu2j Ybsx4pDFUDuDSNeX5oVE1LxEvgbSC+Ll1GErO4Gc44A6x8dEYyLWeU2bqrI13E+KxCpz xwdpNwFPxCcS+Yow498MKgMx3eJouWMllygHoOwbqJsxoHh2vOX4WU94eIHPIaf8Lx/t teFCiT3tnOUKdyY5crFuMNiFgRvdpqIzdGfKk0aUEMzuFln4WT7HMmshQszgDTbJgibc QKvg== X-Gm-Message-State: AOJu0Yyo28vEf5oCroUl6IFVkNGL8wmh+XlTLUynAVEJNcYsqhcDmyPR wQ0XqhrRrma9ji8Vt9rqahHMdyVktbf/+sFBKY/xEy/fEs6OqYTlJ+liz0d07HWx0F2f0KtvkRT smhoiRSuO9UdIH7YSgHtSiXzN4JaZ3lUEMYD5l6UCPg== X-Gm-Gg: ASbGncs1F5lPpTwwk8/2UQgwmdhgu48eLEjTkxd9rZnuhsP1IJEhJME7F8Uc1xUQoUW t/UteiOGox07jKwLuLXl8CkE1kDfG3xcl55X1gYqT9mwvgKHxPRmFmb+zQrbOlBgbL9QLdXMhen UY3CXJGi+5NPhVkOD5eWK8HD7iaF+BoxZnDFvsCT3jwVckqb42CfyFnprUdQC7xqf+bX11pIIvj cg0gx8N X-Google-Smtp-Source: AGHT+IGjTfuAkbNRx0g5EWtNauXkwCBQtgo1LYN9ls9YYbVEdkK7+FIv7ZLaf68+jWZd7qa7RrFYEnvxKfXrZ+EG5DE= X-Received: by 2002:a17:902:fc47:b0:234:aa9a:9e0f with SMTP id d9443c01a7336-2462ee7b309mr58606605ad.23.1755883797588; Fri, 22 Aug 2025 10:29:57 -0700 (PDT) Precedence: list list-help: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 References: <0027ab13-89fb-40b4-991b-2f88a35c2f31@gmx.de> <011001dc1358$287f1860$797d4920$@glaive.pro> In-Reply-To: <011001dc1358$287f1860$797d4920$@glaive.pro> Date: Fri, 22 Aug 2025 19:29:46 +0200 X-Gm-Features: Ac12FXzvu_ay2K_FNmBoW4B6T-r_IG3I9ismU9j0HUcpn4EVH6NQ1xPUXZG3S-I Message-ID: Subject: Re: [PHP-DEV] [RFC] Add clamp function To: Juris Evertovskis Cc: PHP Internals Content-Type: multipart/alternative; boundary="00000000000005b01c063cf78e75" From: kylekatarnls@gmail.com (Kyle Katarn) --00000000000005b01c063cf78e75 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 2025-08-22 at 13:30, Juris Evertovskis wrote: > *From:* Kyle Katarn > *Sent:* Friday, August 22, 2025 11:02 AM > *To:* Christoph M. Becker > *Cc:* Tim D=C3=BCsterhus ; PHP Internals < > internals@lists.php.net> > *Subject:* Re: [PHP-DEV] [RFC] Add clamp function > > > I created https://wiki.php.net/rfc/clamp_v2 (re-using most of the info > from https://wiki.php.net/rfc/clamp). > > Hey, > > > > I=E2=80=99m not against adding such a function, but I noticed that the RF= C says > > > > > some of which use min > and max to check the > bound, which is costly and slow when called often > > > > What makes them slow? If we can create a significantly faster `clamp`, ca= n > we use the same optimizations to improve min and max? > > > > BR, > > Juris > I taked it from https://wiki.php.net/rfc/clamp and from what I understand reading the code: min and max support variadic array of parameters, or a single array value, so min($max, max($min, $value)) will result in 2 loops, plus if-else paths depending on the type. While the current implementation of clamp accepts only 1 value, 1 min and 1 max, so it just does $value < $min ? $min : $value > $max ? $max : $value, which is way less work. Also from my point of view, min($max, max($min, $value)) has a counter-intuitive reading when used to clamp a value. --00000000000005b01c063cf78e75 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

2025-08-22 at 13:30= , Juris Evertovskis <juris@glaive.pr= o> wrote:

From:= Kyle Katarn = <kylekatarnl= s@gmail.com>
Sent: Friday, August 22, 2025 11:02 AM
To: Christoph M. Becker <cmbecker69@gmx.de>
Cc: Tim D=C3=BCsterhus <= ;tim@bastelstu.be= >; PHP Internals <internals@lists.php.net>
Subject: Re: [PHP-DEV= ] [RFC] Add clamp function


> I created=C2=A0https://wiki.php.net/rfc/= clamp_v2 (re-using most of the info from=C2=A0https://wiki.php.net/rfc/clamp).
Hey,

=C2=A0

I=E2=80=99m not aga= inst adding such a function, but I noticed that the RFC says<= /span>

=C2= =A0

= > some of which use=C2=A0min=C2=A0and=C2=A0max=C2=A0to check the bound, which is costly and slow= when called often

=C2=A0

What makes them slow? If we can create a si= gnificantly faster `clamp`, can we use the same optimizations to improve mi= n and max?

=C2=A0

BR,

= Juris


I taked it from=C2=A0https://wiki.php.net/rfc/clamp and from what I unders= tand reading the code: min and max support variadic array of parameters, or= a single array value, so min($max, max($min, $value)) will result in 2 loo= ps, plus if-else paths depending on the type. While the current implementat= ion of clamp accepts only 1 value, 1 min and 1 max, so it just does $value = < $min ? $min : $value > $max ? $max : $value, which is way less work= .

Also from my point of view,=C2=A0 min($max, max(= $min, $value)) has a counter-intuitive reading when used to clamp a value.<= /div>
--00000000000005b01c063cf78e75--