-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(skiplist): rebuild from source code
- Loading branch information
0 parents
commit ff85f93
Showing
13 changed files
with
986 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: Go | ||
|
||
on: | ||
push: | ||
branches: [master, feat/*] | ||
pull_request: | ||
branches: [master, feat/*] | ||
|
||
defaults: | ||
run: | ||
working-directory: . | ||
|
||
jobs: | ||
checkout: | ||
name: Go static analysis And Unit test | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check out code into the Go module directory | ||
uses: actions/checkout@v2 | ||
- name: Set up Go 1.x | ||
uses: actions/setup-go@v2 | ||
with: | ||
go-version: ^1.13 | ||
- name: golangci-lint installation | ||
uses: golangci/golangci-lint-action@v2 | ||
|
||
- name: Get dependencies | ||
run: go mod tidy | ||
|
||
- name: Unit Test | ||
run: go test . -v -coverprofile=covprofile.cov | ||
|
||
- name: Install goveralls | ||
env: | ||
GO111MODULE: off | ||
run: go get github.com/mattn/goveralls | ||
|
||
- name: Send coverage | ||
env: | ||
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
run: goveralls -coverprofile=covprofile.cov -service=github |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.out | ||
.idea | ||
.vscode | ||
.test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2021 ruth | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
![](https://github.com/actions/starter-workflows/blob/main/icons/go.svg) | ||
# SkipList Implement in go | ||
|
||
[![Go Report Card](https://goreportcard.com/badge/github.com/Ruth-Seven/skiplist)](https://goreportcard.com/report/github.com/Ruth-Seven/skiplist) [![Go](https://github.com/Ruth-Seven/skiplist/actions/workflows/go.yaml/badge.svg)](https://github.com/Ruth-Seven/skiplist/actions/workflows/go.yaml) [![Coverage Status](https://coveralls.io/repos/github/Ruth-Seven/skiplist/badge.svg)](https://coveralls.io/github/Ruth-Seven/skiplist) | ||
|
||
A Basically implement of skiplist, which doesn't consider courrency in go. | ||
skip lists: A probabilistaic alternative to balanced treees阐述了实现伪代码和设计上的工作 | ||
|
||
skip lists 的优点: | ||
|
||
- 结构简单,每个节点占用的非信息空间少 | ||
- 查询速度和非递归自动调整树更快 | ||
- 每个节点的指针组创建之后就不必改动大小,只在插入或者删除后修改指向 | ||
- 有概率 P(Level=K)=1/(2^k)的概率,每个该节点的期望指针数是 2,修改和创建期望数也是如此 | ||
|
||
> 设立首次搜索 level= L(n)= log_2(n),获取避免从当前最大 level 进行搜索,优化搜索次数。之后发现该条对性能没有明显提升,而且容易破坏更新值内容,故舍弃。 | ||
## drawback | ||
|
||
limited by the lack of go generics, I have to use float64 to compare SkipItem. It's terrible, since you never know when you code will collision even if there is two rather different objet. | ||
|
||
how to solve it? The answer is generics. | ||
|
||
## test | ||
|
||
```shell | ||
go clean -cache && go test -v | ||
go clean -cache && go test -bench=. -v # please don't use --race without runned parallel | ||
go clean -cache && go test -bench=. -v -cpuprofile=cpu.out -benchtime=100000000x | ||
go tool pprof -http localhost:8000 ./cpu.out | ||
go test -run=TestMonkey | ||
``` | ||
|
||
Results of benchmark: | ||
|
||
```shell | ||
#v0.0.2 | ||
// go clean -cache && go test -run=NOTEST -bench=. -v -benchtime=1000000x -count=20 > x.out | ||
// benchstat x.out | ||
goos: darwin | ||
goarch: amd64 | ||
pkg: github.com/MiniSkipList/skiplist | ||
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz | ||
|
||
// -benchtime=1000000x -count=20 | ||
SkipListInsert-12 141ns ± 3% | ||
SkipListFind-12 143ns ± 6% | ||
SkipListFindBiggerOrEqual-12 147ns ±13% | ||
SkipListDelete-12 32.1ns ±20% | ||
BucketmapInsert-12 71.9ns ±14% | ||
BucketmapFind-12 66.5ns ±12% | ||
BucketmapDelete-12 2.37ns ±18% | ||
|
||
// -benchtime=10000000x -count=20 | ||
name time/op | ||
SkipListInsert-12 177ns ± 4% | ||
SkipListFind-12 174ns ± 7% | ||
SkipListFindBiggerOrEqual-12 173ns ± 4% | ||
SkipListDelete-12 26.3ns ± 3% | ||
BucketmapInsert-12 88.0ns ± 2% | ||
BucketmapFind-12 81.3ns ± 5% | ||
BucketmapDelete-12 2.40ns ±36% | ||
|
||
// -benchtime=100000000x -count=5 | ||
name time/op | ||
SkipListInsert-12 227ns ±50% | ||
SkipListFind-12 192ns ±10% | ||
SkipListFindBiggerOrEqual-12 186ns ± 0% | ||
SkipListDelete-12 25.5ns ± 1% | ||
BucketmapInsert-12 122ns ± 2% | ||
BucketmapFind-12 111ns ± 0% | ||
BucketmapDelete-12 1.77ns ± 2% | ||
``` | ||
|
||
operations / comparation times: | ||
|
||
```shell | ||
BenchmarkSkipListInsert findtime/ops: 4845312580 / 100000001 :48.453125 | ||
BenchmarkSkipListFind findtime/ops: 5022697254 / 100000001 :50.226972 | ||
BenchmarkSkipListFindBiggerOrEqual findtime/ops: 5022697254 / 100000001 :50.226972 | ||
BenchmarkSkipListDelete findtime/ops: 2520589160 / 100000001 :25.205891 | ||
``` | ||
|
||
## Reference | ||
|
||
[Skip Lists: A Probabilistic Alternative to Balanced Trees](https://15721.courses.cs.cmu.edu/spring2018/papers/08-oltpindexes1/pugh-skiplists-cacm1990.pdf) | ||
|
||
[A Contention-Friendly, Non-Blocking Skip List](https://hal.inria.fr/hal-00699794v4/document) | ||
|
||
[SkipList in C++](https://github.com/HiWong/SkipListPro) | ||
|
||
[A another implement of skiplist in go](https://github.com/mauricegit/skiplist) And [open API](https://pkg.go.dev/github.com/MauriceGit/skiplist) | ||
> The repo request you take a trunck of courage to read this codeeeeee. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module github.com/skiplist | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/kr/pretty v0.1.0 // indirect | ||
github.com/stretchr/testify v1.7.0 | ||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= | ||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= | ||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= | ||
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= | ||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | ||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= | ||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package skiplist | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"testing" | ||
"time" | ||
) | ||
|
||
// type operater interface { | ||
// operate() error | ||
// } | ||
|
||
type operation func() error | ||
|
||
type MonkeyTester struct { | ||
// Monkey []operater | ||
Monkey []operation | ||
} | ||
|
||
type Item struct { | ||
key float64 | ||
value int64 | ||
} | ||
|
||
func (i Item) ExtendedKey() float64 { | ||
return i.key | ||
} | ||
|
||
type ListAndMap struct { | ||
list SkipList | ||
m map[float64]int64 | ||
} | ||
|
||
func NewListAndMap() ListAndMap { | ||
lm := ListAndMap{ | ||
list: NewSkipList(0), | ||
m: make(map[float64]int64), | ||
} | ||
return lm | ||
} | ||
|
||
func (lm *ListAndMap) genRandom() Item { | ||
value := randUint64(1000) | ||
key := float64((randUint64(10000))) // set the random distribution of key at (-10000, 10000) | ||
return Item{ | ||
key: key, | ||
value: int64(value), | ||
} | ||
} | ||
|
||
func (lm *ListAndMap) Insert() error { | ||
item := lm.genRandom() | ||
lm.list.Insert(item) | ||
lm.m[item.key] = item.value | ||
return nil | ||
} | ||
|
||
func (lm *ListAndMap) Delete() error { | ||
item := lm.genRandom() | ||
lm.list.Delete(item.ExtendedKey()) | ||
delete(lm.m, item.key) | ||
return nil | ||
} | ||
|
||
func (lm *ListAndMap) Find() error { | ||
item := lm.genRandom() | ||
t1, ok1 := lm.list.Find(item.ExtendedKey()) | ||
v, ok2 := lm.m[item.key] | ||
if ok1 != ok2 { | ||
return errors.New("not both empty") | ||
} | ||
if t1 != nil && v != t1.(Item).value { | ||
return errors.New("found value is not equal") | ||
} | ||
return nil | ||
} | ||
|
||
func (lm *ListAndMap) Equal() error { | ||
size := lm.list.Size() | ||
if size != len(lm.m) { | ||
return errors.New("the size is different") | ||
} | ||
for entity := lm.list.Minimal(); entity != nil; entity = lm.list.Next(entity) { | ||
if v, ok := lm.m[entity.key]; !ok { | ||
return errors.New("can't find item in map") | ||
} else if v != entity.Element.(Item).value { | ||
return errors.New("found value isn't identical") | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func NewMonkey() MonkeyTester { | ||
ml := NewListAndMap() | ||
monkey := MonkeyTester{ | ||
Monkey: []operation{ | ||
ml.Insert, ml.Delete, ml.Find, ml.Equal, | ||
}, | ||
} | ||
return monkey | ||
} | ||
|
||
func (m *MonkeyTester) DoOnce() error { | ||
for _, op := range m.Monkey { | ||
err := op() | ||
if err != nil { | ||
return fmt.Errorf("operate A failed. op: %v, err: %v", op, err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (m *MonkeyTester) RamdonDo() error { | ||
idx := randUint64(uint64(len(m.Monkey))) | ||
err := m.Monkey[idx]() | ||
if err != nil { | ||
return fmt.Errorf("fail to call RamdonDo . op: %T, err: %v", m.Monkey[idx], err) | ||
} | ||
return nil | ||
} | ||
|
||
func TestMain(t *testing.T) { | ||
testAmount := int64(1e6) | ||
gap := testAmount / 100 | ||
threshold := int64(0) | ||
log.Print(testAmount) | ||
monkey := NewMonkey() | ||
err := monkey.DoOnce() | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
|
||
now := time.Now() | ||
for i := int64(0); i < testAmount; i++ { | ||
if i >= threshold { | ||
consume := time.Since(now).Seconds() | ||
log.Printf("Process:(%v/%v)%3f%% \t time: %8.3f, \t unit: %.6fs/op \n", | ||
i, testAmount, float64(i)/float64(testAmount)*100, consume, consume/float64(gap)) | ||
threshold += gap | ||
} | ||
err = monkey.RamdonDo() | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
} | ||
} |
Oops, something went wrong.