[tpop3d-discuss][PATCH]: avoid tons of time() calls

Chris Lightfoot chris at ex-parrot.com
Wed, 28 Jun 2006 19:56:50 +0100


On Wed, Jun 28, 2006 at 07:52:08PM +0200, Arkadiusz Miskiewicz wrote:
> Hi,
> 
> I've just checked that on busy server I get
> nearly 10000 time() calls per second!
> 
> # grep  19:36:04 OUT |grep time | wc -l
> 9668
> 
> where OUT is output from strace -tt -f -F -s 200 -p 5571 -o OUT
> (5571 is main parent of all tpop3d childs).
> 
> Do we need to call it so often?
> 
> In most cases if (!(c = connections[i])) will just continue and I see no point
> to check time every iteration.

ok, fair enough. Do you need the local variable, though?
Why not just break if time(NULL) >= start + LATENCY ?

(Actually the elegant way to do this would probably be to
set a timer at the start of the function, and then set a
flag in the signal handler to abort the loop. That would
allow convenient subsecond precision too, though that's
not a very important enhancement.)

> --- netloop.c       2006-06-26 07:30:25.000000000 +0200
> +++ netloop.c   2006-06-28 19:46:00.000000000 +0200
> @@ -387,16 +387,20 @@
>      static size_t i;
>      size_t i0;
>      time_t start;
> +    int latency = 1;
> 
>      time(&start);
> 
> -    for (i0 = (i + max_connections - 1) % max_connections; time(NULL) < start + LATENCY && i != i0; i = (i + 1) % max_connections) {
> +    for (i0 = (i + max_connections - 1) % max_connections; latency && i != i0; i = (i + 1) % max_connections) {
>          connection c;
>          int r;
> 
>          if (!(c = connections[i]))
>              continue;
> 
> +       if (time(NULL) >= start + LATENCY)
> +               latency = 0;
> +
>          if (i > 0 && post_fork) {
>              connections[0] = c;
>              connections[i] = NULL;
> 

-- 
``Because that's where the money is.''
  (Willie Sutton, explaining why he robbed banks)