Hello,
in this post, I’d like to make a proposal to contribute
to Ruby’s fitness for the challenges of parallelization.
The problem with sharing objects between threads
is concurrent write access.
If the objects were frozen,
everything would be OK … but also boring.
Now, I think it should be possible
to freeze the “already existing” data of an object
and at the same time leave it open for “further growth”.
Especially, I have Hashes and Arrays in mind:
New key/value pairs could be added to Hashes,
objects could be appended to Arrays.
So, I think it would be beneficial to introduce a third state of objects
between frozen and normal, like “growing” or “crystallizing” or
“monotonous” or “incrementally_freezing” or “partially_frozen” …
In this state, all methods that would
modify the already “crystallized” data of the object
should throw an exception.
E.g. for strings, only appending would be allowed.
Perhaps it would help implementing this idea,
if the << method would be made special
and be the only accepted modifying method
in the “growing” state.
Also, it would probably be good to have a means of determining
if a method call is safe to use, in the sense
that its result will always be the same object,
regardless of what other threads do to the receiver.
(Let’s say, it is a “permanent” method call)
Roughly:
-
when the receiving object is frozen, all method calls are “permanent”
-
when the receiver is in the normal (unrestricted) state,
method calls are “permanent”, iff the receiver is an immediate value. -
when the object is in the “growing” state, then it’s a bit tricky:
Just having an array “permanent_methods” would not always work,
because there are methods that are both,
“permanent” or not, depending on the arguments.For example, we would definitely want to use Array#[],
but this is only “permanent” for non-negative indices.Perhaps, there could be some annotation to those methods,
(raising an exception if the call would not guarantee a permanent
result),
and this would only be switched on if the object is in the
“crystallizing” state
(and the general check for “permanentness” were switched on, say
globally).
OK, let me stop here, for now.
Has a similar idea been discussed before?
Regards,
Sven