Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add IoVec APIs to improve perf
Browse files Browse the repository at this point in the history
  • Loading branch information
bzEq committed Mar 31, 2024
1 parent ca4e053 commit 00a2005
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 11 deletions.
42 changes: 36 additions & 6 deletions core/iovec/iovec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func (self IoVec) Concat() []byte {
}

func (self *IoVec) Take(s []byte) *IoVec {
if len(s) == 0 {
return self
}
*self = append(*self, s)
return self
}
Expand Down Expand Up @@ -69,6 +72,31 @@ func (self *IoVec) Consume() []byte {
return b.Bytes()
}

func (self IoVec) LastByte() (byte, error) {
l := len(self)
if l == 0 {
return 0, fmt.Errorf("This IoVec is empty")
}
k := len(self[l-1])
return self[l-1][k-1], nil
}

func (self *IoVec) Drop(s int) error {
l := len(*self)
c := 0
for i := l - 1; i >= 0; i-- {
v := (*self)[i]
vl := len(v)
if c+vl >= s {
(*self) = (*self)[:i]
self.Take(v[:c+vl-s])
return nil
}
c += vl
}
return fmt.Errorf("Unable to drop %d bytes", s)
}

func (self IoVec) At(i int) (byte, error) {
c := 0
for _, v := range self {
Expand All @@ -91,17 +119,19 @@ func (self IoVec) Peek(i int) ([]byte, error) {
return nil, fmt.Errorf("Index %d out of bound", i)
}

func (self *IoVec) Drop(i int) error {
func (self *IoVec) Split(i int) (tail IoVec) {
c := 0
for k, v := range *self {
if i >= c && i < c+len(v) {
*self = (*self)[:k]
if i-c != 0 {
*self = append(*self, v[:i-c])
tail.Take(v[i-c:])
if len((*self)[k+1:]) != 0 {
tail = append(tail, (*self)[k+1:]...)
}
return nil
*self = (*self)[:k]
self.Take(v[:i-c])
return
}
c += len(v)
}
return fmt.Errorf("Index %d out of bound", i)
return
}
64 changes: 62 additions & 2 deletions core/iovec/iovec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestDrop(t *testing.T) {
var v IoVec
v.Take([]byte("hello"))
v.Take([]byte("bar"))
if err := v.Drop(5); err != nil {
if err := v.Drop(3); err != nil {
t.Fail()
}
s := string(v.Consume())
Expand All @@ -45,7 +45,7 @@ func TestDrop2(t *testing.T) {
var v IoVec
v.Take([]byte("hello"))
v.Take([]byte("bar"))
if err := v.Drop(6); err != nil {
if err := v.Drop(2); err != nil {
t.Fail()
}
s := string(v.Consume())
Expand All @@ -54,6 +54,66 @@ func TestDrop2(t *testing.T) {
}
}

func TestSplit(t *testing.T) {
var v IoVec
v.Take([]byte("hello"))
v.Take([]byte("bar"))
tail := v.Split(0)
s := string(tail.Consume())
if s != "hellobar" {
t.Fail()
}
sv := string(v.Consume())
if sv != "" {
t.Fail()
}
}

func TestSplit1(t *testing.T) {
var v IoVec
v.Take([]byte("hello"))
v.Take([]byte("bar"))
tail := v.Split(5)
s := string(tail.Consume())
if s != "bar" {
t.Fail()
}
sv := string(v.Consume())
if sv != "hello" {
t.Fail()
}
}

func TestSplit2(t *testing.T) {
var v IoVec
v.Take([]byte("hello"))
v.Take([]byte("bar"))
tail := v.Split(6)
s := string(tail.Consume())
if s != "ar" {
t.Fail()
}
sv := string(v.Consume())
if sv != "hellob" {
t.Fail()
}
}

func TestSplit3(t *testing.T) {
var v IoVec
v.Take([]byte("hello"))
v.Take([]byte("bar"))
tail := v.Split(3)
s := string(tail.Consume())
if s != "lobar" {
t.Fail()
}
sv := string(v.Consume())
if sv != "hel" {
t.Fail()
}
}

func TestConcat(t *testing.T) {
var v IoVec
v.Take([]byte("hello"))
Expand Down
5 changes: 2 additions & 3 deletions passes/passes.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,11 @@ type OBFSDecoder struct {
}

func (self *OBFSDecoder) Run(b *iovec.IoVec) error {
l := b.Len()
t, err := b.At(l - 1)
t, err := b.LastByte()
if err != nil {
return err
}
if err := b.Drop(l - (1 + int(t))); err != nil {
if err := b.Drop(1 + int(t)); err != nil {
return err
}
return WrapLegacyPass(self, b)
Expand Down

0 comments on commit 00a2005

Please sign in to comment.