How are Arrays Sets and SortedSets implemented under the hood in Ruby

EugeneMi

Usually Arrays are implemented as a chunks of memory, sets as hash maps, and sorted sets as skip lists. Is that also the case in Ruby?

I'm trying to evaluate the use of different containers in Ruby in terms of performance and memory footprint

Jörg W Mittag

Arrays are part of the Ruby core library. Each Ruby implementation has its own implementation of arrays. The Ruby Language Specification only specifies the behavior of Ruby arrays, it does not specify any particular implementation strategy. It doesn't even specify any performance constraints that would force or at least suggest a particular implementation strategy.

However, most Rubyists have some expectation about the performance characteristics of arrays that would force an implementation that doesn't meet them into obscurity, because nobody would actually use it:

  • inserting, prepending or appending as well as deleting an element has a worst-case step-complexity of O(n) and an amortized worst-case step-complexity of O(1)
  • accessing an element has a worst-case step-complexity of O(1)
  • iterating over all elements has a worst-case step-complexity of O(n)

This means that arrays need to be implemented as dynamic arrays with exponential resizing, otherwise those performance guarantees couldn't be met. You might get away with a very wide and shallow tree, but AFAIK no Ruby implementation does that.

Here's Rubinius's array implementation, which I personally find the easiest to read of all Ruby implementations. (Note: only the bare essentials are implemented in C++, most of the array methods are implemented in Ruby, e.g. in kernel/common/array.rb).

Set and SortedSet are part of the set library in the stdlib. The stdlib is shared mostly unchanged between Ruby implementations (at least the parts that are actually written in Ruby, obviously the parts written in other languages can't be shared), and since Set is written in Ruby, you can expect it to the same on all Ruby implementations.

Set is implemented as a Hash, where only the keys are used, the values are simply always true: see Set#add in lib/set.rb.

SortedSet is backed by a Red-Black-Tree that isn't implemented in Ruby.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

LINQ to SQL under the hood

From Dev

How do pointers work "under the hood" in C?

From Dev

How does enum.toString() work under the hood?

From Dev

Understanding how java debugging really works under the hood

From Dev

What is a blob under the hood?

From Dev

How does querySelector works under the hood?

From Dev

Under the hood of Uninformative Prior?

From Dev

How does PHP references work under the hood for arrays?

From Dev

How does mongoose populate work under the hood

From Dev

How does dart implement library privacy under the hood?

From Dev

Are Lisp lists always implemented as linked lists under the hood?

From Dev

How are `tagbody` and `go` implemented under the hood in Common Lisp?

From Dev

How does gperftools work under the hood?

From Dev

how Garbage collector works under the hood to collect dead object?

From Dev

How java AtomicReference works under the hood

From Dev

How does addEventListener work under the hood?

From Dev

How does this recursive array permutation function work under the hood?

From Dev

Objective-C - How does KVC work under the hood?

From Dev

How does lookup in $PATH work under the hood?

From Dev

How does lookup in $PATH work under the hood?

From Dev

Under the hood of Dispatcher

From Dev

How does mounting on the GUI work "under the hood"

From Dev

LINQ to SQL under the hood

From Dev

Is SplFixedArray implemented in C/C++ under the hood?

From Dev

Under the hood of Uninformative Prior?

From Dev

how copyToLocal and copyFromLocal work under the hood

From Dev

How do prototypes work under the hood?

From Dev

How is the 'apply ' method executed under the hood?

From Dev

Decoding under the hood in python

Related Related

HotTag

Archive