Project

General

Profile

Actions

Feature #17327

closed

The Queue constructor should take an initial set of items

Added by chrisseaton (Chris Seaton) over 3 years ago. Updated about 3 years ago.

Status:
Closed
Target version:
-
[ruby-core:100858]

Description

I often create a Queue and then process it with a set of concurrent workers in threads. I end up writing:

q = Queue.new
worklist.each do |work|
  q.push work
end

I'd rather be able to write

q = Queue.new(*worklist)

Updated by ioquatix (Samuel Williams) over 3 years ago

What about a way to bulk add items, e.g. q.concat or whatever is the same for Array, and maybe it would be best to have the first argument as an array, e.g. Queue.new(worklist)? I think it's more expensive to expand it in CRuby when you write *worklist.

Internally, this might still be adding one item at a time, in order to invoke the right "wakeup" machinery.

Updated by chrisseaton (Chris Seaton) over 3 years ago

I'm not worried about Queue.new(worklist) or Queue.new(*worklist), so that's fine if more people feel that way. I think the key thing is conciseness in text source code, and also avoiding needing to synchronise while adding each individual item.

Updated by ioquatix (Samuel Williams) over 3 years ago

That all makes sense to me.

Updated by byroot (Jean Boussier) over 3 years ago

I agree that the constructor should take an enumerable rather than variadic arguments, as it would be consistent with Set.new([1, 2, 3]), and Array.new([1, 2, 3])

Updated by ufuk (Ufuk Kayserilioglu) over 3 years ago

Agreed with @byroot (Jean Boussier) (actually I was going to note the same, he beat me to it).

I would also like to note that different from Set, for example, the order of items in the supplied parameter matters in the Queue case. Even though the expected outcome is for the items to be pushed to the Queue in given order, it might still be a good idea to explicitly call that out in the method documentation.

Updated by chrisseaton (Chris Seaton) over 3 years ago

I updated to take a single array rather than a variable number of arguments.

I had two choices for how to do this - Set takes an Enumerable, using each to access items, and Array takes another Array, using #to_ary if needed.

I went with the same as what Array does, because both Array and Queue are core libraries, where Set is a standard library.

Updated by matz (Yukihiro Matsumoto) about 3 years ago

I accept the idea. Take Enumerable as initial values.

Matz.

Updated by ko1 (Koichi Sasada) about 3 years ago

  • Assignee set to ko1 (Koichi Sasada)
Actions #10

Updated by nobu (Nobuyoshi Nakada) about 3 years ago

  • Status changed from Open to Closed

Applied in changeset git|1f0e0dfb228fd14b3f6687539ba274ba6a2d1643.


Thread::Queue.new should accept an Enumerable [Feature #17327]

Enumerable implements #to_a but not #to_array.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0