public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/92559] Returning std∷map breaks tail-recursion optimization
       [not found] <bug-92559-4@http.gcc.gnu.org/bugzilla/>
@ 2021-07-22 22:08 ` pinskia at gcc dot gnu.org
  2021-07-24  7:15 ` Hi-Angel at yandex dot ru
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-07-22 22:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92559

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I don't think this can ever be optimized.  Mainly because there are copies
happening due to passing via value and returning by value.

If I change it to foo to MyMap &foo(MyMap &m), I get the behavior you want.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/92559] Returning std∷map breaks tail-recursion optimization
       [not found] <bug-92559-4@http.gcc.gnu.org/bugzilla/>
  2021-07-22 22:08 ` [Bug c++/92559] Returning std∷map breaks tail-recursion optimization pinskia at gcc dot gnu.org
@ 2021-07-24  7:15 ` Hi-Angel at yandex dot ru
  2021-07-24 14:00 ` Hi-Angel at yandex dot ru
  2021-07-24 15:11 ` Hi-Angel at yandex dot ru
  3 siblings, 0 replies; 4+ messages in thread
From: Hi-Angel at yandex dot ru @ 2021-07-24  7:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92559

--- Comment #3 from Konstantin Kharlamov <Hi-Angel at yandex dot ru> ---
(In reply to Andrew Pinski from comment #2)
> I don't think this can ever be optimized.  Mainly because there are copies
> happening due to passing via value and returning by value.

Please correct me if I'm wrong, but it seems like, given the code in question:

    MyMap foo(MyMap m) {
        if (m[0] == 0)
            return m;
        else {
            m[0] -= 1;
            return foo(m);
        }
    }

When tail-recursion optimization applied, it should be analogous to this code:

    MyMap foo(MyMap m) {
        while(m[0] > 0) {
            m[0] -= 1;
            m = MyMap(m);
        }
        return m;
    }

Doesn't look impossible to me, unless of course I am missing something.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/92559] Returning std∷map breaks tail-recursion optimization
       [not found] <bug-92559-4@http.gcc.gnu.org/bugzilla/>
  2021-07-22 22:08 ` [Bug c++/92559] Returning std∷map breaks tail-recursion optimization pinskia at gcc dot gnu.org
  2021-07-24  7:15 ` Hi-Angel at yandex dot ru
@ 2021-07-24 14:00 ` Hi-Angel at yandex dot ru
  2021-07-24 15:11 ` Hi-Angel at yandex dot ru
  3 siblings, 0 replies; 4+ messages in thread
From: Hi-Angel at yandex dot ru @ 2021-07-24 14:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92559

--- Comment #4 from Konstantin Kharlamov <Hi-Angel at yandex dot ru> ---
By the way, FTR: I don't have the code anymore, but initially the problem came
from a real-life algorithm involving lots of state, which looked barely
readable when implemented in iterative way (i.e. as opposed to "recursive",
that is when you use for/while-cycles instead of recursion). The iterative
version was 1.5 times bigger than the recursive one, and has a lot of
additional state-tracking variables; and I think it also has two cycles (as
compared to recursive version that has none at all).

Back then we decided that we want the readable code, because at the end of the
day, readable code is the reason people don't write assembly and rely on
compilers to optimize instead. Of course, we would have to add to CI some
checks to make sure the optimization was done as expected (tho I don't think it
was ever added, simply because since the optimization currently doesn't work,
there's nothing to check).

IIRC, this bug-report was a reduced problem of tail-recursion not working for
us. The state in the algorithm has std::map in particular I think.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/92559] Returning std∷map breaks tail-recursion optimization
       [not found] <bug-92559-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2021-07-24 14:00 ` Hi-Angel at yandex dot ru
@ 2021-07-24 15:11 ` Hi-Angel at yandex dot ru
  3 siblings, 0 replies; 4+ messages in thread
From: Hi-Angel at yandex dot ru @ 2021-07-24 15:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92559

--- Comment #5 from Konstantin Kharlamov <Hi-Angel at yandex dot ru> ---
(In reply to Konstantin Kharlamov from comment #4)
> By the way, FTR: I don't have the code anymore, but initially the problem
> came from a real-life algorithm involving lots of state, which looked barely
> readable when implemented in iterative way (i.e. as opposed to "recursive",
> that is when you use for/while-cycles instead of recursion). The iterative
> version was 1.5 times bigger than the recursive one, and has a lot of
> additional state-tracking variables; and I think it also has two cycles (as
> compared to recursive version that has none at all).

Oh, I just remember: the recursive version was also immutable, which also made
the code much easier to understand (and I was kinda hoping, easier for the
compiler to optimize as well). The iterative version involved lots of
mutations. They always do.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-07-24 15:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-92559-4@http.gcc.gnu.org/bugzilla/>
2021-07-22 22:08 ` [Bug c++/92559] Returning std∷map breaks tail-recursion optimization pinskia at gcc dot gnu.org
2021-07-24  7:15 ` Hi-Angel at yandex dot ru
2021-07-24 14:00 ` Hi-Angel at yandex dot ru
2021-07-24 15:11 ` Hi-Angel at yandex dot ru

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).