From aa6642de630cfc10063154d84e45a7bff30e9103 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Tue, 7 Nov 2023 13:22:48 -0500 Subject: Use embedded TypedData for Time objects This drops the total size of a Time object from 86 bytes to 80 bytes. Running the benchmark benchmark/time_now.yml, this commit improves performance of Time.now by about 30%: ``` Time.now Branch: 13159405.4 i/s Master: 10036908.7 i/s - 1.31x slower Time.now(in: "+09:00") Branch: 2712172.6 i/s Master: 2138637.9 i/s - 1.27x slower ``` It also decreases memory usage by about 20%: ``` ary = 10_000_000.times.map { Time.now } puts `ps -o rss= -p #{$$}` ``` Branch: 961792 Master: 1196544 Co-Authored-By: Jean Boussier --- test/ruby/test_time.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'test/ruby/test_time.rb') diff --git a/test/ruby/test_time.rb b/test/ruby/test_time.rb index 40b7ac79f8..45439be995 100644 --- a/test/ruby/test_time.rb +++ b/test/ruby/test_time.rb @@ -1407,7 +1407,10 @@ class TestTime < Test::Unit::TestCase def test_memsize # Time objects are common in some code, try to keep them small omit "Time object size test" if /^(?:i.?86|x86_64)-linux/ !~ RUBY_PLATFORM - omit "GC is in debug" if GC::INTERNAL_CONSTANTS[:DEBUG] + omit "GC is in debug" if GC::INTERNAL_CONSTANTS[:RVALUE_OVERHEAD] > 0 + omit "memsize is not accurate due to using malloc_usable_size" if GC::INTERNAL_CONSTANTS[:SIZE_POOL_COUNT] == 1 + omit "Only run this test on 64-bit" if RbConfig::SIZEOF["void*"] != 8 + require 'objspace' t = Time.at(0) sizeof_timew = @@ -1418,7 +1421,7 @@ class TestTime < Test::Unit::TestCase end sizeof_vtm = RbConfig::SIZEOF["void*"] * 4 + 8 expect = GC::INTERNAL_CONSTANTS[:BASE_SLOT_SIZE] + sizeof_timew + sizeof_vtm - assert_equal expect, ObjectSpace.memsize_of(t) + assert_operator ObjectSpace.memsize_of(t), :<=, expect rescue LoadError => e omit "failed to load objspace: #{e.message}" end -- cgit v1.2.3