Skip to content

Commit

Permalink
viml/reltime(): allow negative result neovim#10453
Browse files Browse the repository at this point in the history
- define proftime_T as signed integer
- profile_sub(): allow negative result

closes neovim#10452
  • Loading branch information
justinmk committed Jul 9, 2019
1 parent 652be3c commit 06af88c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/nvim/os/time.c
Expand Up @@ -39,16 +39,16 @@ void time_init(void)
/// @see gettimeofday(2)
///
/// @return Current time in microseconds.
uint64_t os_utime(void)
int64_t os_utime(void)
FUNC_ATTR_WARN_UNUSED_RESULT
{
uv_timeval64_t tm;
int e = uv_gettimeofday(&tm);
if (e != 0 || tm.tv_sec < 0 || tm.tv_usec < 0) {
return 0;
}
uint64_t rv = (uint64_t)tm.tv_sec * 1000 * 1000; // s => μs
STRICT_ADD(rv, tm.tv_usec, &rv, uint64_t);
int64_t rv = tm.tv_sec * 1000 * 1000; // s => μs
STRICT_ADD(rv, tm.tv_usec, &rv, int64_t);
return rv;
}

Expand Down
4 changes: 2 additions & 2 deletions src/nvim/profile.c
Expand Up @@ -61,7 +61,7 @@ proftime_T profile_setlimit(int64_t msec) FUNC_ATTR_WARN_UNUSED_RESULT
}
assert(msec <= (INT64_MAX / 1000LL) - 1);

proftime_T usec = (proftime_T)msec * 1000ULL;
proftime_T usec = msec * 1000;
return os_utime() + usec;
}

Expand Down Expand Up @@ -111,7 +111,7 @@ proftime_T profile_add(proftime_T tm1, proftime_T tm2) FUNC_ATTR_CONST
/// @return `tm1` - `tm2`
proftime_T profile_sub(proftime_T tm1, proftime_T tm2) FUNC_ATTR_CONST
{
return tm1 > tm2 ? tm1 - tm2 : 0; // os_utime() may go backwards.
return tm1 - tm2;
}

/// Adds the `self` time from the total time and the `children` time.
Expand Down
2 changes: 1 addition & 1 deletion src/nvim/profile.h
Expand Up @@ -4,7 +4,7 @@
#include <stdint.h>
#include <time.h>

typedef uint64_t proftime_T;
typedef int64_t proftime_T;

#define TIME_MSG(s) do { \
if (time_fd != NULL) time_msg(s, NULL); \
Expand Down
10 changes: 10 additions & 0 deletions test/functional/eval/reltime_spec.lua
Expand Up @@ -33,4 +33,14 @@ describe('reltimestr(), reltimefloat()', function()
ok(reltimefloat(differs) < 1.0)

end)

it('reltime() allows negative result #10452', function()
local older_time = reltime()
command('sleep 1m')
local newer_time = reltime()
-- Should be something like -0.002123.
local rv = tonumber(reltimestr(reltime(newer_time, older_time)))
ok(rv < 0)
ok(rv > -10)
end)
end)

0 comments on commit 06af88c

Please sign in to comment.