Git Product home page Git Product logo

go-immutable's Introduction

Immutable data structures for Go

GitHub tag (latest SemVer) PkgGoDev Go Report Card

  • Based on a immutable persistent Hash Array Mapped Trie (HAMT)
  • Minimal interface to the core HAMT implementation so that you can easily implement your own structures on top of it.
  • Comes with some set and map implementations ready to go
  • Inspired by Clojure's data structures
  • Excellent performance (lookup is about the same as native go maps, insertion about 20% that of mutable native go maps.)

Example:

package main

import (
  "fmt"
  "github.com/rsms/go-immutable"
)

m := immutable.EmptyStrMap

m1 := m.Set("Hello", 123)
m2 := m.Set("Hello", 456).Set("Sun", 9)
m3 := m2.Del("Hello")

fmt.Printf("m1: %s\n", m1)
fmt.Printf("m2: %s\n", m2)
fmt.Printf("m3: %s\n", m3)

// Output:
// m1: {"Hello": 123}
// m2: {"Sun": 9, "Hello": 456}
// m3: {"Sun": 9}

Benchmark

TEST                            SAMPLES       TIME PER ITERATION
BenchmarkHamtLookup_10          48042776          23.8 ns/op
BenchmarkHamtLookup_100         36947116          31.0 ns/op
BenchmarkHamtLookup_1000        32566189          34.8 ns/op
BenchmarkHamtLookup_10000       24775086          49.1 ns/op

BenchmarkGoMapLookup_10         69836985          16.3 ns/op
BenchmarkGoMapLookup_100        66960720          16.5 ns/op
BenchmarkGoMapLookup_1000       36865258          30.7 ns/op
BenchmarkGoMapLookup_10000      28110822          43.3 ns/op

BenchmarkHamtInsert_10           8019391         137.0 ns/op
BenchmarkHamtInsert_25           6851881         174.0 ns/op
BenchmarkHamtInsert_50           5419015         221.0 ns/op
BenchmarkHamtInsert_100          3860596         293.0 ns/op
BenchmarkHamtInsert_1000         2596484         469.0 ns/op
BenchmarkHamtInsert_5000         1892996         644.0 ns/op

BenchmarkGoMapInsert_10         17940093          66.5 ns/op
BenchmarkGoMapInsert_25         13996856          74.3 ns/op
BenchmarkGoMapInsert_50         14645752          83.3 ns/op
BenchmarkGoMapInsert_100        14309284          82.8 ns/op
BenchmarkGoMapInsert_1000       10754890         106.0 ns/op
BenchmarkGoMapInsert_5000       11948517         100.0 ns/op

Results are from a 2018 MacBook Pro. BenchmarkHamt* and BenchmarkGo* are tests with same input using HAMT and native Go maps, respectively. Run these benchmarks yourself with ./dev -bench.

Keep in mind that HAMT is immutable and derivative data structure which requires lots of memory allocations, compared to the mutable in-place native go maps. I've chosen to compare the HAMT implementation with Go maps since Go maps are likely what you are familiar with. :-)

go-immutable's People

Contributors

rsms avatar

Stargazers

 avatar  avatar Steven Simba avatar Gabriel S. Facina avatar Benjamin Saint-Cyr avatar Cora Sutton avatar buzai avatar Haider Alshamma avatar Silas Sewell avatar Mr Morsalin avatar Johannes Brüderl avatar  avatar Leonardo Nascimento avatar MB avatar Hercules Merscher avatar Robb Böhnke avatar Jhonn W. Frazão avatar João Maia avatar Piotr Kubisa avatar Alejandro Falkowski avatar Márk Bartos avatar Pierrick Martin avatar ahmetlutfu avatar Erhan Yakut avatar  avatar Víctor Ramírez avatar Matthew Lee avatar Pham Hong Phuc (frank) avatar D. Kasi Pavan Kumar avatar fall simply avatar Toan Tran avatar Zaydek MG avatar Indragie Karunaratne avatar Patrick Smith avatar

Watchers

 avatar James Cloos avatar René Post avatar  avatar

Forkers

networkteam

go-immutable's Issues

Differing results

Hi,

i see slightly different results when i compare the Hamt with a std/map :

diff factor  8 | build-up of im.Map took 21325 | std/map 2608
diff factor >3 | oracle lookup took 1324 | std/map 422
diff factor 17 | oracle remove took 9237 | std/map 512
equal          | size-of-hamt is 4500000 | std/map 4500000

The test fills the hamt/map with 6-M items and a oracle with a subset of 1.5M random-unique items from the set of 6-M items. It then tests the membership of the subsets items against the hamt/map and finally removes all subset-items from the Hamt/map. Thus ending with 6M - 1.5M = 4.5M-items in either Hamt or std/map.
I'm pretty new to Go - maybe i'm doing smth. wrong here ?
My test-code won't run in the Playground.

greets Andreas

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.