Bladeren bron

Add 'pkg/persistent/' from commit '8ed0fbdb11911ed89129a090bcc1562f3a759481'

git-subtree-dir: pkg/persistent
git-subtree-mainline: 543b12366182cb2c9c74ccdba553cc43625aec21
git-subtree-split: 8ed0fbdb11911ed89129a090bcc1562f3a759481
Qi Xiao 3 jaren geleden
bovenliggende
commit
0a5057a64d

+ 33 - 0
pkg/persistent/.gitignore

@@ -0,0 +1,33 @@
+/cover
+
+[._]*.s[a-w][a-z]
+[._]s[a-w][a-z]
+*.un~
+Session.vim
+.netrwhist
+*~
+
+# Compiled Object files, Static and Dynamic libs (Shared Objects)
+*.o
+*.a
+*.so
+
+# Folders
+_obj
+_test
+
+# Architecture specific extensions/prefixes
+*.[568vq]
+[568vq].out
+
+*.cgo1.go
+*.cgo2.c
+_cgo_defun.c
+_cgo_gotypes.go
+_cgo_export.*
+
+_testmain.go
+
+*.exe
+*.test
+*.prof

+ 10 - 0
pkg/persistent/.travis.yml

@@ -0,0 +1,10 @@
+language: go
+go:
+    - 1.14
+    - 1.15
+sudo: false
+os:
+    - linux
+    - osx
+script:
+    make travis

+ 210 - 0
pkg/persistent/LICENSE

@@ -0,0 +1,210 @@
+Eclipse Public License - v 1.0
+
+THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
+LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM
+CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
+
+   1. DEFINITIONS
+
+   "Contribution" means:
+
+a) in the case of the initial Contributor, the initial code and documentation
+distributed under this Agreement, and
+
+      b) in the case of each subsequent Contributor:
+
+         i) changes to the Program, and
+
+         ii) additions to the Program;
+
+where such changes and/or additions to the Program originate from and are
+distributed by that particular Contributor. A Contribution 'originates' from
+a Contributor if it was added to the Program by such Contributor itself or
+anyone acting on such Contributor's behalf. Contributions do not include additions
+to the Program which: (i) are separate modules of software distributed in
+conjunction with the Program under their own license agreement, and (ii) are
+not derivative works of the Program.
+
+   "Contributor" means any person or entity that distributes the Program.
+
+"Licensed Patents" mean patent claims licensable by a Contributor which are
+necessarily infringed by the use or sale of its Contribution alone or when
+combined with the Program.
+
+"Program" means the Contributions distributed in accordance with this Agreement.
+
+"Recipient" means anyone who receives the Program under this Agreement, including
+all Contributors.
+
+   2. GRANT OF RIGHTS
+
+a) Subject to the terms of this Agreement, each Contributor hereby grants
+Recipient a non-exclusive, worldwide, royalty-free copyright license to reproduce,
+prepare derivative works of, publicly display, publicly perform, distribute
+and sublicense the Contribution of such Contributor, if any, and such derivative
+works, in source code and object code form.
+
+b) Subject to the terms of this Agreement, each Contributor hereby grants
+Recipient a non-exclusive, worldwide, royalty-free patent license under Licensed
+Patents to make, use, sell, offer to sell, import and otherwise transfer the
+Contribution of such Contributor, if any, in source code and object code form.
+This patent license shall apply to the combination of the Contribution and
+the Program if, at the time the Contribution is added by the Contributor,
+such addition of the Contribution causes such combination to be covered by
+the Licensed Patents. The patent license shall not apply to any other combinations
+which include the Contribution. No hardware per se is licensed hereunder.
+
+c) Recipient understands that although each Contributor grants the licenses
+to its Contributions set forth herein, no assurances are provided by any Contributor
+that the Program does not infringe the patent or other intellectual property
+rights of any other entity. Each Contributor disclaims any liability to Recipient
+for claims brought by any other entity based on infringement of intellectual
+property rights or otherwise. As a condition to exercising the rights and
+licenses granted hereunder, each Recipient hereby assumes sole responsibility
+to secure any other intellectual property rights needed, if any. For example,
+if a third party patent license is required to allow Recipient to distribute
+the Program, it is Recipient's responsibility to acquire that license before
+distributing the Program.
+
+d) Each Contributor represents that to its knowledge it has sufficient copyright
+rights in its Contribution, if any, to grant the copyright license set forth
+in this Agreement.
+
+   3. REQUIREMENTS
+
+A Contributor may choose to distribute the Program in object code form under
+its own license agreement, provided that:
+
+      a) it complies with the terms and conditions of this Agreement; and
+
+      b) its license agreement:
+
+i) effectively disclaims on behalf of all Contributors all warranties and
+conditions, express and implied, including warranties or conditions of title
+and non-infringement, and implied warranties or conditions of merchantability
+and fitness for a particular purpose;
+
+ii) effectively excludes on behalf of all Contributors all liability for damages,
+including direct, indirect, special, incidental and consequential damages,
+such as lost profits;
+
+iii) states that any provisions which differ from this Agreement are offered
+by that Contributor alone and not by any other party; and
+
+iv) states that source code for the Program is available from such Contributor,
+and informs licensees how to obtain it in a reasonable manner on or through
+a medium customarily used for software exchange.
+
+   When the Program is made available in source code form:
+
+      a) it must be made available under this Agreement; and
+
+b) a copy of this Agreement must be included with each copy of the Program.
+
+Contributors may not remove or alter any copyright notices contained within
+the Program.
+
+Each Contributor must identify itself as the originator of its Contribution,
+if any, in a manner that reasonably allows subsequent Recipients to identify
+the originator of the Contribution.
+
+   4. COMMERCIAL DISTRIBUTION
+
+Commercial distributors of software may accept certain responsibilities with
+respect to end users, business partners and the like. While this license is
+intended to facilitate the commercial use of the Program, the Contributor
+who includes the Program in a commercial product offering should do so in
+a manner which does not create potential liability for other Contributors.
+Therefore, if a Contributor includes the Program in a commercial product offering,
+such Contributor ("Commercial Contributor") hereby agrees to defend and indemnify
+every other Contributor ("Indemnified Contributor") against any losses, damages
+and costs (collectively "Losses") arising from claims, lawsuits and other
+legal actions brought by a third party against the Indemnified Contributor
+to the extent caused by the acts or omissions of such Commercial Contributor
+in connection with its distribution of the Program in a commercial product
+offering. The obligations in this section do not apply to any claims or Losses
+relating to any actual or alleged intellectual property infringement. In order
+to qualify, an Indemnified Contributor must: a) promptly notify the Commercial
+Contributor in writing of such claim, and b) allow the Commercial Contributor
+to control, and cooperate with the Commercial Contributor in, the defense
+and any related settlement negotiations. The Indemnified Contributor may participate
+in any such claim at its own expense.
+
+For example, a Contributor might include the Program in a commercial product
+offering, Product X. That Contributor is then a Commercial Contributor. If
+that Commercial Contributor then makes performance claims, or offers warranties
+related to Product X, those performance claims and warranties are such Commercial
+Contributor's responsibility alone. Under this section, the Commercial Contributor
+would have to defend claims against the other Contributors related to those
+performance claims and warranties, and if a court requires any other Contributor
+to pay any damages as a result, the Commercial Contributor must pay those
+damages.
+
+   5. NO WARRANTY
+
+EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON
+AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS
+OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF
+TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
+Each Recipient is solely responsible for determining the appropriateness of
+using and distributing the Program and assumes all risks associated with its
+exercise of rights under this Agreement, including but not limited to the
+risks and costs of program errors, compliance with applicable laws, damage
+to or loss of data, programs or equipment, and unavailability or interruption
+of operations.
+
+   6. DISCLAIMER OF LIABILITY
+
+EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY
+CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
+LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
+WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS
+GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+
+   7. GENERAL
+
+If any provision of this Agreement is invalid or unenforceable under applicable
+law, it shall not affect the validity or enforceability of the remainder of
+the terms of this Agreement, and without further action by the parties hereto,
+such provision shall be reformed to the minimum extent necessary to make such
+provision valid and enforceable.
+
+If Recipient institutes patent litigation against any entity (including a
+cross-claim or counterclaim in a lawsuit) alleging that the Program itself
+(excluding combinations of the Program with other software or hardware) infringes
+such Recipient's patent(s), then such Recipient's rights granted under Section
+2(b) shall terminate as of the date such litigation is filed.
+
+All Recipient's rights under this Agreement shall terminate if it fails to
+comply with any of the material terms or conditions of this Agreement and
+does not cure such failure in a reasonable period of time after becoming aware
+of such noncompliance. If all Recipient's rights under this Agreement terminate,
+Recipient agrees to cease use and distribution of the Program as soon as reasonably
+practicable. However, Recipient's obligations under this Agreement and any
+licenses granted by Recipient relating to the Program shall continue and survive.
+
+Everyone is permitted to copy and distribute copies of this Agreement, but
+in order to avoid inconsistency the Agreement is copyrighted and may only
+be modified in the following manner. The Agreement Steward reserves the right
+to publish new versions (including revisions) of this Agreement from time
+to time. No one other than the Agreement Steward has the right to modify this
+Agreement. The Eclipse Foundation is the initial Agreement Steward. The Eclipse
+Foundation may assign the responsibility to serve as the Agreement Steward
+to a suitable separate entity. Each new version of the Agreement will be given
+a distinguishing version number. The Program (including Contributions) may
+always be distributed subject to the version of the Agreement under which
+it was received. In addition, after a new version of the Agreement is published,
+Contributor may elect to distribute the Program (including its Contributions)
+under the new version. Except as expressly stated in Sections 2(a) and 2(b)
+above, Recipient receives no rights or licenses to the intellectual property
+of any Contributor under this Agreement, whether expressly, by implication,
+estoppel or otherwise. All rights in the Program not expressly granted under
+this Agreement are reserved.
+
+This Agreement is governed by the laws of the State of New York and the intellectual
+property laws of the United States of America. No party to this Agreement
+will bring a legal action under this Agreement more than one year after the
+cause of action arose. Each party waives its rights to a jury trial in any
+resulting litigation.

+ 44 - 0
pkg/persistent/Makefile

