[ruby-cvs:78453] 4d7a6d04b2 (master): Avoid unnecessary tzset() call

"KOSAKI Motohiro" <[email protected]>
Newsgroups gmane.comp.lang.ruby.cvs
Message-ID <[email protected]>
KOSAKI Motohiro	2019-12-02 01:21:05 +0900 (Mon, 02 Dec 2019)

  New Revision: 4d7a6d04b2

  https://github.com/ruby/ruby/commit/4d7a6d04b2

  Log:
    Avoid unnecessary tzset() call
    
    Akatsuki reported ENV['TZ'] = 'UTC' improved 7x-8x faster on following code.
    t = Time.now; 100000.times { Time.new(2019) }; Time.now - t
    https://hackerslab.aktsk.jp/2019/12/01/141551
    
    commit 4bc1669127(reduce tzset) dramatically improved this situation. But still,
    TZ=UTC is faster than default.
    
    This patch removs unnecessary tzset() call completely.
    
    Performance check
      ----------------------
    test program: t = Time.now; 100000.times { Time.new(2019) }; Time.now - t
    before:         0.387sec
    before(w/ TZ):  0.197sec
    after:          0.162sec
    after(w/ TZ):   0.165sec
    
    OK. Now, Time creation 2x faster *and* TZ=UTC doesn't improve anything.
    We can forget this hack completely. :)
    
    Side note:
    This patch slightly changes Time.new(t) behavior implicitly. Before this patch, it might changes
    default timezone implicitly. But after this patch, it doesn't. You need to reset TZ
    (I mean ENV['TZ'] = nil) explicitly.
    But I don't think this is big impact. Don't try to change /etc/localtime on runtime.
    
    Side note2: following test might be useful for testing "ENV['TZ'] = nil".
      -----------------------------------------
    % cat <<'End' | sudo sh -s
    rm -f /etc/localtime-; cp -a /etc/localtime /etc/localtime-
    rm /etc/localtime; ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
    ./ruby -e '
    p Time.new(2000).zone # JST
    File.unlink("/etc/localtime"); File.symlink("/usr/share/zoneinfo/America/Los_Angeles", "/etc/localtime")
    p Time.new(2000).zone # JST (ruby does not follow /etc/localtime modification automatically)
    ENV["TZ"] = nil
    p Time.new(2000).zone # PST (ruby detect /etc/localtime modification)
    '
    rm /etc/localtime; cp -a /etc/localtime- /etc/localtime; rm /etc/localtime-
    End

  Modified files:
    hash.c
    time.c
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.