Commit graph

704 commits

Author SHA1 Message Date
Jean Boussier
2a6345e957 time.c: fix time_mark_and_move when WIDEVALUE_IS_WIDER
In such case the pointer need to be casted.
2025-08-09 13:32:53 +02:00
Jean Boussier
1aabd2cb36 Convert time to use rb_gc_mark_and_move
The `p->field = rb_gc_location(p->field)` isn't ideal because it means all
references are rewritten on compaction, regardless of whether the referenced
object has moved. This isn't good for caches nor for Copy-on-Write.

`rb_gc_mark_and_move` avoid needless writes, and most of the time allow to
have a single function for both marking and updating references.
2025-08-07 21:00:00 +02:00
Nobuyoshi Nakada
b246fb604e
Use <> for a system header 2025-07-24 12:56:08 +09:00
John Hawthorn
6b3fa23563 Add write barrier on tm_from_time->timew
We want to always use time_set_timew, as timew is 64-bit even on 32-bit
platforms so we need to be careful to both write that size, but still
trigger write barriers if we end up with a heap object.
2025-06-18 10:17:34 -07:00
John Hawthorn
0e2067dfa7 Add missing write barrier to time_init_copy 2025-06-18 10:17:34 -07:00
John Hawthorn
7439f35378 Write barrier for zone on time 2025-06-18 10:17:34 -07:00
Nobuyoshi Nakada
583ce06c0e Normalize subseconds using wideint_t instead of time_t 2025-06-13 19:35:52 +09:00
Nobuyoshi Nakada
b8de3cfb04 Conversion is needed between WIDEVALUE and VALUE 2025-06-13 19:35:52 +09:00
Jean Boussier
8a475d15d6 Implement dcompact for Time objects 2025-03-31 16:56:29 +02:00
Nobuyoshi Nakada
42daa6c2a2
[Bug #21141] [DOC] Fix indentation 2025-03-24 15:49:41 +09:00
Nobuyoshi Nakada
51bc992822
[Bug #21141] [DOC] Refine description of Time#utc? 2025-03-24 15:43:40 +09:00
Nobuyoshi Nakada
06919949a6
[Bug #21141] [DOC] Clarify what time is in UTC 2025-03-16 12:09:18 +09:00
Nobuyoshi Nakada
34098b669c
[Bug #21144] Win32: Convert the time zone name to the current locale
The Windows time zone IDs provided by Microsoft as of 24H1 are ASCII
only all, but the API itself is not impossible to set non-ASCII key
name.  Prefer the current locale encoding for now until we move to
UTF-8 including environment variables and command line arguments.
2025-02-20 15:34:32 +09:00
Nobuyoshi Nakada
3f07bc76ff [Bug #21144] Win32: Use Windows time zone ID if TZ is not set
If the TZ environment variable is not set, the time zone names
retrieved from the system are localized for UI display and may vary
across editions and language packs for the same time zone.
Use the time zone IDs that are invariant across environments instead.
2025-02-19 18:27:32 +09:00
Nobuyoshi Nakada
657bd31cc7
Trivial optimization
- Not call `strlen` at the end of the zone name.
- Use the initialized UTC string.
2025-02-17 18:27:24 +09:00
tomoya ishida
2d3d7a74e9
[DOC] Fix wrong call-seq format (#12662) 2025-01-29 21:53:00 +09:00
Nobuyoshi Nakada
fb82f3a632 Win32: Defer change of timezone name encoding after 3.4
This change will be merged into 3.5 along with other encoding, command
line, environment variables, etc.

Revert following commits:

- bd831bcca5
  [Bug #20929] Win32: Use `wcsftime`

- 1c15f641cc
  [Bug #20929] Win32: Encode timezone name in UTF-8

- 78762b5218
  [Bug #20929] Fix `assert_zone_encoding`
2024-12-24 13:25:55 +09:00
Victor Shepelev
78f55ccd9b
[DOC] Move Time#xmlschema docs to the core (#12403) 2024-12-21 20:06:48 +02:00
Nobuyoshi Nakada
70f5c62af1
Extract NUM2WV macro 2024-12-14 18:01:19 +09:00
Nobuyoshi Nakada
bd831bcca5
[Bug #20929] Win32: Use wcsftime
`_strftime_l` is not available on msvcrt that is still used by 32bit
mingw compiler.
2024-12-07 19:02:17 +09:00
Nobuyoshi Nakada
1c15f641cc [Bug #20929] Win32: Encode timezone name in UTF-8 2024-12-06 18:59:38 +09:00
Nobuyoshi Nakada
9611c619ac
[Bug #20797] Check seconds in UTC offset as well as minutes 2024-10-14 13:55:55 +09:00
Nobuyoshi Nakada
0641951e7b
2digits macros 2024-10-14 13:17:55 +09:00
S-H-GAMELINKS
e766cb3e57 Suppress warnings in time_init_parse function
When building Ruby on Ubuntu 22.04 and GCC 11.4.0, the following warning appeared.
And this change has suppressed warning.

```
compiling ../ruby/time.c
../ruby/time.c: In function ‘time_init_parse’:
../ruby/time.c:2650:21: warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
 2650 |         if (ndigits < TIME_SCALE_NUMDIGITS) {
      |                     ^
../ruby/time.c:2654:26: warning: comparison of integer expressions of different signedness: ‘size_t’ {aka ‘long unsigned int’} and ‘int’ [-Wsign-compare]
 2654 |         else if (ndigits > TIME_SCALE_NUMDIGITS) {
      |
```
2024-10-05 07:45:50 +09:00
Takashi Kokubun
798a115a15 Fix a -Wsign-compare warning
../time.c:5247:50: warning: operand of ‘?:’ changes signedness from ‘int’ to ‘long unsigned int’ due to unsignedness of other operand [-Wsign-compare]
 5247 |         int w = (year >= -9999 && year <= 9999 ? year_width : (int)(year < 0) + DECIMAL_SIZE_OF(year));
      |                                                  ^~~~~~~~~~
2024-09-23 16:39:31 -07:00
Nobuyoshi Nakada
7be1fafe58
Refactor Time#xmlschema
And refine uncommon date cases.

# Iteration per second (i/s)

|                            |compare-ruby|built-ruby|
|:---------------------------|-----------:|---------:|
|time.xmlschema              |      5.020M|   14.192M|
|                            |           -|     2.83x|
|utc_time.xmlschema          |      6.454M|   15.331M|
|                            |           -|     2.38x|
|time.xmlschema(6)           |      4.216M|   10.043M|
|                            |           -|     2.38x|
|utc_time.xmlschema(6)       |      5.486M|   10.592M|
|                            |           -|     1.93x|
|time.xmlschema(9)           |      4.294M|   10.340M|
|                            |           -|     2.41x|
|utc_time.xmlschema(9)       |      4.784M|   10.909M|
|                            |           -|     2.28x|
|fraction_sec.xmlschema(10)  |    366.982k|    3.406M|
|                            |           -|     9.28x|
|future_time.xmlschema       |    994.595k|   15.853M|
|                            |           -|    15.94x|
2024-09-23 14:29:25 +09:00
Nobuyoshi Nakada
214668fccb
[Feature #20707] Fix negative UTC offset conversion
In short, get rid of division and modulo of negative integers.
2024-09-06 15:53:46 +09:00
Jean Boussier
57e3fc32ea Move Time#xmlschema in core and optimize it
[Feature #20707]

Converting Time into RFC3339 / ISO8601 representation is an significant
hotspot for applications that serialize data in JSON, XML or other formats.

By moving it into core we can optimize it much further than what `strftime` will
allow.

```
compare-ruby: ruby 3.4.0dev (2024-08-29T13:11:40Z master 6b08a50a62) +YJIT [arm64-darwin23]
built-ruby: ruby 3.4.0dev (2024-08-30T13:17:32Z native-xmlschema 34041ff71f) +YJIT [arm64-darwin23]
warming up......

|                        |compare-ruby|built-ruby|
|:-----------------------|-----------:|---------:|
|time.xmlschema          |      1.087M|    5.190M|
|                        |           -|     4.78x|
|utc_time.xmlschema      |      1.464M|    6.848M|
|                        |           -|     4.68x|
|time.xmlschema(6)       |    859.960k|    4.646M|
|                        |           -|     5.40x|
|utc_time.xmlschema(6)   |      1.080M|    5.917M|
|                        |           -|     5.48x|
|time.xmlschema(9)       |    893.909k|    4.668M|
|                        |           -|     5.22x|
|utc_time.xmlschema(9)   |      1.056M|    5.707M|
|                        |           -|     5.40x|
```
2024-09-05 19:23:12 +02:00
Tanaka Akira
56fe5ad778 Time internal representation comment removed. 2024-06-02 10:18:56 +09:00
Nobuyoshi Nakada
055613fd86
Fix pointer incompatiblity
Since the subsecond part is discarded, WIDEVAL to VALUE conversion is
needed.
2024-03-22 18:18:35 +09:00
Nobuyoshi Nakada
d3279a0c11
[Bug #20327] Do not count subsecond to calculate UTC offset
Assume that there will never be any time zones with UTC offsets that
are subseconds.  Historically, UTC offset has only been used down to
the second.
2024-03-07 13:08:37 +09:00
Jean Boussier
d4f3dcf4df Refactor VM root modules
This `st_table` is used to both mark and pin classes
defined from the C API. But `vm->mark_object_ary` already
does both much more efficiently.

Currently a Ruby process starts with 252 rooted classes,
which uses `7224B` in an `st_table` or `2016B` in an `RArray`.

So a baseline of 5kB saved, but since `mark_object_ary` is
preallocated with `1024` slots but only use `405` of them,
it's a net `7kB` save.

`vm->mark_object_ary` is also being refactored.

Prior to this changes, `mark_object_ary` was a regular `RArray`, but
since this allows for references to be moved, it was marked a second
time from `rb_vm_mark()` to pin these objects.

This has the detrimental effect of marking these references on every
minors even though it's a mostly append only list.

But using a custom TypedData we can save from having to mark
all the references on minor GC runs.

Addtionally, immediate values are now ignored and not appended
to `vm->mark_object_ary` as it's just wasted space.
2024-03-06 15:33:43 -05:00
Nobuyoshi Nakada
df63e5bef6
[DOC] Functions for Time#initialize implementation 2024-02-17 17:27:02 +09:00
Nobuyoshi Nakada
6e4bfb35bb
Fix an argument name 2024-02-17 17:21:07 +09:00
Yusuke Endoh
25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
Nobuyoshi Nakada
092a17e7bd
[DOC] Markup class name Time as code 2023-12-02 21:48:00 +09:00
Nobuyoshi Nakada
ae3e9f114c
[DOC] Include timezones document in timev.rb
So that it can be referred as a part of the document of `Time`.
2023-12-02 21:46:52 +09:00
Jean Boussier
b4f551686b Get rid of useless dsize functions
If we always return 0, we might as well not define
the function at all.
2023-11-21 15:15:03 +01:00
Peter Zhu
aa6642de63 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>
2023-11-07 15:48:06 -05:00
haseponpon
089227e948
[DOC] Specify the unit of return value for Time#- 2023-08-16 22:51:17 +09:00
Burdette Lamar
8c5b9ebf71
[DOC] Improve doc guide compliance (#8221) 2023-08-15 14:43:58 -04:00
Jeremy Evans
5d4fff8456 Tighten Time.new(string) parsing
Disallow:

* Only year-month
* Only year-month-day
* Preceding whitespace
* Trailing whitespace

Fixes [Bug #19293]
2023-07-13 16:49:39 -07:00
Nobuyoshi Nakada
b934976024
Prefer 0 over NULL as function pointers
SunC warns use of `NULL`, pointer to data as function pointers.
2023-06-23 03:15:55 +09:00
Peter Zhu
a84c99468f Fix crash in Time on 32-bit systems
[Bug #19575]

struct vtm is packed causing it to have a size that is not aligned on
32-bit systems. When allocating it on the stack, it will have unaligned
addresses which means that the fields won't be marked by the GC when
scanning the stack (since the GC only marks aligned addresses). This can
cause crashes when the fields are heap allocated objects like Bignums.

This commit moves the flags in struct time_object into struct vtm for
space efficiency and removes the need for packing.

This is an example of a crash:

    ruby(rb_print_backtrace+0xd) [0x56848945] ../src/vm_dump.c:785
    ruby(rb_vm_bugreport) ../src/vm_dump.c:1101
    ruby(rb_assert_failure+0x7a) [0x56671857] ../src/error.c:878
    ruby(vm_search_cc+0x0) [0x56666e47] ../src/vm_method.c:1366
    ruby(rb_vm_search_method_slowpath) ../src/vm_insnhelper.c:2090
    ruby(callable_method_entry+0x5) [0x568232d3] ../src/vm_method.c:1406
    ruby(rb_callable_method_entry) ../src/vm_method.c:1413
    ruby(gccct_method_search_slowpath) ../src/vm_eval.c:427
    ruby(gccct_method_search+0x20f) [0x568237ef] ../src/vm_eval.c:476
    ruby(opt_equality_by_mid_slowpath+0x2c) [0x5682388c] ../src/vm_insnhelper.c:2338
    ruby(rb_equal+0x37) [0x566fe577] ../src/object.c:133
    ruby(rb_big_eq+0x34) [0x56876ee4] ../src/bignum.c:5554
    ruby(rb_int_equal+0x14) [0x566f3ed4] ../src/numeric.c:4640
    ruby(rb_int_equal) ../src/numeric.c:4634
    ruby(vm_call0_cfunc_with_frame+0x6d) [0x568303c2] ../src/vm_eval.c:148
    ruby(vm_call0_cfunc) ../src/vm_eval.c:162
    ruby(vm_call0_body) ../src/vm_eval.c:208
    ruby(rb_funcallv_scope+0xd1) [0x56833971] ../src/vm_eval.c:85
    ruby(RB_TEST+0x0) [0x567e8488] ../src/time.c:78
    ruby(eq) ../src/time.c:78
    ruby(small_vtm_sub) ../src/time.c:1523
    ruby(timelocalw+0x23b) [0x567f3e9b] ../src/time.c:1593
    ruby(time_s_alloc+0x0) [0x567f536b] ../src/time.c:3698
    ruby(time_new_timew) ../src/time.c:2694
    ruby(time_s_mktime) ../src/time.c:3698
2023-04-04 11:12:07 -04:00
BurdetteLamar
3b239d2480 Remove (newly unneeded) remarks about aliases 2023-02-19 14:26:34 -08:00
Nobuyoshi Nakada
a56d959ed5
Replace PACKED_STRUCT and PACKED_STRUCT_UNALIGNED 2023-02-08 12:35:27 +09:00
git
23052e005e * remove trailing spaces. [ci skip] 2023-02-07 21:03:20 +00:00
Peter Zhu
1c0127ed5e Make Time objects WB protected
Co-Authored-By: Jean Boussier <byroot@ruby-lang.org>
2023-02-07 16:03:08 -05:00
Peter Zhu
e9090eb64d Remove unused code in time.c
Co-Authored-By: Jean Boussier <byroot@ruby-lang.org>
2023-02-07 16:03:08 -05:00
Nobuyoshi Nakada
5be0d42d2c
[Bug #19296] Precheck bits of time components 2023-01-02 23:29:19 +09:00