From dda048381f798697337435c617d96f3086467d59 Mon Sep 17 00:00:00 2001 From: naruse Date: Wed, 4 Jul 2018 10:06:51 +0000 Subject: Optimize Time.utc Time.utc uses timegmw() and it uses leap second information. If the given time is larger than known_leap_seconds_limit, it calls find_time_t, which uses localtime(3) and calls stat(2) in it. This patch avoid it by setting known_leap_seconds_limit to 0 if the timezone doesn't have leapsecond information (if no leap second is found "now", I assume the timezone doesn't have leapsecond information). Before: % time ./miniruby --disable-gem -e'time = Time.now; year = time.year; month = time.month; day = time.day; hour = time.hour; min = time.min; sec = time.sec + time.subsec; i = 0; while i < 100000; ::Time.utc(year, month, day, hour, min, sec); i += 1; end' ./miniruby --disable-gem 0.35s user 0.19s system 99% cpu 0.542 total After: % time ./miniruby --disable-gem -e'time = Time.now; year = time.year; month = time.month; day = time.day; hour = time.hour; min = time.min; sec = time.sec + time.subsec; i = 0; while i < 100000; ::Time.utc(year, month, day, hour, min, sec); i += 1; end' ./miniruby --disable-gem 0.23s user 0.00s system 99% cpu 0.233 total git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63848 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- time.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'time.c') diff --git a/time.c b/time.c index f48f0e29f3..f513dc0b14 100644 --- a/time.c +++ b/time.c @@ -1095,6 +1095,9 @@ init_leap_second_info(void) timew = timegmw_noleapsecond(&vtm); number_of_leap_seconds_known = NUM2INT(w2v(wsub(TIMET2WV(known_leap_seconds_limit), rb_time_unmagnify(timew)))); + if (number_of_leap_seconds_known == 0) { + known_leap_seconds_limit = 0; + } } } -- cgit v1.2.3