@@ -0,0 +1,44 @@
+PKG_BASE := github.com/xiaq/persistent
+PKGS := $(shell go list ./... | grep -v /vendor/)
+PKG_COVERS := $(shell go list ./... | grep -v '^$(PKG_BASE)/vendor/' | grep -v '^$(PKG_BASE)$$' | sed "s|^$(PKG_BASE)/|cover/|" | sed 's/$$/.cover/')
+COVER_MODE := set
+
+FIRST_GOPATH=$(shell go env GOPATH | cut -d: -f1)
+
+default: get test
+
+get:
+	go get .
+
+generate:
+	go generate ./...
+
+test:
+	go test $(PKGS)
+
+cover/%.cover: %
+	mkdir -p $(dir $@)
+	go test -coverprofile=$@ -covermode=$(COVER_MODE) ./$<
+
+cover/all: $(PKG_COVERS)
+	echo mode: $(COVER_MODE) > $@
+	for f in $(PKG_COVERS); do test -f $$f && sed 1d $$f >> $@ || true; done
+
+# We would love to test for coverage in pull requests, but it's now
+# bettered turned off for two reasons:
+#
+# 1) The goverall badge will always show the "latest" coverage, even if that
+# comes from a PR.
+#
+# 2) Some of the tests have fluctuating coverage (the test against
+# edit.tty.AsyncReader), and goveralls will put a big cross on the PR when the
+# coverage happens to drop.
+goveralls: cover/all
+	test "$(TRAVIS_PULL_REQUEST)" = false \
+		&& go get -u github.com/mattn/goveralls \
+		&& $(FIRST_GOPATH)/bin/goveralls -coverprofile=cover/all -service=travis-ci \
+		|| echo "not sending to coveralls"
+
+travis: goveralls
+
+.PHONY: default get generate test goveralls upload travis

+ 79 - 0
pkg/persistent/README.md

