Module SekSource

This library offers efficient implementations of ephemeral sequences and persistent sequences, together with efficient conversions between these two data structures.

Type Abbreviations

The following type abbreviations help give readable types to some operations on sequences.

Sourcetype index = int

An index into a sequence is an integer. It is comprised between 0 (included) and the length of the sequence (excluded or included, depending on the circumstances).

Sourcetype length = int

The length of a sequence is a nonnegative integer.

Sourcetype capacity = int

The capacity of a chunk is a nonnegative integer.

Sourcetype depth = int

The depth of a chunk is a nonnegative integer.

Library API

The signature SEK is the public API of the library. If you are a new user, you do not need to follow this link: the library's API appears below anyway. Just read on!

Sourcemodule type SEK = sig ... end

Ephemeral and Persistent Sequences

The submodules Ephemeral and Persistent offer implementations of ephemeral (mutable) and persistent (immutable) sequences.

Sourcetype side
Sourceval front : side
Sourceval back : side

A side appears as a parameter to several operations, such as push and pop, which can operate at either end of a sequence.

Sourcetype direction
Sourceval forward : direction
Sourceval backward : direction

A direction appears as a parameter to several operations, such as iter, which can traverse the sequence either forward (from front to back) or backward (from back to front).

Sourceexception Empty

The exception Empty is raised by pop and peek operations when applied to an empty sequence.

Sourcemodule Ephemeral : sig ... end

The submodule Ephemeral, also available under the name E, offers an implementation of ephemeral (mutable) sequences.

Sourcemodule Persistent : sig ... end

The submodule Persistent, also available under the name P, offers an implementation of persistent (immutable) sequences.

Sourcemodule E = Ephemeral

E is a short name for the submodule Ephemeral.

Sourcemodule P = Persistent

P is a short name for the submodule Persistent.

Conversion Functions

The following functions offer fast conversions between ephemeral and persistent sequences.

Sourceval snapshot : 'a Ephemeral.t -> 'a Persistent.t

snapshot s constructs and returns a persistent sequence whose elements are the elements of s. It is less efficient than snapshot_and_clear, whose use should be preferred, when possible.

Sourceval snapshot_and_clear : 'a Ephemeral.t -> 'a Persistent.t

snapshot_and_clear s constructs and returns a persistent sequence whose elements are the elements of s. As a side effect, it clears s.

Sourceval edit : 'a Persistent.t -> 'a Ephemeral.t

edit s constructs and returns a new ephemeral sequence whose elements are the elements of s.

Emulation Layers
Sourcemodule Queue : sig ... end

The submodule Queue is a replacement for OCaml's standard Queue module, where a queue is implemented as an ephemeral sequence. Elements are enqueued at the back end of the sequence and dequeued at the front end.

Sourcemodule Stack : sig ... end

The submodule Stack is a replacement for OCaml's standard Stack module, where a stack is implemented as an ephemeral sequence. Elements are pushed and popped at the front end of the sequence.

Miscellaneous
Sourceval released : unit -> unit

The function call released() does nothing if the library was compiled in release mode, and fails (with an assertion failure) if the library was compiled with assertions enabled.

Settings

The following settings can be controlled by passing parameters to the functor Make (below).

Chunk Capacity

A sequence is represented in memory by a complex data structure that involves chunks, that is, arrays of elements. The data structure is organized in several layers. In the outermost layer, at depth 0, chunks of elements are used. In the next layer, at depth 1, chunks of chunks of elements are used, and so on: at depth k+1, chunks whose elements are depth-k chunks are used.

The functor parameter C, whose signature is CAPACITY, determines the desired capacity of these chunks. This capacity may depend on the depth.

Sourcemodule type CAPACITY = sig ... end
Overwriting Empty Slots

The functor parameter O, whose signature is OVERWRITE_EMPTY_SLOTS, determines whether the content of a just-emptied slot in an ephemeral sequence should be overwritten with the default value that was supplied when the sequence was created.

