From e5b3c4757db55cba7d54d6dd0ca403b61541da87 Mon Sep 17 00:00:00 2001 From: Jakub Sztandera Date: Sat, 21 Sep 2019 19:23:13 +0200 Subject: [PATCH] More iterative algorithms - Add RunIterator and decoder from RLE - Add BitIterator and BitsFromRuns - Add BitsFromSlice - Add RunsFromBits License: MIT Signed-off-by: Jakub Sztandera --- lib/rlepluslazy/bits.go | 139 +++++++++++++++++++++++++ lib/rlepluslazy/bits_test.go | 27 +++++ lib/rlepluslazy/interface.go | 24 +++++ lib/rlepluslazy/rleplus.go | 61 +++++++++++ lib/rlepluslazy/rleplus_golden_test.go | 3 + lib/rlepluslazy/rleplus_test.go | 78 +++++++++++++- 6 files changed, 330 insertions(+), 2 deletions(-) create mode 100644 lib/rlepluslazy/bits.go create mode 100644 lib/rlepluslazy/bits_test.go create mode 100644 lib/rlepluslazy/interface.go create mode 100644 lib/rlepluslazy/rleplus_golden_test.go diff --git a/lib/rlepluslazy/bits.go b/lib/rlepluslazy/bits.go new file mode 100644 index 000000000..b3b0061b8 --- /dev/null +++ b/lib/rlepluslazy/bits.go @@ -0,0 +1,139 @@ +package rlepluslazy + +import ( + "sort" +) + +type it2b struct { + source RunIterator + curIdx uint64 + + run Run +} + +func (it *it2b) HasNext() bool { + return it.run.Valid() +} + +func (it *it2b) Next() (uint64, error) { + it.run.Len-- + res := it.curIdx + it.curIdx++ + return res, it.prep() +} + +func (it *it2b) prep() error { + for !it.run.Valid() && it.source.HasNext() { + var err error + it.run, err = it.source.NextRun() + if err != nil { + return err + } + + if !it.run.Val { + it.curIdx += it.run.Len + it.run.Len = 0 + } + } + return nil +} + +func BitsFromRuns(source RunIterator) (BitIterator, error) { + it := &it2b{source: source} + if err := it.prep(); err != nil { + return nil, err + } + return it, nil +} + +type sliceIt struct { + s []uint64 +} + +func (it sliceIt) HasNext() bool { + return len(it.s) != 0 +} + +func (it *sliceIt) Next() (uint64, error) { + res := it.s[0] + it.s = it.s[1:] + return res, nil +} + +func BitsFromSlice(slice []uint64) BitIterator { + sort.Slice(slice, func(i, j int) bool { return slice[i] < slice[j] }) + return &sliceIt{slice} +} + +type it2r struct { + source BitIterator + + runIdx uint64 + run [2]Run +} + +func (it *it2r) HasNext() bool { + return it.run[0].Valid() +} + +func (it *it2r) NextRun() (Run, error) { + res := it.run[0] + it.runIdx = it.runIdx + res.Len + it.run[0], it.run[1] = it.run[1], Run{} + return res, it.prep() +} + +func (it *it2r) prep() error { + if !it.HasNext() { + return nil + } + if it.run[0].Val == false { + it.run[1].Val = true + it.run[1].Len = 1 + return nil + } + + for it.source.HasNext() && !it.run[1].Valid() { + nB, err := it.source.Next() + if err != nil { + return err + } + + //fmt.Printf("runIdx: %d, run[0].Len: %d, nB: %d\n", it.runIdx, it.run[0].Len, nB) + if it.runIdx+it.run[0].Len == nB { + it.run[0].Len++ + } else { + it.run[1].Len = nB - it.runIdx - it.run[0].Len + it.run[1].Val = false + } + } + return nil +} + +func (it *it2r) init() error { + if it.source.HasNext() { + nB, err := it.source.Next() + if err != nil { + return err + } + it.run[0].Len = nB + it.run[0].Val = false + it.run[1].Len = 1 + it.run[1].Val = true + } + + if !it.run[0].Valid() { + it.run[0], it.run[1] = it.run[1], Run{} + return it.prep() + } + return nil +} + +func RunsFromBits(source BitIterator) (RunIterator, error) { + it := &it2r{source: source} + + if err := it.init(); err != nil { + return nil, err + } + return it, nil +} diff --git a/lib/rlepluslazy/bits_test.go b/lib/rlepluslazy/bits_test.go new file mode 100644 index 000000000..96eac25fa --- /dev/null +++ b/lib/rlepluslazy/bits_test.go @@ -0,0 +1,27 @@ +package rlepluslazy + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestRunsFromBits(t *testing.T) { + expected := []Run{Run{Val: false, Len: 0x1}, + {Val: true, Len: 0x3}, + {Val: false, Len: 0x2}, + {Val: true, Len: 0x3}, + } + rit, err := RunsFromBits(BitsFromSlice([]uint64{1, 2, 3, 6, 7, 8})) + assert.NoError(t, err) + i := 10 + output := make([]Run, 0, 4) + for rit.HasNext() && i > 0 { + run, err := rit.NextRun() + assert.NoError(t, err) + i-- + output = append(output, run) + } + assert.NotEqual(t, 0, i, "too many iterations") + assert.Equal(t, expected, output) +} diff --git a/lib/rlepluslazy/interface.go b/lib/rlepluslazy/interface.go new file mode 100644 index 000000000..e9f45910f --- /dev/null +++ b/lib/rlepluslazy/interface.go @@ -0,0 +1,24 @@ +package rlepluslazy + +type Run struct { + Val bool + Len uint64 +} + +func (r Run) Valid() bool { + return r.Len != 0 +} + +type RunIterator interface { + NextRun() (Run, error) + HasNext() bool +} + +type RunIterable interface { + RunIterator() (RunIterator, error) +} + +type BitIterator interface { + Next() (uint64, error) + HasNext() bool +} diff --git a/lib/rlepluslazy/rleplus.go b/lib/rlepluslazy/rleplus.go index d24e1ed51..0e48f9651 100644 --- a/lib/rlepluslazy/rleplus.go +++ b/lib/rlepluslazy/rleplus.go @@ -37,6 +37,67 @@ func (rle *RLE) check() error { return nil } +func (rle *RLE) RunIterator() (RunIterator, error) { + vit := rle.vec.Iterator(bitvector.LSB0) + vit(2) // Take version + + it := &rleIterator{next: vit} + // next run is previous in relation to prep + // so we invert the value + it.nextRun.Val = vit(1) != 1 + if err := it.prep(); err != nil { + return nil, err + } + return it, nil +} + +type rleIterator struct { + next func(uint) byte + + nextRun Run +} + +func (it *rleIterator) HasNext() bool { + return it.nextRun.Valid() +} + +func (it *rleIterator) NextRun() (Run, error) { + ret := it.nextRun + return ret, it.prep() +} + +func (it *rleIterator) prep() error { + x := it.next(1) + + switch x { + case 1: + it.nextRun.Len = 1 + + case 0: + y := it.next(1) + switch y { + case 1: + it.nextRun.Len = uint64(it.next(4)) + case 0: + var buf = make([]byte, 0, 10) + for { + b := it.next(8) + buf = append(buf, b) + if b&0x80 == 0 { + break + } + if len(buf) > 10 { + return xerrors.Errorf("run too long: %w", ErrDecode) + } + } + it.nextRun.Len, _ = binary.Uvarint(buf) + } + } + + it.nextRun.Val = !it.nextRun.Val + return nil +} + func (rle *RLE) Iterator() (*iterator, error) { vit := rle.vec.Iterator(bitvector.LSB0) vit(2) // Take version diff --git a/lib/rlepluslazy/rleplus_golden_test.go b/lib/rlepluslazy/rleplus_golden_test.go new file mode 100644 index 000000000..ff4984fa1 --- /dev/null +++ b/lib/rlepluslazy/rleplus_golden_test.go @@ -0,0 +1,3 @@ +package rlepluslazy + +var goldenRLE = []byte{0x40, 0x31, 0x40, 0x84, 0x5c, 0xc0, 0xc6, 0x38, 0xc0, 0xc1, 0x5e, 0x20, 0x92, 0x6, 0x12, 0xbd, 0x9, 0x3a, 0xa3, 0x1, 0x7c, 0x60, 0x97, 0xec, 0x3, 0x8f, 0x4f, 0x1, 0x95, 0x19, 0x26, 0x30, 0x66, 0x80, 0x7, 0x4b, 0x1, 0x7, 0x5f, 0x8, 0xc7, 0x94, 0x28, 0x1f, 0xd0, 0xa9, 0x47, 0x88, 0x7f, 0x80, 0x82, 0x71, 0x82, 0x8, 0x64, 0x82, 0x83, 0x91, 0x46, 0x93, 0x7, 0xb5, 0x40, 0x5f, 0xe4, 0x3b, 0x20, 0x1b, 0x1a, 0x71, 0x44, 0x70, 0x6e, 0x21, 0x81, 0x2e, 0x40, 0x6, 0xfc, 0x7, 0x16, 0xa9, 0x2, 0x16, 0xc8, 0x2, 0x12, 0x34, 0x48, 0xe0, 0x3e, 0xe0, 0x43, 0x2d, 0xa0, 0x52, 0xff, 0xe0, 0xf1, 0xc, 0x1, 0x4, 0xb5, 0x2, 0x44, 0x98, 0x12, 0x7a, 0x26, 0x36, 0xa, 0xa5, 0x2, 0xa9, 0x24, 0x80, 0x2, 0x9, 0x8b, 0x57, 0x1, 0x5, 0xd8, 0x4, 0x11, 0x53, 0x2, 0x95, 0xdc, 0x3, 0x95, 0xc5, 0x0, 0x91, 0xcb, 0x1, 0x36, 0xb8, 0x71, 0xd8, 0xe, 0x50, 0x28, 0xf4, 0x70, 0x18, 0xe, 0xd0, 0x89, 0x64, 0xb0, 0xc0, 0xf, 0x70, 0x18, 0xb3, 0x18, 0xd, 0x48, 0x5, 0x10, 0x84, 0x6, 0x20, 0x41, 0xff, 0x81, 0x8, 0xed, 0x81, 0xc3, 0x6a, 0x82, 0xf, 0xee, 0x80, 0xc2, 0xe4, 0x0, 0x20, 0x34, 0x3f, 0x98, 0x4c, 0x78, 0xd0, 0x1b, 0xf0, 0xc1, 0x34, 0x83, 0x8, 0x78, 0x81, 0x88, 0xb4, 0x80, 0x42, 0xbb, 0x1, 0x10, 0x30, 0x89, 0xbc, 0xf, 0x24, 0x33, 0xc1, 0x82, 0xf4, 0x40, 0xa2, 0x74, 0xc0, 0x4, 0x5f, 0xc1, 0x21, 0x52, 0x40, 0xa1, 0x73, 0xc0, 0x26, 0xfb, 0xc0, 0xe4, 0x4, 0x9, 0xda, 0x9, 0x3c, 0xca, 0x9, 0xa0, 0x20, 0x7a, 0xc0, 0x21, 0x51, 0x0, 0x14, 0xf2, 0xf, 0x34, 0x44, 0x9, 0x58, 0xa1, 0xbe, 0x80, 0xe, 0xe2, 0xd, 0x54, 0x60, 0x7, 0x34, 0x20, 0x44, 0xd2, 0x19, 0x14, 0xbe, 0x1c, 0xfa, 0xd, 0x6c, 0x54, 0x25, 0x88, 0xc0, 0xd5, 0x40, 0x7} diff --git a/lib/rlepluslazy/rleplus_test.go b/lib/rlepluslazy/rleplus_test.go index 90aaee2e4..53444a47b 100644 --- a/lib/rlepluslazy/rleplus_test.go +++ b/lib/rlepluslazy/rleplus_test.go @@ -1,6 +1,8 @@ package rlepluslazy import ( + "math/rand" + "runtime" "testing" "github.com/filecoin-project/go-lotus/extern/rleplus" @@ -30,7 +32,10 @@ func TestDecode(t *testing.T) { assert.NoError(t, err) decoded := make([]uint64, 0, len(expectedNumbers)) - it, err := rle.Iterator() + rit, err := rle.RunIterator() + assert.NoError(t, err) + + it, err := BitsFromRuns(rit) assert.NoError(t, err) for it.HasNext() { bit, err := it.Next() @@ -40,5 +45,74 @@ func TestDecode(t *testing.T) { // Our decoded integers are the same as expected assert.Equal(t, expectedNumbers, decoded) - +} + +func TestGolden(t *testing.T) { + t.SkipNow() + N := 1000 + mod := uint32(1) << 16 + runExProp := float32(0.9) + + bits := make([]uint64, N) + + for i := 0; i < N; i++ { + x := rand.Uint32() % mod + bits[i] = uint64(x) + for rand.Float32() < runExProp && i+1 < N { + i++ + x = (x + 1) % mod + bits[i] = uint64(x) + } + } + + out, _, err := rleplus.Encode(bits) + assert.NoError(t, err) + t.Logf("%#v", out) + _, runs := rleplus.RunLengths(bits) + t.Logf("runs: %v", runs) +} + +func BenchmarkIterator(b *testing.B) { + decoded := make([]uint64, 0, 1000) + + for i := 0; i < b.N; i++ { + decoded = decoded[:0] + rle, _ := FromBuf(goldenRLE) + it, _ := rle.Iterator() + for it.HasNext() { + bit, _ := it.Next() + decoded = append(decoded, bit) + } + runtime.KeepAlive(decoded) + } +} + +func BenchmarkRunIterator(b *testing.B) { + runs := make([]Run, 0, 1000) + for i := 0; i < b.N; i++ { + runs = runs[:0] + rle, _ := FromBuf(goldenRLE) + rit, _ := rle.RunIterator() + for rit.HasNext() { + run, _ := rit.NextRun() + runs = append(runs, run) + } + runtime.KeepAlive(runs) + } +} + +func BenchmarkRunsToBits(b *testing.B) { + decoded := make([]uint64, 0, 1000) + + for i := 0; i < b.N; i++ { + decoded = decoded[:0] + rle, _ := FromBuf(goldenRLE) + rit, _ := rle.RunIterator() + it, _ := BitsFromRuns(rit) + for it.HasNext() { + bit, _ := it.Next() + decoded = append(decoded, bit) + } + runtime.KeepAlive(decoded) + } }