Date: Fri, 23 May 2025 18:31:58 +0000
On Thursday, May 22nd, 2025 at 9:56 PM, Jeremy Rifkin <rifkin.jer_at_[hidden]> wrote:
> How do you want the compiler to optimize this for you?
It should compile the `switch` body without
cases, label them with cases, record those
addresses, form a hash table in compiled
binary by mapping from string literal to the
addresses. Let `e` in `switch (e)` be the key
to lookup the table, jump to the corresponding
value of the key as if using a computed goto
Labels as Values (Using the GNU Compiler Collection (GCC))
>
> Also note that focusing only on fallthrough isn’t very representative as most switch usage does not involve case fallthrough like this.
Business code can have lots of and lots of
if...else chains comparing string. Sometimes,
because of a lack of fall-through, you see code
that just repeats. Fall-through is a stigmatized
name for a feature that prefixes a branch with
additional entry and processing. When you need
it, it expresses exactly the intent.
> Jeremy
>
> On Thu, May 22, 2025 at 21:56 Zhihao Yuan <zy_at_[hidden]> wrote:
>
> > On Thursday, May 22nd, 2025 at 1:07 PM, Jeremy Rifkin via Std-Proposals <std-proposals_at_[hidden]> wrote:
> >
> > > > Wow, if you believe that C++ must care> about performance, then you may want
> > > > to consider supporting a `switch` that
> > > > tests strings!
> > >
> > > There is no optimization that is unique to switch. Compilers optimize if-else chains just fine.
> >
> >
> > I want to write
> >
> > switch (foo()) {
> > case "old-label":
> > migrate();
> > case "new-label":
> > arg &= flag;
> > work1(arg);
> > break;
> > case "second-case":
> > work2(arg);
> > }
> >
> > Please show me how to express this in if...else,
> > and which compiler optimizes it fine.
> >
> > --
> > Zhihao Yuan, ID lichray
> > The best way to predict the future is to invent it.
> > _______________________________________________
> >
> >
> >
> > > Jeremy
> > >
> > >
> > > On Thu, May 22, 2025 at 1:45 PM Zhihao Yuan via Std-Proposals <std-proposals_at_[hidden]> wrote:
> > >
> > > > On Thursday, May 22nd, 2025 at 10:53 AM, Nikolay Mihaylov via Std-Proposals <std-proposals_at_[hidden]> wrote:
> > > >
> > > > > My 5 cents again :)
> > > > >
> > > > > I am strongly against modifying switch in this way :)
> > > > > C++ is C and C++, is not Javascript or PHP where nobody cares about performance.
> > > >
> > > >
> > > > Wow, if you believe that C++ must care
> > > > about performance, then you may want
> > > > to consider supporting a `switch` that
> > > > tests strings! What a compiler can do to
> > > > optimize a pattern matching that tests
> > > > strings but simulates fall-through between
> > > > the cases?
> > > >
> > > > --
> > > > Zhihao Yuan, ID lichray
> > > > The best way to predict the future is to invent it.
> > > > _______________________________________________
> > > >
> > > >
> > > > >
> > > > > This is exactly why match were proposed.
> > > > >
> > > > > On Wed, May 21, 2025 at 4:56 PM Jason McKesson via Std-Proposals <std-proposals_at_[hidden]> wrote:
> > > > >
> > > > > > On Wed, May 21, 2025 at 3:34 AM Filip via Std-Proposals
> > > > > > <std-proposals_at_[hidden]> wrote:
> > > > > > >
> > > > > > > I agree, it seems like a better idea to have switch in non constexpr context available, to act like a nicer option acting like if else.
> > > > > > >
> > > > > > > Maybe I’m missing the key functionality of match, but it looks like a different syntax for something that we already have.
> > > > > > >
> > > > > > > I agree that assignment with match looks like a good idea, why wouldn’t we add that to the switch statement?
> > > > > >
> > > > > > Because we're getting pattern matching anyway, which is a superset of
> > > > > > what switch can do. There's no point in improving a legacy feature
> > > > > > when it is simultaneously being rendered obsolete.
> > > > > >
> > > > > > > string b = “hello”;
> > > > > > > auto var = switch(b){
> > > > > > > case “hi”: { return 42; }
> > > > > > > case “hello”: { return 43; }
> > > > > > > default: { return 0; } // probably should be mandatory
> > > > > > > };
> > > > > >
> > > > > > ```
> > > > > > b match {
> > > > > > "hi" => return 42;
> > > > > > "hello" => return 43;
> > > > > > _ => return 0;
> > > > > > }
> > > > > > ```
> > > > > > --
> > > > > > Std-Proposals mailing list
> > > > > > Std-Proposals_at_[hidden]
> > > > > > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> > > >
> > > > --
> > > > Std-Proposals mailing list
> > > > Std-Proposals_at_[hidden]
> > > > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> How do you want the compiler to optimize this for you?
It should compile the `switch` body without
cases, label them with cases, record those
addresses, form a hash table in compiled
binary by mapping from string literal to the
addresses. Let `e` in `switch (e)` be the key
to lookup the table, jump to the corresponding
value of the key as if using a computed goto
Labels as Values (Using the GNU Compiler Collection (GCC))
>
> Also note that focusing only on fallthrough isn’t very representative as most switch usage does not involve case fallthrough like this.
Business code can have lots of and lots of
if...else chains comparing string. Sometimes,
because of a lack of fall-through, you see code
that just repeats. Fall-through is a stigmatized
name for a feature that prefixes a branch with
additional entry and processing. When you need
it, it expresses exactly the intent.
> Jeremy
>
> On Thu, May 22, 2025 at 21:56 Zhihao Yuan <zy_at_[hidden]> wrote:
>
> > On Thursday, May 22nd, 2025 at 1:07 PM, Jeremy Rifkin via Std-Proposals <std-proposals_at_[hidden]> wrote:
> >
> > > > Wow, if you believe that C++ must care> about performance, then you may want
> > > > to consider supporting a `switch` that
> > > > tests strings!
> > >
> > > There is no optimization that is unique to switch. Compilers optimize if-else chains just fine.
> >
> >
> > I want to write
> >
> > switch (foo()) {
> > case "old-label":
> > migrate();
> > case "new-label":
> > arg &= flag;
> > work1(arg);
> > break;
> > case "second-case":
> > work2(arg);
> > }
> >
> > Please show me how to express this in if...else,
> > and which compiler optimizes it fine.
> >
> > --
> > Zhihao Yuan, ID lichray
> > The best way to predict the future is to invent it.
> > _______________________________________________
> >
> >
> >
> > > Jeremy
> > >
> > >
> > > On Thu, May 22, 2025 at 1:45 PM Zhihao Yuan via Std-Proposals <std-proposals_at_[hidden]> wrote:
> > >
> > > > On Thursday, May 22nd, 2025 at 10:53 AM, Nikolay Mihaylov via Std-Proposals <std-proposals_at_[hidden]> wrote:
> > > >
> > > > > My 5 cents again :)
> > > > >
> > > > > I am strongly against modifying switch in this way :)
> > > > > C++ is C and C++, is not Javascript or PHP where nobody cares about performance.
> > > >
> > > >
> > > > Wow, if you believe that C++ must care
> > > > about performance, then you may want
> > > > to consider supporting a `switch` that
> > > > tests strings! What a compiler can do to
> > > > optimize a pattern matching that tests
> > > > strings but simulates fall-through between
> > > > the cases?
> > > >
> > > > --
> > > > Zhihao Yuan, ID lichray
> > > > The best way to predict the future is to invent it.
> > > > _______________________________________________
> > > >
> > > >
> > > > >
> > > > > This is exactly why match were proposed.
> > > > >
> > > > > On Wed, May 21, 2025 at 4:56 PM Jason McKesson via Std-Proposals <std-proposals_at_[hidden]> wrote:
> > > > >
> > > > > > On Wed, May 21, 2025 at 3:34 AM Filip via Std-Proposals
> > > > > > <std-proposals_at_[hidden]> wrote:
> > > > > > >
> > > > > > > I agree, it seems like a better idea to have switch in non constexpr context available, to act like a nicer option acting like if else.
> > > > > > >
> > > > > > > Maybe I’m missing the key functionality of match, but it looks like a different syntax for something that we already have.
> > > > > > >
> > > > > > > I agree that assignment with match looks like a good idea, why wouldn’t we add that to the switch statement?
> > > > > >
> > > > > > Because we're getting pattern matching anyway, which is a superset of
> > > > > > what switch can do. There's no point in improving a legacy feature
> > > > > > when it is simultaneously being rendered obsolete.
> > > > > >
> > > > > > > string b = “hello”;
> > > > > > > auto var = switch(b){
> > > > > > > case “hi”: { return 42; }
> > > > > > > case “hello”: { return 43; }
> > > > > > > default: { return 0; } // probably should be mandatory
> > > > > > > };
> > > > > >
> > > > > > ```
> > > > > > b match {
> > > > > > "hi" => return 42;
> > > > > > "hello" => return 43;
> > > > > > _ => return 0;
> > > > > > }
> > > > > > ```
> > > > > > --
> > > > > > Std-Proposals mailing list
> > > > > > Std-Proposals_at_[hidden]
> > > > > > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> > > >
> > > > --
> > > > Std-Proposals mailing list
> > > > Std-Proposals_at_[hidden]
> > > > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Received on 2025-05-23 18:32:08