@@ -0,0 +1,79 @@
+# Persistent data structure in Go
+
+[![GoDoc](https://godoc.org/github.com/xiaq/persistent?status.svg)](https://godoc.org/github.com/xiaq/persistent)
+[![Build Status](https://travis-ci.org/xiaq/persistent.svg?branch=master)](https://travis-ci.org/xiaq/persistent)
+[![Coverage Status](https://coveralls.io/repos/github/xiaq/persistent/badge.svg?branch=master)](https://coveralls.io/github/xiaq/persistent?branch=master)
+
+This is a Go clone of Clojure's persistent data structures.
+
+The API is not stable yet. **DO NOT USE** unless you are willing to cope with
+API changes.
+
+License is [Eclipse Public License 1.0](http://opensource.org/licenses/eclipse-1.0.php) (like Clojure).
+
+
+## Implementation notes
+
+The list provided here is a singly-linked list and is very trivial to implement.
+
+The implementation of persistent vector and hash map and based on a series of [excellent](http://blog.higher-order.net/2009/02/01/understanding-clojures-persistentvector-implementation) [blog](http://blog.higher-order.net/2009/09/08/understanding-clojures-persistenthashmap-deftwice) [posts](http://blog.higher-order.net/2010/08/16/assoc-and-clojures-persistenthashmap-part-ii.html) as well as the Clojure source code. Despite the hash map appearing more complicated, the vector is slightly harder to implement due to the "tail array" optimization and some tricky transformation of the tree structure, which is fully replicated here.
+
+## Benchmarking results
+
+### Vectors
+
+Compared to native slices,
+
+*   Adding elements is anywhere from 2x to 8x as slow.
+
+*   Sequential read is about 9x as slow.
+
+*   Random read is about 7x as slow.
+
+Benchmarked on an early 2015 MacBook Pro, with Go 1.9:
+
+```
+goos: darwin
+goarch: amd64
+pkg: github.com/xiaq/persistent/vector
+BenchmarkConsNativeN1-4                  1000000              2457 ns/op
+BenchmarkConsNativeN2-4                   300000              4418 ns/op
+BenchmarkConsNativeN3-4                    30000             55424 ns/op
+BenchmarkConsNativeN4-4                      300           4493289 ns/op
+BenchmarkConsPersistentN1-4               100000             12250 ns/op 4.99x
+BenchmarkConsPersistentN2-4                50000             26394 ns/op 5.97x
+BenchmarkConsPersistentN3-4                 3000            452146 ns/op 8.16x
+BenchmarkConsPersistentN4-4                  100          13057887 ns/op 2.91x
+BenchmarkNthSeqNativeN4-4                  30000             43156 ns/op
+BenchmarkNthSeqPersistentN4-4               3000            399193 ns/op 9.25x
+BenchmarkNthRandNative-4                   20000             73860 ns/op
+BenchmarkNthRandPersistent-4                3000            546124 ns/op 7.39x
+BenchmarkEqualNative-4                     50000             23828 ns/op
+BenchmarkEqualPersistent-4                  2000           1020893 ns/op 42.84x
+```
+
+### Hash map
+
+Compared to native maps, adding elements is about 3-6x slow. Difference is
+more pronunced when keys are sequential integers, but that workload is very
+rare in the real world.
+
+Benchmarked on an early 2015 MacBook Pro, with Go 1.9:
+
+```
+goos: darwin
+goarch: amd64
+pkg: github.com/xiaq/persistent/hashmap
+BenchmarkSequentialConsNative1-4                  300000              4143 ns/op
+BenchmarkSequentialConsNative2-4                   10000            130423 ns/op
+BenchmarkSequentialConsNative3-4                     300           4600842 ns/op
+BenchmarkSequentialConsPersistent1-4              100000             14005 ns/op 3.38x
+BenchmarkSequentialConsPersistent2-4                2000            641820 ns/op 4.92x
+BenchmarkSequentialConsPersistent3-4                  20          55180306 ns/op 11.99x
+BenchmarkRandomStringsConsNative1-4               200000              7536 ns/op
+BenchmarkRandomStringsConsNative2-4                 5000            264489 ns/op
+BenchmarkRandomStringsConsNative3-4                  100          12132244 ns/op
+BenchmarkRandomStringsConsPersistent1-4            50000             29109 ns/op 3.86x
+BenchmarkRandomStringsConsPersistent2-4             1000           1327321 ns/op 5.02x
+BenchmarkRandomStringsConsPersistent3-4               20          74204196 ns/op 6.12x
+```

+ 33 - 0
pkg/persistent/add-slowdown

@@ -0,0 +1,33 @@
+#!/usr/bin/env elvish
+# Parse an output of "go test -bench .", annotating benchmark results for
+# persistent operations with the slowdown ratio compared to their native
+# counterparts.
+use re
+
+fn extract [line]{
+    # Extract the name and ns/op of a benchmark entry.
+    fields = [(re:split '\s+' $line)]
+    if (not (eq $fields[-1] ns/op)) {
+        fail 'Last column of '(repr $line)' not ns/op'
+    }
+    put $fields[0] $fields[-2]
+}
+
+native = [&]
+
+each [line]{
+    if (re:match Native $line) {
+        # Remember the result so that it can be used later.
+        name data = (extract $line)
+        native[$name] = $data
+    } elif (re:match Persistent $line) {
+        # Calculate slowdown and append to the end of the line.
+        name data = (extract $line)
+        native-name = (re:replace Persistent Native $name)
+        if (not (has-key $native $native-name)) {
+            fail 'Native counterpart for '$name' not found'
+        }
+        line = $line' '(printf '%.2f' (/ $data $native[$native-name]))'x'
+    }
+    echo $line
+}

+ 3 - 0
pkg/persistent/go.mod

@@ -0,0 +1,3 @@
+module github.com/xiaq/persistent
+
+go 1.12

+ 56 - 0
pkg/persistent/hash/hash.go

@@ -0,0 +1,56 @@
+// Package hash contains some common hash functions suitable for use in hash
+// maps.
+package hash
+
+import "unsafe"
+
+const DJBInit uint32 = 5381
+
+func DJBCombine(acc, h uint32) uint32 {
+	return mul33(acc) + h
+}
+
+func DJB(hs ...uint32) uint32 {
+	acc := DJBInit
+	for _, h := range hs {
+		acc = DJBCombine(acc, h)
+	}
+	return acc
+}
+
+func UInt32(u uint32) uint32 {
+	return u
+}
+
+func UInt64(u uint64) uint32 {
+	return mul33(uint32(u>>32)) + uint32(u&0xffffffff)
+}
+
+func Pointer(p unsafe.Pointer) uint32 {
+	if unsafe.Sizeof(p) == 4 {
+		return UInt32(uint32(uintptr(p)))
+	} else {
+		return UInt64(uint64(uintptr(p)))
+	}
+	// NOTE: We don't care about 128-bit archs yet.
+}
+
+func UIntPtr(u uintptr) uint32 {
+	if unsafe.Sizeof(u) == 4 {
+		return UInt32(uint32(u))
+	} else {
+		return UInt64(uint64(u))
+	}
+}
+
+func String(s string) uint32 {
+	h := DJBInit
+	for i := 0; i < len(s); i++ {
+		h = DJBCombine(h, uint32(s[i]))
+	}
+	return h
+}
+
+func mul33(u uint32) uint32 {
+	return u<<5 + u
+}

+ 587 - 0
pkg/persistent/hashmap/hashmap.go

@@ -0,0 +1,587 @@
+// Package hashmap implements persistent hashmap.
+package hashmap
+
+import (
+	"bytes"
+	"encoding"
+	"encoding/json"
+	"fmt"
+	"reflect"
+	"strconv"
+)
+
+const (
+	chunkBits = 5
+	nodeCap   = 1 << chunkBits
+	chunkMask = nodeCap - 1
+)
+
+// Equal is the type of a function that reports whether two keys are equal.
+type Equal func(k1, k2 interface{}) bool
+
+// Hash is the type of a function that returns the hash code of a key.
+type Hash func(k interface{}) uint32
+
+// New takes an equality function and a hash function, and returns an empty
+// Map.
+func New(e Equal, h Hash) Map {
+	return &hashMap{0, emptyBitmapNode, nil, e, h}
+}
+
+type hashMap struct {
+	count int
+	root  node
+	nilV  *interface{}
+	equal Equal
+	hash  Hash
+}
+
+func (m *hashMap) Len() int {
+	return m.count
+}
+
+func (m *hashMap) Index(k interface{}) (interface{}, bool) {
+	if k == nil {
+		if m.nilV == nil {
+			return nil, false
+		}
+		return *m.nilV, true
+	}
+	return m.root.find(0, m.hash(k), k, m.equal)
+}
+
+func (m *hashMap) Assoc(k, v interface{}) Map {
+	if k == nil {
+		newCount := m.count
+		if m.nilV == nil {
+			newCount++
+		}
+		return &hashMap{newCount, m.root, &v, m.equal, m.hash}
+	}
+	newRoot, added := m.root.assoc(0, m.hash(k), k, v, m.hash, m.equal)
+	newCount := m.count
+	if added {
+		newCount++
+	}
+	return &hashMap{newCount, newRoot, m.nilV, m.equal, m.hash}
+}
+
+func (m *hashMap) Dissoc(k interface{}) Map {
+	if k == nil {
+		newCount := m.count
+		if m.nilV != nil {
+			newCount--
+		}
+		return &hashMap{newCount, m.root, nil, m.equal, m.hash}
+	}
+	newRoot, deleted := m.root.without(0, m.hash(k), k, m.equal)
+	newCount := m.count
+	if deleted {
+		newCount--
+	}
+	return &hashMap{newCount, newRoot, m.nilV, m.equal, m.hash}
+}
+
+func (m *hashMap) Iterator() Iterator {
+	if m.nilV != nil {
+		return &nilVIterator{true, *m.nilV, m.root.iterator()}
+	}
+	return m.root.iterator()
+}
+
+type nilVIterator struct {
+	atNil bool
+	nilV  interface{}
+	tail  Iterator
+}
+
+func (it *nilVIterator) Elem() (interface{}, interface{}) {
+	if it.atNil {
+		return nil, it.nilV
+	}
+	return it.tail.Elem()
+}
+
+func (it *nilVIterator) HasElem() bool {
+	return it.atNil || it.tail.HasElem()
+}
+
+func (it *nilVIterator) Next() {
+	if it.atNil {
+		it.atNil = false
+	} else {
+		it.tail.Next()
+	}
+}
+
+func (m *hashMap) MarshalJSON() ([]byte, error) {
+	var buf bytes.Buffer
+	buf.WriteByte('{')
+	first := true
+	for it := m.Iterator(); it.HasElem(); it.Next() {
+		if first {
+			first = false
+		} else {
+			buf.WriteByte(',')
+		}
+		k, v := it.Elem()
+		kString, err := convertKey(k)
+		if err != nil {
+			return nil, err
+		}
+		kBytes, err := json.Marshal(kString)
+		if err != nil {
+			return nil, err
+		}
+		vBytes, err := json.Marshal(v)
+		if err != nil {
+			return nil, err
+		}
+		buf.Write(kBytes)
+		buf.WriteByte(':')
+		buf.Write(vBytes)
+	}
+	buf.WriteByte('}')
+	return buf.Bytes(), nil
+}
+
+// convertKey converts a map key to a string. The implementation matches the
+// behavior of how json.Marshal encodes keys of the builtin map type.
+func convertKey(k interface{}) (string, error) {
+	kref := reflect.ValueOf(k)
+	if kref.Kind() == reflect.String {
+		return kref.String(), nil
+	}
+	if t, ok := k.(encoding.TextMarshaler); ok {
+		b2, err := t.MarshalText()
+		if err != nil {
+			return "", err
+		}
+		return string(b2), nil
+	}
+	switch kref.Kind() {
+	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+		return strconv.FormatInt(kref.Int(), 10), nil
+	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+		return strconv.FormatUint(kref.Uint(), 10), nil
+	}
+	return "", fmt.Errorf("unsupported key type %T", k)
+}
+
+// node is an interface for all nodes in the hash map tree.
+type node interface {
+	// assoc adds a new pair of key and value. It returns the new node, and
+	// whether the key did not exist before (i.e. a new pair has been added,
+	// instead of replaced).
+	assoc(shift, hash uint32, k, v interface{}, h Hash, eq Equal) (node, bool)
+	// without removes a key. It returns the new node and whether the key did
+	// not exist before (i.e. a key was indeed removed).
+	without(shift, hash uint32, k interface{}, eq Equal) (node, bool)
+	// find finds the value for a key. It returns the found value (if any) and
+	// whether such a pair exists.
+	find(shift, hash uint32, k interface{}, eq Equal) (interface{}, bool)
+	// iterator returns an iterator.
+	iterator() Iterator
+}
+
+// arrayNode stores all of its children in an array. The array is always at
+// least 1/4 full, otherwise it will be packed into a bitmapNode.
+type arrayNode struct {
+	nChildren int
+	children  [nodeCap]node
+}
+
+func (n *arrayNode) withNewChild(i uint32, newChild node, d int) *arrayNode {
+	newChildren := n.children
+	newChildren[i] = newChild
+	return &arrayNode{n.nChildren + d, newChildren}
+}
+
+func (n *arrayNode) assoc(shift, hash uint32, k, v interface{}, h Hash, eq Equal) (node, bool) {
+	idx := chunk(shift, hash)
+	child := n.children[idx]
+	if child == nil {
+		newChild, _ := emptyBitmapNode.assoc(shift+chunkBits, hash, k, v, h, eq)
+		return n.withNewChild(idx, newChild, 1), true
+	}
+	newChild, added := child.assoc(shift+chunkBits, hash, k, v, h, eq)
+	return n.withNewChild(idx, newChild, 0), added
+}
+
+func (n *arrayNode) without(shift, hash uint32, k interface{}, eq Equal) (node, bool) {
+	idx := chunk(shift, hash)
+	child := n.children[idx]
+	if child == nil {
+		return n, false
+	}
+	newChild, _ := child.without(shift+chunkBits, hash, k, eq)
+	if newChild == child {
+		return n, false
+	}
+	if newChild == emptyBitmapNode {
+		if n.nChildren <= nodeCap/4 {
+			// less than 1/4 full; shrink
+			return n.pack(int(idx)), true
+		}
+		return n.withNewChild(idx, nil, -1), true
+	}
+	return n.withNewChild(idx, newChild, 0), true
+}
+
+func (n *arrayNode) pack(skip int) *bitmapNode {
+	newNode := bitmapNode{0, make([]mapEntry, n.nChildren-1)}
+	j := 0
+	for i, child := range n.children {
+		// TODO(xiaq): Benchmark performance difference after unrolling this
+		// into two loops without the if
+		if i != skip && child != nil {
+			newNode.bitmap |= 1 << uint(i)
+			newNode.entries[j].value = child
+			j++
+		}
+	}
+	return &newNode
+}
+
+func (n *arrayNode) find(shift, hash uint32, k interface{}, eq Equal) (interface{}, bool) {
+	idx := chunk(shift, hash)
+	child := n.children[idx]
+	if child == nil {
+		return nil, false
+	}
+	return child.find(shift+chunkBits, hash, k, eq)
+}
+
+func (n *arrayNode) iterator() Iterator {
+	it := &arrayNodeIterator{n, 0, nil}
+	it.fixCurrent()
+	return it
+}
+
+type arrayNodeIterator struct {
+	n       *arrayNode
+	index   int
+	current Iterator
+}
+
+func (it *arrayNodeIterator) fixCurrent() {
+	for ; it.index < nodeCap && it.n.children[it.index] == nil; it.index++ {
+	}
+	if it.index < nodeCap {
+		it.current = it.n.children[it.index].iterator()
+	} else {
+		it.current = nil
+	}
+}
+
+func (it *arrayNodeIterator) Elem() (interface{}, interface{}) {
+	return it.current.Elem()
+}
+
+func (it *arrayNodeIterator) HasElem() bool {
+	return it.current != nil
+}
+
+func (it *arrayNodeIterator) Next() {
+	it.current.Next()
+	if !it.current.HasElem() {
+		it.index++
+		it.fixCurrent()
+	}
+}
+
+var emptyBitmapNode = &bitmapNode{}
+
+type bitmapNode struct {
+	bitmap  uint32
+	entries []mapEntry
+}
+
+// mapEntry is a map entry. When used in a collisionNode, it is also an entry
+// with non-nil key. When used in a bitmapNode, it is also abused to represent
+// children when the key is nil.
+type mapEntry struct {
+	key   interface{}
+	value interface{}
+}
+
+func chunk(shift, hash uint32) uint32 {
+	return (hash >> shift) & chunkMask
+}
+
+func bitpos(shift, hash uint32) uint32 {
+	return 1 << chunk(shift, hash)
+}
+
+func index(bitmap, bit uint32) uint32 {
+	return popCount(bitmap & (bit - 1))
+}
+
+const (
+	m1  uint32 = 0x55555555
+	m2         = 0x33333333
+	m4         = 0x0f0f0f0f
+	m8         = 0x00ff00ff
+	m16        = 0x0000ffff
+)
+
+// TODO(xiaq): Use an optimized implementation.
+func popCount(u uint32) uint32 {
+	u = (u & m1) + ((u >> 1) & m1)
+	u = (u & m2) + ((u >> 2) & m2)
+	u = (u & m4) + ((u >> 4) & m4)
+	u = (u & m8) + ((u >> 8) & m8)
+	u = (u & m16) + ((u >> 16) & m16)
+	return u
+}
+
+func createNode(shift uint32, k1 interface{}, v1 interface{}, h2 uint32, k2 interface{}, v2 interface{}, h Hash, eq Equal) node {
+	h1 := h(k1)
+	if h1 == h2 {
+		return &collisionNode{h1, []mapEntry{{k1, v1}, {k2, v2}}}
+	}
+	n, _ := emptyBitmapNode.assoc(shift, h1, k1, v1, h, eq)
+	n, _ = n.assoc(shift, h2, k2, v2, h, eq)
+	return n
+}
+
+func (n *bitmapNode) unpack(shift, idx uint32, newChild node, h Hash, eq Equal) *arrayNode {
+	var newNode arrayNode
+	newNode.nChildren = len(n.entries) + 1
+	newNode.children[idx] = newChild
+	j := 0
+	for i := uint(0); i < nodeCap; i++ {
+		if (n.bitmap>>i)&1 != 0 {
+			entry := n.entries[j]
+			j++
+			if entry.key == nil {
+				newNode.children[i] = entry.value.(node)
+			} else {
+				newNode.children[i], _ = emptyBitmapNode.assoc(
+					shift+chunkBits, h(entry.key), entry.key, entry.value, h, eq)
+			}
+		}
+	}
+	return &newNode
+}
+
+func (n *bitmapNode) withoutEntry(bit, idx uint32) *bitmapNode {
+	if n.bitmap == bit {
+		return emptyBitmapNode
+	}
+	return &bitmapNode{n.bitmap ^ bit, withoutEntry(n.entries, idx)}
+}
+
+func withoutEntry(entries []mapEntry, idx uint32) []mapEntry {
+	newEntries := make([]mapEntry, len(entries)-1)
+	copy(newEntries[:idx], entries[:idx])
+	copy(newEntries[idx:], entries[idx+1:])
+	return newEntries
+}
+
+func (n *bitmapNode) withReplacedEntry(i uint32, entry mapEntry) *bitmapNode {
+	return &bitmapNode{n.bitmap, replaceEntry(n.entries, i, entry.key, entry.value)}
+}
+
+func replaceEntry(entries []mapEntry, i uint32, k, v interface{}) []mapEntry {
+	newEntries := append([]mapEntry(nil), entries...)
+	newEntries[i] = mapEntry{k, v}
+	return newEntries
+}
+
+func (n *bitmapNode) assoc(shift, hash uint32, k, v interface{}, h Hash, eq Equal) (node, bool) {
+	bit := bitpos(shift, hash)
+	idx := index(n.bitmap, bit)
+	if n.bitmap&bit == 0 {
+		// Entry does not exist yet
+		nEntries := len(n.entries)
+		if nEntries >= nodeCap/2 {
+			// Unpack into an arrayNode
+			newNode, _ := emptyBitmapNode.assoc(shift+chunkBits, hash, k, v, h, eq)
+			return n.unpack(shift, chunk(shift, hash), newNode, h, eq), true
+		}
+		// Add a new entry
+		newEntries := make([]mapEntry, len(n.entries)+1)
+		copy(newEntries[:idx], n.entries[:idx])
+		newEntries[idx] = mapEntry{k, v}
+		copy(newEntries[idx+1:], n.entries[idx:])
+		return &bitmapNode{n.bitmap | bit, newEntries}, true
+	}
+	// Entry exists
+	entry := n.entries[idx]
+	if entry.key == nil {
+		// Non-leaf child
+		child := entry.value.(node)
+		newChild, added := child.assoc(shift+chunkBits, hash, k, v, h, eq)
+		return n.withReplacedEntry(idx, mapEntry{nil, newChild}), added
+	}
+	// Leaf
+	if eq(k, entry.key) {
+		// Identical key, replace
+		return n.withReplacedEntry(idx, mapEntry{k, v}), false
+	}
+	// Create and insert new inner node
+	newNode := createNode(shift+chunkBits, entry.key, entry.value, hash, k, v, h, eq)
+	return n.withReplacedEntry(idx, mapEntry{nil, newNode}), true
+}
+
+func (n *bitmapNode) without(shift, hash uint32, k interface{}, eq Equal) (node, bool) {
+	bit := bitpos(shift, hash)
+	if n.bitmap&bit == 0 {
+		return n, false
+	}
+	idx := index(n.bitmap, bit)
+	entry := n.entries[idx]
+	if entry.key == nil {
+		// Non-leaf child
+		child := entry.value.(node)
+		newChild, deleted := child.without(shift+chunkBits, hash, k, eq)
+		if newChild == child {
+			return n, false
+		}
+		if newChild == emptyBitmapNode {
+			return n.withoutEntry(bit, idx), true
+		}
+		return n.withReplacedEntry(idx, mapEntry{nil, newChild}), deleted
+	} else if eq(entry.key, k) {
+		// Leaf, and this is the entry to delete.
+		return n.withoutEntry(bit, idx), true
+	}
+	// Nothing to delete.
+	return n, false
+}
+
+func (n *bitmapNode) find(shift, hash uint32, k interface{}, eq Equal) (interface{}, bool) {
+	bit := bitpos(shift, hash)
+	if n.bitmap&bit == 0 {
+		return nil, false
+	}
+	idx := index(n.bitmap, bit)
+	entry := n.entries[idx]
+	if entry.key == nil {
+		child := entry.value.(node)
+		return child.find(shift+chunkBits, hash, k, eq)
+	} else if eq(entry.key, k) {
+		return entry.value, true
+	}
+	return nil, false
+}
+
+func (n *bitmapNode) iterator() Iterator {
+	it := &bitmapNodeIterator{n, 0, nil}
+	it.fixCurrent()
+	return it
+}
+
+type bitmapNodeIterator struct {
+	n       *bitmapNode
+	index   int
+	current Iterator
+}
+
+func (it *bitmapNodeIterator) fixCurrent() {
+	if it.index < len(it.n.entries) {
+		entry := it.n.entries[it.index]
+		if entry.key == nil {
+			it.current = entry.value.(node).iterator()
+		} else {
+			it.current = nil
+		}
+	} else {
+		it.current = nil
+	}
+}
+
+func (it *bitmapNodeIterator) Elem() (interface{}, interface{}) {
+	if it.current != nil {
+		return it.current.Elem()
+	}
+	entry := it.n.entries[it.index]
+	return entry.key, entry.value
+}
+
+func (it *bitmapNodeIterator) HasElem() bool {
+	return it.index < len(it.n.entries)
+}
+
+func (it *bitmapNodeIterator) Next() {
+	if it.current != nil {
+		it.current.Next()
+	}
+	if it.current == nil || !it.current.HasElem() {
+		it.index++
+		it.fixCurrent()
+	}
+}
+
+type collisionNode struct {
+	hash    uint32
+	entries []mapEntry
+}
+
+func (n *collisionNode) assoc(shift, hash uint32, k, v interface{}, h Hash, eq Equal) (node, bool) {
+	if hash == n.hash {
+		idx := n.findIndex(k, eq)
+		if idx != -1 {
+			return &collisionNode{
+				n.hash, replaceEntry(n.entries, uint32(idx), k, v)}, false
+		}
+		newEntries := make([]mapEntry, len(n.entries)+1)
+		copy(newEntries[:len(n.entries)], n.entries[:])
+		newEntries[len(n.entries)] = mapEntry{k, v}
+		return &collisionNode{n.hash, newEntries}, true
+	}
+	// Wrap in a bitmapNode and add the entry
+	wrap := bitmapNode{bitpos(shift, n.hash), []mapEntry{{nil, n}}}
+	return wrap.assoc(shift, hash, k, v, h, eq)
+}
+
+func (n *collisionNode) without(shift, hash uint32, k interface{}, eq Equal) (node, bool) {
+	idx := n.findIndex(k, eq)
+	if idx == -1 {
+		return n, false
+	}
+	if len(n.entries) == 1 {
+		return emptyBitmapNode, true
+	}
+	return &collisionNode{n.hash, withoutEntry(n.entries, uint32(idx))}, true
+}
+
+func (n *collisionNode) find(shift, hash uint32, k interface{}, eq Equal) (interface{}, bool) {
+	idx := n.findIndex(k, eq)
+	if idx == -1 {
+		return nil, false
+	}
+	return n.entries[idx].value, true
+}
+
+func (n *collisionNode) findIndex(k interface{}, eq Equal) int {
+	for i, entry := range n.entries {
+		if eq(k, entry.key) {
+			return i
+		}
+	}
+	return -1
+}
+
+func (n *collisionNode) iterator() Iterator {
+	return &collisionNodeIterator{n, 0}
+}
+
+type collisionNodeIterator struct {
+	n     *collisionNode
+	index int
+}
+
+func (it *collisionNodeIterator) Elem() (interface{}, interface{}) {
+	entry := it.n.entries[it.index]
+	return entry.key, entry.value
+}
+
+func (it *collisionNodeIterator) HasElem() bool {
+	return it.index < len(it.n.entries)
+}
+
+func (it *collisionNodeIterator) Next() {
+	it.index++
+}

+ 382 - 0
pkg/persistent/hashmap/hashmap_test.go

@@ -0,0 +1,382 @@
+package hashmap
+
+import (
+	"math/rand"
+	"reflect"
+	"strconv"
+	"testing"
+	"time"
+
+	"github.com/xiaq/persistent/hash"
+)
+
+const (
+	NSequential = 0x1000
+	NCollision  = 0x100
+	NRandom     = 0x4000
+	NReplace    = 0x200
+
+	SmallRandomPass      = 0x100
+	NSmallRandom         = 0x400
+	SmallRandomHighBound = 0x50
+	SmallRandomLowBound  = 0x200
+
+	NArrayNode = 0x100
+
+	NIneffectiveDissoc = 0x200
+
+	N1 = nodeCap + 1
+	N2 = nodeCap*nodeCap + 1
+	N3 = nodeCap*nodeCap*nodeCap + 1
+)
+
+type testKey uint64
+type anotherTestKey uint32
+
+func equalFunc(k1, k2 interface{}) bool {
+	switch k1 := k1.(type) {
+	case testKey:
+		t2, ok := k2.(testKey)
+		return ok && k1 == t2
+	case anotherTestKey:
+		return false
+	default:
+		return k1 == k2
+	}
+}
+
+func hashFunc(k interface{}) uint32 {
+	switch k := k.(type) {
+	case uint32:
+		return k
+	case string:
+		return hash.String(k)
+	case testKey:
+		// Return the lower 32 bits for testKey. This is intended so that hash
+		// collisions can be easily constructed.
+		return uint32(k & 0xffffffff)
+	case anotherTestKey:
+		return uint32(k)
+	default:
+		return 0
+	}
+}
+
+var empty = New(equalFunc, hashFunc)
+
+type refEntry struct {
+	k testKey
+	v string
+}
+
+func hex(i uint64) string {
+	return "0x" + strconv.FormatUint(i, 16)
+}
+
+func init() {
+	rand.Seed(time.Now().UTC().UnixNano())
+}
+
+var randomStrings []string
+
+// getRandomStrings returns a slice of N3 random strings. It builds the slice
+// once and caches it. If the slice is built for the first time, it stops the
+// timer of the benchmark.
+func getRandomStrings(b *testing.B) []string {
+	if randomStrings == nil {
+		b.StopTimer()
+		defer b.StartTimer()
+		randomStrings = make([]string, N3)
+		for i := 0; i < N3; i++ {
+			randomStrings[i] = makeRandomString()
+		}
+	}
+	return randomStrings
+}
+
+// makeRandomString builds a random string consisting of n bytes (randomized
+// between 0 and 99) and each byte is randomized between 0 and 255. The string
+// need not be valid UTF-8.
+func makeRandomString() string {
+	bytes := make([]byte, rand.Intn(100))
+	for i := range bytes {
+		bytes[i] = byte(rand.Intn(256))
+	}
+	return string(bytes)
+}
+
+func TestHashMap(t *testing.T) {
+	var refEntries []refEntry
+	add := func(k testKey, v string) {
+		refEntries = append(refEntries, refEntry{k, v})
+	}
+
+	for i := 0; i < NSequential; i++ {
+		add(testKey(i), hex(uint64(i)))
+	}
+	for i := 0; i < NCollision; i++ {
+		add(testKey(uint64(i+1)<<32), "collision "+hex(uint64(i)))
+	}
+	for i := 0; i < NRandom; i++ {
+		// Avoid rand.Uint64 for compatibility with pre 1.8 Go
+		k := uint64(rand.Int63())>>31 | uint64(rand.Int63())<<32
+		add(testKey(k), "random "+hex(k))
+	}
+	for i := 0; i < NReplace; i++ {
+		k := uint64(rand.Int31n(NSequential))
+		add(testKey(k), "replace "+hex(k))
+	}
+
+	testHashMapWithRefEntries(t, refEntries)
+}
+
+func TestHashMapSmallRandom(t *testing.T) {
+	for p := 0; p < SmallRandomPass; p++ {
+		var refEntries []refEntry
+		add := func(k testKey, v string) {
+			refEntries = append(refEntries, refEntry{k, v})
+		}
+
+		for i := 0; i < NSmallRandom; i++ {
+			k := uint64(uint64(rand.Int31n(SmallRandomHighBound))<<32 |
+				uint64(rand.Int31n(SmallRandomLowBound)))
+			add(testKey(k), "random "+hex(k))
+		}
+
+		testHashMapWithRefEntries(t, refEntries)
+	}
+}
+
+var marshalJSONTests = []struct {
+	in      Map
+	wantOut string
+	wantErr bool
+}{
+	{makeHashMap(uint32(1), "a", "2", "b"), `{"1":"a","2":"b"}`, false},
+	// Invalid key type
+	{makeHashMap([]interface{}{}, "x"), "", true},
+}
+
+func TestMarshalJSON(t *testing.T) {
+	for i, test := range marshalJSONTests {
+		out, err := test.in.MarshalJSON()
+		if string(out) != test.wantOut {
+			t.Errorf("m%d.MarshalJSON -> out %s, want %s", i, out, test.wantOut)
+		}
+		if (err != nil) != test.wantErr {
+			var wantErr string
+			if test.wantErr {
+				wantErr = "non-nil"
+			} else {
+				wantErr = "nil"
+			}
+			t.Errorf("m%d.MarshalJSON -> err %v, want %s", i, err, wantErr)
+		}
+	}
+}
+
+func makeHashMap(data ...interface{}) Map {
+	m := empty
+	for i := 0; i+1 < len(data); i += 2 {
+		k, v := data[i], data[i+1]
+		m = m.Assoc(k, v)
+	}
+	return m
+}
+
+// testHashMapWithRefEntries tests the operations of a Map. It uses the supplied
+// list of entries to build the map, and then test all its operations.
+func testHashMapWithRefEntries(t *testing.T, refEntries []refEntry) {
+	m := empty
+	// Len of Empty should be 0.
+	if m.Len() != 0 {
+		t.Errorf("m.Len = %d, want %d", m.Len(), 0)
+	}
+
+	// Assoc and Len, test by building a map simutaneously.
+	ref := make(map[testKey]string, len(refEntries))
+	for _, e := range refEntries {
+		ref[e.k] = e.v
+		m = m.Assoc(e.k, e.v)
+		if m.Len() != len(ref) {
+			t.Errorf("m.Len = %d, want %d", m.Len(), len(ref))
+		}
+	}
+
+	// Index.
+	testMapContent(t, m, ref)
+	got, in := m.Index(anotherTestKey(0))
+	if in {
+		t.Errorf("m.Index <bad key> returns entry %v", got)
+	}
+	// Iterator.
+	testIterator(t, m, ref)
+
+	// Dissoc.
+	// Ineffective ones.
+	for i := 0; i < NIneffectiveDissoc; i++ {
+		k := anotherTestKey(uint32(rand.Int31())>>15 | uint32(rand.Int31())<<16)
+		m = m.Dissoc(k)
+		if m.Len() != len(ref) {
+			t.Errorf("m.Dissoc removes item when it shouldn't")
+		}
+	}
+
+	// Effective ones.
+	for x := 0; x < len(refEntries); x++ {
+		i := rand.Intn(len(refEntries))
+		k := refEntries[i].k
+		delete(ref, k)
+		m = m.Dissoc(k)
+		if m.Len() != len(ref) {
+			t.Errorf("m.Len() = %d after removing, should be %v", m.Len(), len(ref))
+		}
+		_, in := m.Index(k)
+		if in {
+			t.Errorf("m.Index(%v) still returns item after removal", k)
+		}
+		// Checking all elements is expensive. Only do this 1% of the time.
+		if rand.Float64() < 0.01 {
+			testMapContent(t, m, ref)
+			testIterator(t, m, ref)
+		}
+	}
+}
+
+func testMapContent(t *testing.T, m Map, ref map[testKey]string) {
+	for k, v := range ref {
+		got, in := m.Index(k)
+		if !in {
+			t.Errorf("m.Index 0x%x returns no entry", k)
+		}
+		if got != v {
+			t.Errorf("m.Index(0x%x) = %v, want %v", k, got, v)
+		}
+	}
+}
+
+func testIterator(t *testing.T, m Map, ref map[testKey]string) {
+	ref2 := map[interface{}]interface{}{}
+	for k, v := range ref {
+		ref2[k] = v
+	}
+	for it := m.Iterator(); it.HasElem(); it.Next() {
+		k, v := it.Elem()
+		if ref2[k] != v {
+			t.Errorf("iterator yields unexpected pair %v, %v", k, v)
+		}
+		delete(ref2, k)
+	}
+	if len(ref2) != 0 {
+		t.Errorf("iterating was not exhaustive")
+	}
+}
+
+func TestNilKey(t *testing.T) {
+	m := empty
+
+	testLen := func(l int) {
+		if m.Len() != l {
+			t.Errorf(".Len -> %d, want %d", m.Len(), l)
+		}
+	}
+	testIndex := func(wantVal interface{}, wantOk bool) {
+		val, ok := m.Index(nil)
+		if val != wantVal {
+			t.Errorf(".Index -> %v, want %v", val, wantVal)
+		}
+		if ok != wantOk {
+			t.Errorf(".Index -> ok %v, want %v", ok, wantOk)
+		}
+	}
+
+	testLen(0)
+	testIndex(nil, false)
+
+	m = m.Assoc(nil, "nil value")
+	testLen(1)
+	testIndex("nil value", true)
+
+	m = m.Assoc(nil, "nil value 2")
+	testLen(1)
+	testIndex("nil value 2", true)
+
+	m = m.Dissoc(nil)
+	testLen(0)
+	testIndex(nil, false)
+}
+
+func TestIterateMapWithNilKey(t *testing.T) {
+	m := empty.Assoc("k", "v").Assoc(nil, "nil value")
+	var collected []interface{}
+	for it := m.Iterator(); it.HasElem(); it.Next() {
+		k, v := it.Elem()
+		collected = append(collected, k, v)
+	}
+	wantCollected := []interface{}{nil, "nil value", "k", "v"}
+	if !reflect.DeepEqual(collected, wantCollected) {
+		t.Errorf("collected %v, want %v", collected, wantCollected)
+	}
+}
+
+func BenchmarkSequentialConsNative1(b *testing.B) { nativeSequentialAdd(b.N, N1) }
+func BenchmarkSequentialConsNative2(b *testing.B) { nativeSequentialAdd(b.N, N2) }
+func BenchmarkSequentialConsNative3(b *testing.B) { nativeSequentialAdd(b.N, N3) }
+
+// nativeSequntialAdd starts with an empty native map and adds elements 0...n-1
+// to the map, using the same value as the key, repeating for N times.
+func nativeSequentialAdd(N int, n uint32) {
+	for r := 0; r < N; r++ {
+		m := make(map[uint32]uint32)
+		for i := uint32(0); i < n; i++ {
+			m[i] = i
+		}
+	}
+}
+
+func BenchmarkSequentialConsPersistent1(b *testing.B) { sequentialCons(b.N, N1) }
+func BenchmarkSequentialConsPersistent2(b *testing.B) { sequentialCons(b.N, N2) }
+func BenchmarkSequentialConsPersistent3(b *testing.B) { sequentialCons(b.N, N3) }
+
+// sequentialCons starts with an empty hash map and adds elements 0...n-1 to the
+// map, using the same value as the key, repeating for N times.
+func sequentialCons(N int, n uint32) {
+	for r := 0; r < N; r++ {
+		m := empty
+		for i := uint32(0); i < n; i++ {
+			m = m.Assoc(i, i)
+		}
+	}
+}
+
+func BenchmarkRandomStringsConsNative1(b *testing.B) { nativeRandomStringsAdd(b, N1) }
+func BenchmarkRandomStringsConsNative2(b *testing.B) { nativeRandomStringsAdd(b, N2) }
+func BenchmarkRandomStringsConsNative3(b *testing.B) { nativeRandomStringsAdd(b, N3) }
+
+// nativeSequntialAdd starts with an empty native map and adds n random strings
+// to the map, using the same value as the key, repeating for b.N times.
+func nativeRandomStringsAdd(b *testing.B, n int) {
+	ss := getRandomStrings(b)
+	for r := 0; r < b.N; r++ {
+		m := make(map[string]string)
+		for i := 0; i < n; i++ {
+			s := ss[i]
+			m[s] = s
+		}
+	}
+}
+
+func BenchmarkRandomStringsConsPersistent1(b *testing.B) { randomStringsCons(b, N1) }
+func BenchmarkRandomStringsConsPersistent2(b *testing.B) { randomStringsCons(b, N2) }
+func BenchmarkRandomStringsConsPersistent3(b *testing.B) { randomStringsCons(b, N3) }
+
+func randomStringsCons(b *testing.B, n int) {
+	ss := getRandomStrings(b)
+	for r := 0; r < b.N; r++ {
+		m := empty
+		for i := 0; i < n; i++ {
+			s := ss[i]
+			m = m.Assoc(s, s)
+		}
+	}
+}

+ 45 - 0
pkg/persistent/hashmap/map.go

@@ -0,0 +1,45 @@
+package hashmap
+
+import "encoding/json"
+
+// Map is a persistent associative data structure mapping keys to values. It
+// is immutable, and supports near-O(1) operations to create modified version of
+// the map that shares the underlying data structure. Because it is immutable,
+// all of its methods are safe for concurrent use.
+type Map interface {
+	json.Marshaler
+	// Len returns the length of the map.
+	Len() int
+	// Index returns whether there is a value associated with the given key, and
+	// that value or nil.
+	Index(k interface{}) (interface{}, bool)
+	// Assoc returns an almost identical map, with the given key associated with
+	// the given value.
+	Assoc(k, v interface{}) Map
+	// Dissoc returns an almost identical map, with the given key associated
+	// with no value.
+	Dissoc(k interface{}) Map
+	// Iterator returns an iterator over the map.
+	Iterator() Iterator
+}
+
+// Iterator is an iterator over map elements. It can be used like this:
+//
+//     for it := m.Iterator(); it.HasElem(); it.Next() {
+//         key, value := it.Elem()
+//         // do something with elem...
+//     }
+type Iterator interface {
+	// Elem returns the current key-value pair.
+	Elem() (interface{}, interface{})
+	// HasElem returns whether the iterator is pointing to an element.
+	HasElem() bool
+	// Next moves the iterator to the next position.
+	Next()
+}
+
+// HasKey reports whether a Map has the given key.
+func HasKey(m Map, k interface{}) bool {
+	_, ok := m.Index(k)
+	return ok
+}

+ 39 - 0
pkg/persistent/list/list.go

@@ -0,0 +1,39 @@
+// Package list implements persistent list.
+package list
+
+// List is a persistent list.
+type List interface {
+	// Len returns the number of values in the list.
+	Len() int
+	// Cons returns a new list with an additional value in the front.
+	Cons(interface{}) List
+	// First returns the first value in the list.
+	First() interface{}
+	// Rest returns the list after the first value.
+	Rest() List
+}
+
+// Empty is an empty list.
+var Empty List = &list{}
+
+type list struct {
+	first interface{}
+	rest  *list
+	count int
+}
+
+func (l *list) Len() int {
+	return l.count
+}
+
+func (l *list) Cons(val interface{}) List {
+	return &list{val, l, l.count + 1}
+}
+
+func (l *list) First() interface{} {
+	return l.first
+}
+
+func (l *list) Rest() List {
+	return l.rest
+}

+ 3 - 0
pkg/persistent/persistent.go

@@ -0,0 +1,3 @@
+// Package persistent contains subpackages for persistent data structures,
+// similar to those of Clojure.
+package persistent

+ 433 - 0
pkg/persistent/vector/vector.go

@@ -0,0 +1,433 @@
+// Package vector implements persistent vector.
+//
+// This is a Go clone of Clojure's PersistentVector type
+// (https://github.com/clojure/clojure/blob/master/src/jvm/clojure/lang/PersistentVector.java).
+// For an introduction to the internals, see
+// https://hypirion.com/musings/understanding-persistent-vector-pt-1.
+package vector
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+)
+
+const (
+	chunkBits  = 5
+	nodeSize   = 1 << chunkBits
+	tailMaxLen = nodeSize
+	chunkMask  = nodeSize - 1
+)
+
+// Vector is a persistent sequential container for arbitrary values. It supports
+// O(1) lookup by index, modification by index, and insertion and removal
+// operations at the end. Being a persistent variant of the data structure, it
+// is immutable, and provides O(1) operations to create modified versions of the
+// vector that shares the underlying data structure, making it suitable for
+// concurrent access. The empty value is a valid empty vector.
+type Vector interface {
+	json.Marshaler
+	// Len returns the length of the vector.
+	Len() int
+	// Index returns the i-th element of the vector, if it exists. The second
+	// return value indicates whether the element exists.
+	Index(i int) (interface{}, bool)
+	// Assoc returns an almost identical Vector, with the i-th element
+	// replaced. If the index is smaller than 0 or greater than the length of
+	// the vector, it returns nil. If the index is equal to the size of the
+	// vector, it is equivalent to Cons.
+	Assoc(i int, val interface{}) Vector
+	// Cons returns an almost identical Vector, with an additional element
+	// appended to the end.
+	Cons(val interface{}) Vector
+	// Pop returns an almost identical Vector, with the last element removed. It
+	// returns nil if the vector is already empty.
+	Pop() Vector
+	// SubVector returns a subvector containing the elements from i up to but
+	// not including j.
+	SubVector(i, j int) Vector
+	// Iterator returns an iterator over the vector.
+	Iterator() Iterator
+}
+
+// Iterator is an iterator over vector elements. It can be used like this:
+//
+//     for it := v.Iterator(); it.HasElem(); it.Next() {
+//         elem := it.Elem()
+//         // do something with elem...
+//     }
+type Iterator interface {
+	// Elem returns the element at the current position.
+	Elem() interface{}
+	// HasElem returns whether the iterator is pointing to an element.
+	HasElem() bool
+	// Next moves the iterator to the next position.
+	Next()
+}
+
+type vector struct {
+	count int
+	// height of the tree structure, defined to be 0 when root is a leaf.
+	height uint
+	root   node
+	tail   []interface{}
+}
+
+// Empty is an empty Vector.
+var Empty Vector = &vector{}
+
+// node is a node in the vector tree. It is always of the size nodeSize.
+type node *[nodeSize]interface{}
+
+func newNode() node {
+	return node(&[nodeSize]interface{}{})
+}
+
+func clone(n node) node {
+	a := *n
+	return node(&a)
+}
+
+func nodeFromSlice(s []interface{}) node {
+	var n [nodeSize]interface{}
+	copy(n[:], s)
+	return &n
+}
+
+// Count returns the number of elements in a Vector.
+func (v *vector) Len() int {
+	return v.count
+}
+
+// treeSize returns the number of elements stored in the tree (as opposed to the
+// tail).
+func (v *vector) treeSize() int {
+	if v.count < tailMaxLen {
+		return 0
+	}
+	return ((v.count - 1) >> chunkBits) << chunkBits
+}
+
+func (v *vector) Index(i int) (interface{}, bool) {
+	if i < 0 || i >= v.count {
+		return nil, false
+	}
+
+	// The following is very similar to sliceFor, but is implemented separately
+	// to avoid unncessary copying.
+	if i >= v.treeSize() {
+		return v.tail[i&chunkMask], true
+	}
+	n := v.root
+	for shift := v.height * chunkBits; shift > 0; shift -= chunkBits {
+		n = n[(i>>shift)&chunkMask].(node)
+	}
+	return n[i&chunkMask], true
+}
+
+// sliceFor returns the slice where the i-th element is stored. The index must
+// be in bound.
+func (v *vector) sliceFor(i int) []interface{} {
+	if i >= v.treeSize() {
+		return v.tail
+	}
+	n := v.root
+	for shift := v.height * chunkBits; shift > 0; shift -= chunkBits {
+		n = n[(i>>shift)&chunkMask].(node)
+	}
+	return n[:]
+}
+
+func (v *vector) Assoc(i int, val interface{}) Vector {
+	if i < 0 || i > v.count {
+		return nil
+	} else if i == v.count {
+		return v.Cons(val)
+	}
+	if i >= v.treeSize() {
+		newTail := append([]interface{}(nil), v.tail...)
+		newTail[i&chunkMask] = val
+		return &vector{v.count, v.height, v.root, newTail}
+	}
+	return &vector{v.count, v.height, doAssoc(v.height, v.root, i, val), v.tail}
+}
+
+// doAssoc returns an almost identical tree, with the i-th element replaced by
+// val.
+func doAssoc(height uint, n node, i int, val interface{}) node {
+	m := clone(n)
+	if height == 0 {
+		m[i&chunkMask] = val
+	} else {
+		sub := (i >> (height * chunkBits)) & chunkMask
+		m[sub] = doAssoc(height-1, m[sub].(node), i, val)
+	}
+	return m
+}
+
+func (v *vector) Cons(val interface{}) Vector {
+	// Room in tail?
+	if v.count-v.treeSize() < tailMaxLen {
+		newTail := make([]interface{}, len(v.tail)+1)
+		copy(newTail, v.tail)
+		newTail[len(v.tail)] = val
+		return &vector{v.count + 1, v.height, v.root, newTail}
+	}
+	// Full tail; push into tree.
+	tailNode := nodeFromSlice(v.tail)
+	newHeight := v.height
+	var newRoot node
+	// Overflow root?
+	if (v.count >> chunkBits) > (1 << (v.height * chunkBits)) {
+		newRoot = newNode()
+		newRoot[0] = v.root
+		newRoot[1] = newPath(v.height, tailNode)
+		newHeight++
+	} else {
+		newRoot = v.pushTail(v.height, v.root, tailNode)
+	}
+	return &vector{v.count + 1, newHeight, newRoot, []interface{}{val}}
+}
+
+// pushTail returns a tree with tail appended.
+func (v *vector) pushTail(height uint, n node, tail node) node {
+	if height == 0 {
+		return tail
+	}
+	idx := ((v.count - 1) >> (height * chunkBits)) & chunkMask
+	m := clone(n)
+	child := n[idx]
+	if child == nil {
+		m[idx] = newPath(height-1, tail)
+	} else {
+		m[idx] = v.pushTail(height-1, child.(node), tail)
+	}
+	return m
+}
+
+// newPath creates a left-branching tree of specified height and leaf.
+func newPath(height uint, leaf node) node {
+	if height == 0 {
+		return leaf
+	}
+	ret := newNode()
+	ret[0] = newPath(height-1, leaf)
+	return ret
+}
+
+func (v *vector) Pop() Vector {
+	switch v.count {
+	case 0:
+		return nil
+	case 1:
+		return Empty
+	}
+	if v.count-v.treeSize() > 1 {
+		newTail := make([]interface{}, len(v.tail)-1)
+		copy(newTail, v.tail)
+		return &vector{v.count - 1, v.height, v.root, newTail}
+	}
+	newTail := v.sliceFor(v.count - 2)
+	newRoot := v.popTail(v.height, v.root)
+	newHeight := v.height
+	if v.height > 0 && newRoot[1] == nil {
+		newRoot = newRoot[0].(node)
+		newHeight--
+	}
+	return &vector{v.count - 1, newHeight, newRoot, newTail}
+}
+
+// popTail returns a new tree with the last leaf removed.
+func (v *vector) popTail(level uint, n node) node {
+	idx := ((v.count - 2) >> (level * chunkBits)) & chunkMask
+	if level > 1 {
+		newChild := v.popTail(level-1, n[idx].(node))
+		if newChild == nil && idx == 0 {
+			return nil
+		}
+		m := clone(n)
+		if newChild == nil {
+			// This is needed since `m[idx] = newChild` would store an
+			// interface{} with a non-nil type part, which is non-nil
+			m[idx] = nil
+		} else {
+			m[idx] = newChild
+		}
+		return m
+	} else if idx == 0 {
+		return nil
+	} else {
+		m := clone(n)
+		m[idx] = nil
+		return m
+	}
+}
+
+func (v *vector) SubVector(begin, end int) Vector {
+	if begin < 0 || begin > end || end > v.count {
+		return nil
+	}
+	return &subVector{v, begin, end}
+}
+
+func (v *vector) Iterator() Iterator {
+	return newIterator(v)
+}
+
+func (v *vector) MarshalJSON() ([]byte, error) {
+	return marshalJSON(v.Iterator())
+}
+
+type subVector struct {
+	v     *vector
+	begin int
+	end   int
+}
+
+func (s *subVector) Len() int {
+	return s.end - s.begin
+}
+
+func (s *subVector) Index(i int) (interface{}, bool) {
+	if i < 0 || s.begin+i >= s.end {
+		return nil, false
+	}
+	return s.v.Index(s.begin + i)
+}
+
+func (s *subVector) Assoc(i int, val interface{}) Vector {
+	if i < 0 || s.begin+i > s.end {
+		return nil
+	} else if s.begin+i == s.end {
+		return s.Cons(val)
+	}
+	return s.v.Assoc(s.begin+i, val).SubVector(s.begin, s.end)
+}
+
+func (s *subVector) Cons(val interface{}) Vector {
+	return s.v.Assoc(s.end, val).SubVector(s.begin, s.end+1)
+}
+
+func (s *subVector) Pop() Vector {
+	switch s.Len() {
+	case 0:
+		return nil
+	case 1:
+		return Empty
+	default:
+		return s.v.SubVector(s.begin, s.end-1)
+	}
+}
+
+func (s *subVector) SubVector(i, j int) Vector {
+	return s.v.SubVector(s.begin+i, s.begin+j)
+}
+
+func (s *subVector) Iterator() Iterator {
+	return newIteratorWithRange(s.v, s.begin, s.end)
+}
+
+func (s *subVector) MarshalJSON() ([]byte, error) {
+	return marshalJSON(s.Iterator())
+}
+
+type iterator struct {
+	v        *vector
+	treeSize int
+	index    int
+	end      int
+	path     []pathEntry
+}
+
+type pathEntry struct {
+	node  node
+	index int
+}
+
+func (e pathEntry) current() interface{} {
+	return e.node[e.index]
+}
+
+func newIterator(v *vector) *iterator {
+	return newIteratorWithRange(v, 0, v.Len())
+}
+
+func newIteratorWithRange(v *vector, begin, end int) *iterator {
+	it := &iterator{v, v.treeSize(), begin, end, nil}
+	if it.index >= it.treeSize {
+		return it
+	}
+	// Find the node for begin, remembering all nodes along the path.
+	n := v.root
+	for shift := v.height * chunkBits; shift > 0; shift -= chunkBits {
+		idx := (begin >> shift) & chunkMask
+		it.path = append(it.path, pathEntry{n, idx})
+		n = n[idx].(node)
+	}
+	it.path = append(it.path, pathEntry{n, begin & chunkMask})
+	return it
+}
+
+func (it *iterator) Elem() interface{} {
+	if it.index >= it.treeSize {
+		return it.v.tail[it.index-it.treeSize]
+	}
+	return it.path[len(it.path)-1].current()
+}
+
+func (it *iterator) HasElem() bool {
+	return it.index < it.end
+}
+
+func (it *iterator) Next() {
+	if it.index+1 >= it.treeSize {
+		// Next element is in tail. Just increment the index.
+		it.index++
+		return
+	}
+	// Find the deepest level that can be advanced.
+	var i int
+	for i = len(it.path) - 1; i >= 0; i-- {
+		e := it.path[i]
+		if e.index+1 < len(e.node) {
+			break
+		}
+	}
+	if i == -1 {
+		panic("cannot advance; vector iterator bug")
+	}
+	// Advance on this node, and re-populate all deeper levels.
+	it.path[i].index++
+	for i++; i < len(it.path); i++ {
+		it.path[i] = pathEntry{it.path[i-1].current().(node), 0}
+	}
+	it.index++
+}
+
+type marshalError struct {
+	index int
+	cause error
+}
+
+func (err *marshalError) Error() string {
+	return fmt.Sprintf("element %d: %s", err.index, err.cause)
+}
+
+func marshalJSON(it Iterator) ([]byte, error) {
+	var buf bytes.Buffer
+	buf.WriteByte('[')
+	index := 0
+	for ; it.HasElem(); it.Next() {
+		if index > 0 {
+			buf.WriteByte(',')
+		}
+		elemBytes, err := json.Marshal(it.Elem())
+		if err != nil {
+			return nil, &marshalError{index, err}
+		}
+		buf.Write(elemBytes)
+		index++
+	}
+	buf.WriteByte(']')
+	return buf.Bytes(), nil
+}

+ 439 - 0
pkg/persistent/vector/vector_test.go

@@ -0,0 +1,439 @@
+package vector
+
+import (
+	"errors"
+	"math/rand"
+	"strconv"
+	"testing"
+	"time"
+)
+
+// Nx is the minimum number of elements for the internal tree of the vector to
+// be x levels deep.
+const (
+	N1 = tailMaxLen + 1                              // 33
+	N2 = nodeSize + tailMaxLen + 1                   // 65
+	N3 = nodeSize*nodeSize + tailMaxLen + 1          // 1057
+	N4 = nodeSize*nodeSize*nodeSize + tailMaxLen + 1 // 32801
+)
+
+func init() {
+	rand.Seed(time.Now().UTC().UnixNano())
+}
+
+func TestVector(t *testing.T) {
+	run := func(n int) {
+		t.Run(strconv.Itoa(n), func(t *testing.T) {
+			v := testCons(t, n)
+			testIndex(t, v, 0, n)
+			testAssoc(t, v, "233")
+			testIterator(t, v.Iterator(), 0, n)
+			testPop(t, v)
+		})
+	}
+
+	for i := 0; i <= N3; i++ {
+		run(i)
+	}
+	run(N4)
+}
+
+// Regression test against #4.
+func TestIterator_VectorWithNil(t *testing.T) {
+	run := func(n int) {
+		t.Run(strconv.Itoa(n), func(t *testing.T) {
+			v := Empty
+			for i := 0; i < n; i++ {
+				v = v.Cons(nil)
+			}
+
+			iterated := 0
+			for it := v.Iterator(); it.HasElem(); it.Next() {
+				iterated++
+				if it.Elem() != nil {
+					t.Errorf("element not nil")
+				}
+			}
+			if iterated != n {
+				t.Errorf("did not iterate %d items", n)
+			}
+		})
+	}
+	for i := 0; i <= N3; i++ {
+		run(i)
+	}
+	run(N4)
+}
+
+// testCons creates a vector containing 0...n-1 with Cons, and ensures that the
+// length of the old and new vectors are expected after each Cons. It returns
+// the created vector.
+func testCons(t *testing.T, n int) Vector {
+	v := Empty
+	for i := 0; i < n; i++ {
+		oldv := v
+		v = v.Cons(i)
+
+		if count := oldv.Len(); count != i {
+			t.Errorf("oldv.Count() == %v, want %v", count, i)
+		}
+		if count := v.Len(); count != i+1 {
+			t.Errorf("v.Count() == %v, want %v", count, i+1)
+		}
+	}
+	return v
+}
+
+// testIndex tests Index, assuming that the vector contains begin...int-1.
+func testIndex(t *testing.T, v Vector, begin, end int) {
+	n := v.Len()
+	for i := 0; i < n; i++ {
+		elem, _ := v.Index(i)
+		if elem != i {
+			t.Errorf("v.Index(%v) == %v, want %v", i, elem, i)
+		}
+	}
+	for _, i := range []int{-2, -1, n, n + 1, n * 2} {
+		if elem, _ := v.Index(i); elem != nil {
+			t.Errorf("v.Index(%d) == %v, want nil", i, elem)
+		}
+	}
+}
+
+// testIterator tests the iterator, assuming that the result is begin...end-1.
+func testIterator(t *testing.T, it Iterator, begin, end int) {
+	i := begin
+	for ; it.HasElem(); it.Next() {
+		elem := it.Elem()
+		if elem != i {
+			t.Errorf("iterator produce %v, want %v", elem, i)
+		}
+		i++
+	}
+	if i != end {
+		t.Errorf("iterator produces up to %v, want %v", i, end)
+	}
+}
+
+// testAssoc tests Assoc by replacing each element.
+func testAssoc(t *testing.T, v Vector, subst interface{}) {
+	n := v.Len()
+	for i := 0; i <= n; i++ {
+		oldv := v
+		v = v.Assoc(i, subst)
+
+		if i < n {
+			elem, _ := oldv.Index(i)
+			if elem != i {
+				t.Errorf("oldv.Index(%v) == %v, want %v", i, elem, i)
+			}
+		}
+
+		elem, _ := v.Index(i)
+		if elem != subst {
+			t.Errorf("v.Index(%v) == %v, want %v", i, elem, subst)
+		}
+	}
+
+	n++
+	for _, i := range []int{-1, n + 1, n + 2, n * 2} {
+		newv := v.Assoc(i, subst)
+		if newv != nil {
+			t.Errorf("v.Assoc(%d) = %v, want nil", i, newv)
+		}
+	}
+}
+
+// testPop tests Pop by removing each element.
+func testPop(t *testing.T, v Vector) {
+	n := v.Len()
+	for i := 0; i < n; i++ {
+		oldv := v
+		v = v.Pop()
+
+		if count := oldv.Len(); count != n-i {
+			t.Errorf("oldv.Count() == %v, want %v", count, n-i)
+		}
+		if count := v.Len(); count != n-i-1 {
+			t.Errorf("oldv.Count() == %v, want %v", count, n-i-1)
+		}
+	}
+	newv := v.Pop()
+	if newv != nil {
+		t.Errorf("v.Pop() = %v, want nil", newv)
+	}
+}
+
+func TestSubVector(t *testing.T) {
+	v := Empty
+	for i := 0; i < 10; i++ {
+		v = v.Cons(i)
+	}
+
+	sv := v.SubVector(0, 4)
+	testIndex(t, sv, 0, 4)
+	testAssoc(t, sv, "233")
+	testIterator(t, sv.Iterator(), 0, 4)
+	testPop(t, sv)
+
+	sv = v.SubVector(1, 4)
+	if !checkVector(sv, 1, 2, 3) {
+		t.Errorf("v[0:4] is not expected")
+	}
+	if !checkVector(sv.Assoc(1, "233"), 1, "233", 3) {
+		t.Errorf("v[0:4].Assoc is not expected")
+	}
+	if !checkVector(sv.Cons("233"), 1, 2, 3, "233") {
+		t.Errorf("v[0:4].Cons is not expected")
+	}
+	if !checkVector(sv.Pop(), 1, 2) {
+		t.Errorf("v[0:4].Pop is not expected")
+	}
+	if !checkVector(sv.SubVector(1, 2), 2) {
+		t.Errorf("v[0:4][1:2] is not expected")
+	}
+	testIterator(t, sv.Iterator(), 1, 4)
+
+	if !checkVector(v.SubVector(1, 1)) {
+		t.Errorf("v[1:1] is not expected")
+	}
+	// Begin is allowed to be equal to n if end is also n
+	if !checkVector(v.SubVector(10, 10)) {
+		t.Errorf("v[10:10] is not expected")
+	}
+
+	bad := v.SubVector(-1, 0)
+	if bad != nil {
+		t.Errorf("v.SubVector(-1, 0) = %v, want nil", bad)
+	}
+	bad = v.SubVector(5, 100)
+	if bad != nil {
+		t.Errorf("v.SubVector(5, 100) = %v, want nil", bad)
+	}
+	bad = v.SubVector(-1, 100)
+	if bad != nil {
+		t.Errorf("v.SubVector(-1, 100) = %v, want nil", bad)
+	}
+	bad = v.SubVector(4, 2)
+	if bad != nil {
+		t.Errorf("v.SubVector(4, 2) = %v, want nil", bad)
+	}
+}
+
+// Regression test for https://b.elv.sh/1287: crash when tree has a height >= 1
+// and start of subvector is in the tail.
+func TestSubVector_BeginFromTail(t *testing.T) {
+	v := Empty
+	for i := 0; i < 65; i++ {
+		v = v.Cons(i)
+	}
+	sv := v.SubVector(64, 65)
+	testIterator(t, sv.Iterator(), 64, 65)
+}
+
+func checkVector(v Vector, values ...interface{}) bool {
+	if v.Len() != len(values) {
+		return false
+	}
+	for i, a := range values {
+		if x, _ := v.Index(i); x != a {
+			return false
+		}
+	}
+	return true
+}
+
+func TestVectorEqual(t *testing.T) {
+	v1, v2 := Empty, Empty
+	for i := 0; i < N3; i++ {
+		elem := rand.Int63()
+		v1 = v1.Cons(elem)
+		v2 = v2.Cons(elem)
+		if !eqVector(v1, v2) {
+			t.Errorf("Not equal after Cons'ing %d elements", i+1)
+		}
+	}
+}
+
+func eqVector(v1, v2 Vector) bool {
+	if v1.Len() != v2.Len() {
+		return false
+	}
+	for i := 0; i < v1.Len(); i++ {
+		a1, _ := v1.Index(i)
+		a2, _ := v2.Index(i)
+		if a1 != a2 {
+			return false
+		}
+	}
+	return true
+}
+
+var marshalJSONTests = []struct {
+	in      Vector
+	wantOut string
+	wantErr error
+}{
+	{makeVector("1", 2, nil), `["1",2,null]`, nil},
+	{makeVector("1", makeVector(2)), `["1",[2]]`, nil},
+	{makeVector(0, 1, 2, 3, 4, 5).SubVector(1, 5), `[1,2,3,4]`, nil},
+	{makeVector(0, func() {}), "", errors.New("element 1: json: unsupported type: func()")},
+}
+
+func TestMarshalJSON(t *testing.T) {
+	for i, test := range marshalJSONTests {
+		out, err := test.in.MarshalJSON()
+		if string(out) != test.wantOut {
+			t.Errorf("v%d.MarshalJSON -> out %q, want %q", i, out, test.wantOut)
+		}
+		if err == nil || test.wantErr == nil {
+			if err != test.wantErr {
+				t.Errorf("v%d.MarshalJSON -> err %v, want %v", i, err, test.wantErr)
+			}
+		} else {
+			if err.Error() != test.wantErr.Error() {
+				t.Errorf("v%d.MarshalJSON -> err %v, want %v", i, err, test.wantErr)
+			}
+		}
+	}
+}
+
+func makeVector(elements ...interface{}) Vector {
+	v := Empty
+	for _, element := range elements {
+		v = v.Cons(element)
+	}
+	return v
+}
+
+func BenchmarkConsNativeN1(b *testing.B) { benchmarkNativeAppend(b, N1) }
+func BenchmarkConsNativeN2(b *testing.B) { benchmarkNativeAppend(b, N2) }
+func BenchmarkConsNativeN3(b *testing.B) { benchmarkNativeAppend(b, N3) }
+func BenchmarkConsNativeN4(b *testing.B) { benchmarkNativeAppend(b, N4) }
+
+func benchmarkNativeAppend(b *testing.B, n int) {
+	for r := 0; r < b.N; r++ {
+		var s []interface{}
+		for i := 0; i < n; i++ {
+			s = append(s, i)
+		}
+	}
+}
+
+func BenchmarkConsPersistentN1(b *testing.B) { benchmarkCons(b, N1) }
+func BenchmarkConsPersistentN2(b *testing.B) { benchmarkCons(b, N2) }
+func BenchmarkConsPersistentN3(b *testing.B) { benchmarkCons(b, N3) }
+func BenchmarkConsPersistentN4(b *testing.B) { benchmarkCons(b, N4) }
+
+func benchmarkCons(b *testing.B, n int) {
+	for r := 0; r < b.N; r++ {
+		v := Empty
+		for i := 0; i < n; i++ {
+			v = v.Cons(i)
+		}
+	}
+}
+
+var (
+	sliceN4  = make([]interface{}, N4)
+	vectorN4 = Empty
+)
+
+func init() {
+	for i := 0; i < N4; i++ {
+		vectorN4 = vectorN4.Cons(i)
+	}
+}
+
+var x interface{}
+
+func BenchmarkIndexSeqNativeN4(b *testing.B) { benchmarkIndexSeqNative(b, N4) }
+
+func benchmarkIndexSeqNative(b *testing.B, n int) {
+	for r := 0; r < b.N; r++ {
+		for i := 0; i < n; i++ {
+			x = sliceN4[i]
+		}
+	}
+}
+
+func BenchmarkIndexSeqPersistentN4(b *testing.B) { benchmarkIndexSeqPersistent(b, N4) }
+
+func benchmarkIndexSeqPersistent(b *testing.B, n int) {
+	for r := 0; r < b.N; r++ {
+		for i := 0; i < n; i++ {
+			x, _ = vectorN4.Index(i)
+		}
+	}
+}
+
+var randIndicies []int
+
+func init() {
+	randIndicies = make([]int, N4)
+	for i := 0; i < N4; i++ {
+		randIndicies[i] = rand.Intn(N4)
+	}
+}
+
+func BenchmarkIndexRandNative(b *testing.B) {
+	for r := 0; r < b.N; r++ {
+		for _, i := range randIndicies {
+			x = sliceN4[i]
+		}
+	}
+}
+
+func BenchmarkIndexRandPersistent(b *testing.B) {
+	for r := 0; r < b.N; r++ {
+		for _, i := range randIndicies {
+			x, _ = vectorN4.Index(i)
+		}
+	}
+}
+
+func nativeEqual(s1, s2 []int) bool {
+	if len(s1) != len(s2) {
+		return false
+	}
+	for i, v1 := range s1 {
+		if v1 != s2[i] {
+			return false
+		}
+	}
+	return true
+}
+
+func BenchmarkEqualNative(b *testing.B) {
+	b.StopTimer()
+	var s1, s2 []int
+	for i := 0; i < N4; i++ {
+		s1 = append(s1, i)
+		s2 = append(s2, i)
+	}
+	b.StartTimer()
+
+	for r := 0; r < b.N; r++ {
+		eq := nativeEqual(s1, s2)
+		if !eq {
+			panic("not equal")
+		}
+	}
+}
+
+func BenchmarkEqualPersistent(b *testing.B) {
+	b.StopTimer()
+	v1, v2 := Empty, Empty
+	for i := 0; i < N4; i++ {
+		v1 = v1.Cons(i)
+		v2 = v2.Cons(i)
+	}
+	b.StartTimer()
+
+	for r := 0; r < b.N; r++ {
+		eq := eqVector(v1, v2)
+		if !eq {
+			panic("not equal")
+		}
+	}
+}