summaryrefslogtreecommitdiff
path: root/test/ruby/test_time.rb
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2023-11-07 13:22:48 -0500
committerPeter Zhu <peter@peterzhu.ca>2023-11-07 15:48:06 -0500
commitaa6642de630cfc10063154d84e45a7bff30e9103 (patch)
treea0cbc1bb13334fad5fffba6d9a38dc714bbcf294 /test/ruby/test_time.rb
parent392238e3fd76beb923de1ba3f8d8d6bd28c7030e (diff)
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 <byroot@ruby-lang.org>
Diffstat (limited to 'test/ruby/test_time.rb')
-rw-r--r--test/ruby/test_time.rb7
1 files changed, 5 insertions, 2 deletions
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