Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #120414

Re: thread about the pros and cons of lambdas, but more about cons

From Michael S <already5chosen@yahoo.com>
Newsgroups comp.lang.c++
Subject Re: thread about the pros and cons of lambdas, but more about cons
Date 2024-09-27 16:55 +0300
Organization A noiseless patient Spider
Message-ID <20240927165507.00002a56@yahoo.com> (permalink)
References <20240925195423.00007ecc@yahoo.com> <871q17idqr.fsf@bsb.me.uk> <87v7yjgyfq.fsf@bsb.me.uk> <20240926104124.00007583@yahoo.com>

Show all headers | View raw


On Thu, 26 Sep 2024 10:41:24 +0300
Michael S <already5chosen@yahoo.com> wrote:

> On Wed, 25 Sep 2024 23:28:57 +0100
> Ben Bacarisse <ben@bsb.me.uk> wrote:
> 
> > Ben Bacarisse <ben@bsb.me.uk> writes:
> >   
> > > ... You can,
> > > instead, do this (untested):    
> > 
> > I should have tested!  Of course you have to change the capture (at
> > least for core) from '=' to '&':
> >   
> > >   #include <functional>
> > >    
> > >   int floodfill4(
> > >     unsigned char *grey,
> > >     int width, int height,
> > >     int x, int y,
> > >     unsigned char target, unsigned char dest)
> > >   {
> > >     if (width < 1 || height < 1)
> > >       return 0;
> > >     if (x < 0 || x >= width || y < 0 || y >= height)
> > >       return 0;
> > >    
> > >     size_t w = width, h = height;
> > >     if (grey[y*w+x] != target)
> > >       return 0;
> > >    
> > >     std::function<void(size_t, size_t)> core;
> > >     core = [&] (size_t x, size_t y) {    
> > Corrected-----^  
> > >       if (x >= w || y >= h)
> > >         return;
> > >       auto idx = y*w+x;
> > >       if (grey[idx] == target) {
> > >         grey[idx] = dest;
> > >         core(x - 1, y);
> > >         core(x + 1, y);
> > >         core(x, y - 1);
> > >         core(x, y + 1);
> > >       }
> > >     };
> > >     core(x, y);
> > >     return 1;
> > >   }    
> >   
> 
> That's where I started.
> It is approximately twice slower than tricky version with [&].
> And tricky [&] almost 20% slower than tricky [=].
> I didn't try to understand what std::function thing is, but fast it
> isn't.
> 
> 
> 

After digging a bit deeper I realized that slowness is only a symptom
of the bigger problem. This variant of lambda defeats the whole purpose
of having lambda/context, i.e. reduction of the size of call frame.
This variant of lambda generates call frame of exactly the same size as
straightforward code below.
I'd guess, it means that this variant of lambda tracks its captures
separately, instead of tracking them together via single pointer to
parent's stack frame.

#include <cstddef>

static void floodfill4u(
  unsigned char *grey,
  size_t width, size_t height,
  size_t x, size_t y,
  unsigned char target, unsigned char dest)
{
  if (x >= width || y >= height)
    return;
  auto idx = y*width+x;
  if (grey[idx] == target) {
    grey[idx] = dest;
    floodfill4u(grey, width, height, x - 1, y, target, dest);
    floodfill4u(grey, width, height, x + 1, y, target, dest);
    floodfill4u(grey, width, height, x, y - 1, target, dest);
    floodfill4u(grey, width, height, x, y + 1, target, dest);
  }
}


int floodfill4(
  unsigned char *grey,
  int width, int height,
  int x, int y,
  unsigned char target, unsigned char dest)
{
  if (width < 1 || height < 1)
    return 0;
  if (x < 0 || x >= width || y < 0 || y >= height)
    return 0;
  if (grey[(size_t)y*width+x] != target)
    return 0;
  floodfill4u(grey, width, height, x, y, target, dest);
  return 1;
}

Back to comp.lang.c++ | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-25 19:54 +0300
  Re: thread about the pros and cons of lambdas, but more about cons Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-25 19:17 +0200
  Re: thread about the pros and cons of lambdas, but more about cons Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-25 19:55 +0200
  Re: thread about the pros and cons of lambdas, but more about cons Paavo Helde <eesnimi@osa.pri.ee> - 2024-09-25 22:53 +0300
    Re: thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-25 23:30 +0300
      Re: thread about the pros and cons of lambdas, but more about cons Paavo Helde <eesnimi@osa.pri.ee> - 2024-09-26 00:04 +0300
        Re: thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-26 11:17 +0300
        Re: thread about the pros and cons of lambdas, but more about cons Paavo Helde <eesnimi@osa.pri.ee> - 2024-09-26 11:25 +0300
          Re: thread about the pros and cons of lambdas, but more about cons Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-26 10:28 +0200
            Re: thread about the pros and cons of lambdas, but more about cons Paavo Helde <eesnimi@osa.pri.ee> - 2024-09-26 11:49 +0300
          Re: thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-26 11:38 +0300
  Re: thread about the pros and cons of lambdas, but more about cons Ben Bacarisse <ben@bsb.me.uk> - 2024-09-25 23:13 +0100
    Re: thread about the pros and cons of lambdas, but more about cons Ben Bacarisse <ben@bsb.me.uk> - 2024-09-25 23:28 +0100
      Re: thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-26 10:41 +0300
        Re: thread about the pros and cons of lambdas, but more about cons David Brown <david.brown@hesbynett.no> - 2024-09-26 10:29 +0200
          Re: thread about the pros and cons of lambdas, but more about cons Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-26 11:35 +0200
            Re: thread about the pros and cons of lambdas, but more about cons David Brown <david.brown@hesbynett.no> - 2024-09-26 13:27 +0200
              Re: thread about the pros and cons of lambdas, but more about cons Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-26 13:31 +0200
                Re: thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-26 15:25 +0300
                Re: thread about the pros and cons of lambdas, but more about cons Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-26 14:58 +0200
                Re: thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-26 16:53 +0300
                Re: thread about the pros and cons of lambdas, but more about cons Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-26 15:54 +0200
        Re: thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-27 16:55 +0300
      Re: thread about the pros and cons of lambdas, but more about cons Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-26 10:01 -0700
        Re: thread about the pros and cons of lambdas, but more about cons Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-26 19:04 +0200
        Re: thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-26 20:20 +0300
          Re: thread about the pros and cons of lambdas, but more about cons Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-26 10:38 -0700
            Re: thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-27 16:27 +0300
              Re: thread about the pros and cons of lambdas, but more about cons Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-28 04:06 -0700
    Re: thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-26 10:58 +0300
    Re: thread about the pros and cons of lambdas, but more about cons Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-26 10:27 -0700
      Re: thread about the pros and cons of lambdas, but more about cons Bonita Montero <Bonita.Montero@gmail.com> - 2024-09-26 19:32 +0200
  Re: thread about the pros and cons of lambdas, but more about cons Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-26 14:09 -0700
    Re: thread about the pros and cons of lambdas, but more about cons Michael S <already5chosen@yahoo.com> - 2024-09-27 16:15 +0300
      Re: thread about the pros and cons of lambdas, but more about cons Tim Rentsch <tr.17687@z991.linuxsc.com> - 2024-09-28 04:25 -0700

csiph-web