public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/53097] New: [c++0x] Missed optimization: lambda closure object could be smaller
@ 2012-04-24  0:59 luto at mit dot edu
  2012-04-24  1:46 ` [Bug c++/53097] " pinskia at gcc dot gnu.org
  2014-05-21 17:36 ` shachar at shemesh dot biz
  0 siblings, 2 replies; 3+ messages in thread
From: luto at mit dot edu @ 2012-04-24  0:59 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53097

             Bug #: 53097
           Summary: [c++0x] Missed optimization: lambda closure object
                    could be smaller
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: luto@mit.edu


This code:

include <stdio.h>

int main(int argc, char **argv)
{
  int a, b;
  auto foo = [&](){return a + b;};
  printf("%d\n", (int)sizeof(foo));
  return 0;
}

prints 16 (on x86-64) on gcc 4.6 and something quite close to 4.7 at -O2 and
-Ofast.  This is as expected if the closure object is implemented as imagined
in the spec.

In this particular case, accessing a from the lambda is defined behavior iff
accessing b is defined (because either a and b are both in scope or both out of
scope, so the lambda could be optimized based on the knowledge that a and b are
at a fixed offset from each other.  This would give size 8.

(It sounds like this could be rather difficult.  clang++ 2.9 works the same way
as g++.  I don't really expect to see this optimization implemented anytime
soon.)


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

* [Bug c++/53097] [c++0x] Missed optimization: lambda closure object could be smaller
  2012-04-24  0:59 [Bug c++/53097] New: [c++0x] Missed optimization: lambda closure object could be smaller luto at mit dot edu
@ 2012-04-24  1:46 ` pinskia at gcc dot gnu.org
  2014-05-21 17:36 ` shachar at shemesh dot biz
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-04-24  1:46 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53097

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2012-04-24
     Ever Confirmed|0                           |1
           Severity|normal                      |enhancement

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-04-24 01:45:51 UTC ---
The easy way to implement this would be something like:
include <stdio.h>

int main(int argc, char **argv)
{
  struct {
  int a, b;
  };
  auto foo = [&](){return a + b;};
  printf("%d\n", (int)sizeof(foo));
  return 0;
}

and then only the pointer to the struct needs to be done for the closure.


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

* [Bug c++/53097] [c++0x] Missed optimization: lambda closure object could be smaller
  2012-04-24  0:59 [Bug c++/53097] New: [c++0x] Missed optimization: lambda closure object could be smaller luto at mit dot edu
  2012-04-24  1:46 ` [Bug c++/53097] " pinskia at gcc dot gnu.org
@ 2014-05-21 17:36 ` shachar at shemesh dot biz
  1 sibling, 0 replies; 3+ messages in thread
From: shachar at shemesh dot biz @ 2014-05-21 17:36 UTC (permalink / raw)
  To: gcc-bugs

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

Shachar Shemesh <shachar at shemesh dot biz> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |shachar at shemesh dot biz

--- Comment #2 from Shachar Shemesh <shachar at shemesh dot biz> ---
I should point out that I found this bug because I came here to file the exact
same one.

I actually don't think this is so difficult. In cases of pass by reference and
local variables use, just pass the frame pointer, and use the same frame
pointer relative offsets you would in the main function also in the lambda.

Shachar


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

end of thread, other threads:[~2014-05-21 17:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-04-24  0:59 [Bug c++/53097] New: [c++0x] Missed optimization: lambda closure object could be smaller luto at mit dot edu
2012-04-24  1:46 ` [Bug c++/53097] " pinskia at gcc dot gnu.org
2014-05-21 17:36 ` shachar at shemesh dot biz

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).