public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
[parent not found: <bug-21046-10391@http.gcc.gnu.org/bugzilla/>]
* [Bug tree-optimization/21046] New: move memory allocation out of a loop
@ 2005-04-15 12:42 tkoenig at gcc dot gnu dot org
  2005-04-15 13:04 ` [Bug tree-optimization/21046] " giovannibajo at libero dot it
  2005-04-15 13:37 ` pinskia at gcc dot gnu dot org
  0 siblings, 2 replies; 6+ messages in thread
From: tkoenig at gcc dot gnu dot org @ 2005-04-15 12:42 UTC (permalink / raw)
  To: gcc-bugs

Consider the following program fragment:

  char *p;
  int i;

  for (i=0; i<10; i++)
    {
      p = malloc(20);
      foo(p,i);
      free(p);
    }
}

The loop could be simplified into

  p = malloc(20);
  for (i=0; i<10; i++)
    foo(p,i);

  free(p);

This would reduce the overhead for memory allocation
considerably.

A more challenging case is to change

  for (i=0; i<10; i++)
    {
      p = malloc(2*i+2);
      foo(p,i);
      free(p);
    }

into

  p = malloc(20);
  for (i=0; i<10; i++)
      foo(p,i);

  free(p);

because the amount of memory allocated has an upper bound.

This is probably not a big win for straight C code.  For languages
which generate temporary arrays at runtime, such as Fortran, it
could mean a significant reduction in memory management overhead.

-- 
           Summary: move memory allocation out of a loop
           Product: gcc
           Version: 4.1.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P2
         Component: tree-optimization
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: tkoenig at gcc dot gnu dot org
                CC: gcc-bugs at gcc dot gnu dot org


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


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

end of thread, other threads:[~2020-11-11  9:22 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-21046-4@http.gcc.gnu.org/bugzilla/>
2012-02-22 12:49 ` [Bug tree-optimization/21046] move memory allocation out of a loop xiaoyuanbo at yeah dot net
2014-12-25 12:16 ` tkoenig at gcc dot gnu.org
2020-11-11  9:22 ` tkoenig at gcc dot gnu.org
     [not found] <bug-21046-10391@http.gcc.gnu.org/bugzilla/>
2010-04-29  9:33 ` rguenth at gcc dot gnu dot org
2005-04-15 12:42 [Bug tree-optimization/21046] New: " tkoenig at gcc dot gnu dot org
2005-04-15 13:04 ` [Bug tree-optimization/21046] " giovannibajo at libero dot it
2005-04-15 13:37 ` pinskia at gcc dot gnu dot org

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