Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm
Precedence: bulk
X-No-Archive: yes
List-Id: Zsh Workers List <zsh-workers.zsh.org>
List-Post: <mailto:zsh-workers@zsh.org>
List-Help: <mailto:zsh-workers-help@zsh.org>
X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au
X-Spam-Level: 
X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,
	T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:subject:message-id:mail-followup-to:mime-version
         :content-disposition:user-agent;
        bh=k3zlkh1Q2g/9t7oPWLSi8R2Fjwr+9pZyD/l1/6RkY/o=;
        b=Pqal/2oKZ6LCmwvuJA9/uPmGctc1obn/YqGbk5zbYD4Jm4uv11rK/MqKQqf2p1FLbE
         BqRExsldX3KllWcMYFNmA68ykZ+6K1bptDsgB0YhP4/EIzT5/Lebcex9P0jv8h3+iT0D
         CTo6hNM9w2+9NmQ3b4dVpdiBQ6rTArtLClsZ1AxTHXt9ZkWB3zSZ3YEOW5gvaRHNsokW
         GL91DBBS8+JEoRA9AFx2Io6sUFVO5BJPk2SNsj4nD0crQFeNFhdbZR+4dH9GikSFqO4d
         bVDfvTNCLp5MRvY+B40gNryvAUvqcZwq+uqj8X9aZhEfiotIeqjAapqXDuOLp59bq1PX
         1Hjg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:date:from:to:subject:message-id:mail-followup-to
         :mime-version:content-disposition:user-agent;
        bh=k3zlkh1Q2g/9t7oPWLSi8R2Fjwr+9pZyD/l1/6RkY/o=;
        b=EPNDpYN43j3ZBYzdeP56Pbjh3qNZUtkOeYwqLOlUcTBVAAvoEutTr+xIv7zrKyPiyu
         9i8wma4VCSq+dDoZ/D4B3hKH/aFVtD7EVCDQ+5R9WB/lAkGsOsXbpm1OtVmFecqySrXj
         jTzuOHTxm4POIKkuROfcmFCLdYsC0NbnI8xFEbq/xylfxL2IiL4I+ohTwvTZlqf1fccp
         mxRhBfB0Moworus3Tu5H20pW2YM8obEaFuAcqE6O51NiXOxKQn63N1PwemT6Zwi+cubm
         8a3SmAxO/mcIhONmP3wiUOvn4eH03oPA8XcuvM9PsD6aiJWn9ov/s+H/Sh+F2imAr8MU
         Mi2g==
X-Gm-Message-State: AG10YOSXUb6R/T/cHHbqh3aTmkcGRaoFnnU/uoUnDRyrIRIkkksr5t0Oboo8wHM2dZxkqA==
X-Received: by 10.28.11.131 with SMTP id 125mr26250312wml.58.1456333747384;
        Wed, 24 Feb 2016 09:09:07 -0800 (PST)
Date: Wed, 24 Feb 2016 17:09:04 +0000
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] issues with $SECONDS setting and expansio
Message-ID: <20160224170904.GA12099@chaz.gmail.com>
Mail-Followup-To: Zsh hackers list <zsh-workers@zsh.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
User-Agent: Mutt/1.5.21 (2010-09-15)
X-Seq: zsh-workers 38020

In, intsecondsgetfn, 
(zlong)(now.tv_usec - shtimer.tv_usec) / (zlong)1000000

was always 0 because that's the difference between 2 numbers
that are both under 1000000 divided by 1000000.

Also, by setting shtimer.tv_usec to 0 instead of now.tv_usec,
we're arbitrarily adding up to 1 second to $SECONDS.

Before this patch:

$ time zsh -c 'while ((SECONDS < 1)); do :; done'
zsh -c 'while ((SECONDS < 1)); do :; done'  0.20s user 0.07s system 99% cpu 0.269 total

(SECONDS gets incremented in between 0 and 1 seconds after
startup, above 0.269 seconds. bash and mksh have similar issues
which I've also reported there).

$ zsh -c 'SECONDS=0; typeset -F SECONDS; echo $SECONDS'
0.8081650000

After the patch:

$ time ./zsh -c 'while ((SECONDS < 1)); do :; done'
./zsh -c 'while ((SECONDS < 1)); do :; done'  0.87s user 0.13s system 99% cpu 1.003 total

SECONDS reaches 1 after 1 second, consistently.

$ ./zsh -c 'SECONDS=0; typeset -F SECONDS; echo $SECONDS'
0.0000740000

diff --git a/Src/params.c b/Src/params.c
index 8bd8a8e..7c5f79f 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -3804,8 +3804,8 @@ intsecondsgetfn(UNUSED(Param pm))
 
     gettimeofday(&now, &dummy_tz);
 
-    return (zlong)(now.tv_sec - shtimer.tv_sec) +
-	(zlong)(now.tv_usec - shtimer.tv_usec) / (zlong)1000000;
+    return (zlong)(now.tv_sec - shtimer.tv_sec -
+		  (now.tv_usec < shtimer.tv_usec ? 1 : 0));
 }
 
 /* Function to set value of special parameter `SECONDS' */
@@ -3823,7 +3823,7 @@ intsecondssetfn(UNUSED(Param pm), zlong x)
     shtimer.tv_sec = diff;
     if ((zlong)shtimer.tv_sec != diff)
 	zwarn("SECONDS truncated on assignment");
-    shtimer.tv_usec = 0;
+    shtimer.tv_usec = now.tv_usec;
 }
 
 /**/

