summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-04-29 15:17:29 +0200
committerantirez <antirez@gmail.com>2015-04-29 15:18:51 +0200
commit0b605e54ff9c59e96fa4f275a7e3ab2e0c09e609 (patch)
tree14760ddbed2288abb20af12808ce91fc19bea74a
parentda58926ac5cc920916c9db256596eaef9287e9cc (diff)
downloadredis-0b605e54ff9c59e96fa4f275a7e3ab2e0c09e609.tar.gz
Avoid gettimeofday() in expireIfNeeded() when possible.
When the key expires far in the future compared to the cached time in server.mstime, calling mstime(), that calls gettimeofday(), should not be very useful. Instead when we are near the expire, we want the additional precision. This commit is related to issue #2552.
-rw-r--r--src/db.c16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/db.c b/src/db.c
index dfec71942..2b8acf855 100644
--- a/src/db.c
+++ b/src/db.c
@@ -818,7 +818,21 @@ int expireIfNeeded(redisDb *db, robj *key) {
* only the first time it is accessed and not in the middle of the
* script execution, making propagation to slaves / AOF consistent.
* See issue #1525 on Github for more information. */
- now = server.lua_caller ? server.lua_time_start : mstime();
+ if (server.lua_caller) {
+ now = server.lua_time_start;
+ } else {
+ /* If this is not the Lua caller, we actually need to get the current
+ * time. However gettimeofday(), which is called by mstime(), may be
+ * expensive, so we try to use the cached time instead, as found in
+ * server.mstime, which is not very accurate, but should usually be
+ * in the range of +/- 100 milliseconds.
+ *
+ * If the time the key will expire seems to be much more in the future
+ * compared to server.mstime, we use the server.mstime approximation.
+ * Otherwise if we see the key is going to expire within two seconds
+ * we fetch the actual time from the operating system. */
+ now = (when - server.mstime > 2000) ? server.mstime : mstime();
+ }
/* If we are running in the context of a slave, return ASAP:
* the slave key expiration is controlled by the master that will