Setting this parameter to DoOverwriteEmptySlots is safe.

Setting this parameter to DoNotOverwriteEmptySlots can save time but can also cause a memory leak, because the obsolete value stored in the slot remains reachable and cannot be collected.

Sourcemodule type OVERWRITE_EMPTY_SLOTS = sig ... end
Compact Persistent Sequence Threshold

A persistent sequence whose length is less than or equal to a certain threshold can be represented in a simple and compact way (for instance, using an immutable array).

The functor parameter T, whose signature is THRESHOLD, determines this threshold.

Sourcemodule type THRESHOLD = sig ... end

The Functor Make

The functor Make constructs an implementation of the signature SEK, and allows the user to choose the value of the parameters described above. Be warned, however, that the number and types of the parameters of this functor may change in the future. Users who want maximum forward compatibility should not use this functor.

The following are recommended default arguments for the functor Make.

Complexity Guide

This section offers a simplified guide to the complexity of the operations on sequences.

The elements of a sequence are stored internally in chunks, that is, arrays of a fixed capacity K. This is why this data structure is known as a chunk sequence. A larger value of K speeds up certain operations and reduces memory usage, but slows down other operations. A practical value of K is 128.

As long as no concatenation operations are performed, the space usage of a sequence of length n is (1+10/K) * n + O(K) words. If concatenation operations are involved, the worst-case space bound is doubled and becomes 2 * (1+10/K) * n + O(K) words. Yet, this bound is unlikely to be reached in practice.

Below a certain threshold T, persistent sequences are represented in a more compact form: a persistent sequence of length less than (or equal to) T is represented as an immutable array of length n. Thus, below this threshold T, all operations have cost O(n), where n denotes the length of the sequence, except P.create, P.default, P.length, P.is_empty, P.peek and P.get, which have complexity O(1). Observe that it costs O(T^2) to build a persistent sequence of length T through a series of persistent push operations; this is not recommended! Instead, one should first build an ephemeral sequence through a series of ephemeral push operations, then convert it to a persistent sequence. This way, the construction cost is only O(n+K).

In the remainder of this section, we focus on sequences that contain more than T elements, and review the asymptotic complexity of each operation.

We first review a number of operations whose complexity is the same in the ephemeral and persistent cases:

We continue with a review of operations on ephemeral sequences:

In the case of get and set operations, the O notation hides a fairly large constant factor. In the future, we intend to provide efficient iterators, so as to support performing a series of get and set operations in a more efficient way.

We move on to operations on persistent sequences:

We end this review with a discussion of the conversion functions:

To conclude this complexity guide, let us give some explanations about the representation of sequences in memory, which helps understand the cost of the operations.

The ephemeral data structure and the persistent data structure have the same representation, up to a few variations that need not be discussed here. This allows the main two conversion operations, namely snapshot_and_clear and edit, to be extremely efficient: their time complexity is O(K), regardless of the number of elements n in the data structure.

The operation E.copy, which creates a copy of an ephemeral sequence, exploits the same mechanism: the chunks are shared between the original sequence and the copy. Its time complexity is O(K).

Naturally, this efficiency comes at a cost. When a chunk is shared between several ephemeral or persistent data structures, its content cannot be modified in arbitrary ways. If one is not careful, an operation on a sequence s could have unintended effects on other sequences that share some chunks with s.

Thus, internally, the data structure keeps track of which chunks are definitely uniquely owned and which chunks are possibly shared.

The chunks that participate in a persistent sequence are always regarded as shared. Chunks that participate in an ephemeral sequence s may be either uniquely owned by s or shared with other (ephemeral or persistent) sequences.

Operations on uniquely owned chunks are performed in place, whereas operations on shared chunks may involve a copy-on-write operation.

It should now be clear why a copy operation, such as E.copy, has a hidden cost. Indeed, after the operation, both the original sequence and its copy lose the unique ownership of their chunks. This implies that many subsequent operations on the original sequence and on its copy will be slower than they could have been if no copy had taken place.