Project

General

Profile

Actions

Bug #20978

closed

Ractor[]/Thread[]/Fiber[] behavior difference

Added by zverok (Victor Shepelev) 18 days ago. Updated 2 days ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.0dev (2024-12-22T06:08:19Z master 3808d29e20) +PRISM [x86_64-linux]
[ruby-core:120367]

Description

Ractor.[] and .[]= support string and symbol keys (treating them by string value, and also accepting anything responding to #to_str):

Ractor[:value] = 10
Ractor[:value] #=> 10
Ractor['value'] #=> 10
o = Object.new
def o.to_str = 'value'
Ractor[o] #=> 10

Ractor[1]
# in 'Ractor.[]': 1 is not a symbol nor a string (TypeError)

But Fiber.[] only accept symbols:

Fiber[:value] = 10
Fiber['value']
# in 'Fiber.[]': wrong argument type String (expected Symbol) (TypeError)

Thread#[] and #[]= behave like Ractor’s.

(The documentation for Fiber and Thread follows the reality—while Ractor’s current docs don’t specify key type at all—but the discrepancy feels somewhat weird.)

Updated by nobu (Nobuyoshi Nakada) 17 days ago

Before d4c720a91bc7bb9ff31810e1720acffb939f7a2f, fiber storage used rb_check_id to check arguments.
This function tries to convert a non-Symbol value to a String.
So this bug is a regression, I think.

Actions #3

Updated by nobu (Nobuyoshi Nakada) 17 days ago

  • Status changed from Open to Closed

Applied in changeset git|adad97a0310ebcd749e906be48bdabe9740deddf.


[Bug #20978] Stringize Fiber storage keys

Updated by Eregon (Benoit Daloze) 2 days ago

IIRC it was actually intentional to only allow symbols, as this makes it much more feasible to e.g. represent Fiber storage as object shapes.
This was discussed on the ticket adding Fiber storage.

Strings are not always interned, and interning them is a non-trivial cost.
And when not interned there is a non-trivial overhead to compare them, making every access slower.
At least the current code seems to intern (rb_to_symbol()), so using Strings is just slower but doesn't slow down every Fiber storage access.

Why would anyone want to use strings for this API, if it's going to be converted to Symbol internally anyway and so slower than using the Symbol?

Updated by zverok (Victor Shepelev) 2 days ago

Why would anyone want to use strings for this API, if it's going to be converted to Symbol internally anyway and so slower than using the Symbol?

TBH, I’d, too, rather expect only Symbols to work for such “system programming” (so to say) APIs. I just raised a concern about the inconsistency of similar APIs here.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0