-
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.
- Loading branch information
1 parent
41acb9e
commit d3ae2f4
Showing
9 changed files
with
267 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
Binary file not shown.
Binary file not shown.
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,144 @@ | ||
package stream | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
) | ||
|
||
var data = []struct { | ||
input []byte | ||
output []byte | ||
}{ | ||
{[]byte("abc"), []byte("abc")}, | ||
{[]byte("elvis"), []byte("Elvis")}, | ||
{[]byte("aElvis"), []byte("aElvis")}, | ||
} | ||
|
||
func assembleInputStream() []byte { | ||
var in []byte | ||
for _, d := range data { | ||
in = append(in, d.input...) | ||
} | ||
return in | ||
} | ||
|
||
func assembleOutputStream() []byte { | ||
var out []byte | ||
for _, d := range data { | ||
out = append(out, d.output...) | ||
} | ||
return out | ||
} | ||
|
||
func algOne(data []byte, find []byte, repl []byte, output *bytes.Buffer) { | ||
|
||
input := bytes.NewBuffer(data) | ||
|
||
size := len(find) | ||
|
||
buf := make([]byte, size) | ||
end := size - 1 | ||
|
||
if n, err := io.ReadFull(input, buf[:end]); err != nil { | ||
output.Write(buf[:n]) | ||
return | ||
} | ||
|
||
for { | ||
if _, err := io.ReadFull(input, buf[end:]); err != nil { | ||
output.Write(buf[:end]) | ||
return | ||
} | ||
|
||
if bytes.Compare(buf, find) == 0 { | ||
output.Write(repl) | ||
|
||
if n, err := io.ReadFull(input, buf[:end]); err != nil { | ||
output.Write(buf[:n]) | ||
return | ||
} | ||
continue | ||
} | ||
output.WriteByte(buf[0]) | ||
copy(buf, buf[1:]) | ||
} | ||
} | ||
|
||
func algTwo(data []byte, find []byte, repl []byte, output *bytes.Buffer) { | ||
|
||
input := bytes.NewReader(data) | ||
|
||
size := len(find) | ||
|
||
idx := 0 | ||
|
||
for { | ||
|
||
b, err := input.ReadByte() | ||
if err != nil { | ||
break | ||
} | ||
|
||
if b == find[idx] { | ||
|
||
idx++ | ||
|
||
if idx == size { | ||
output.Write(repl) | ||
idx = 0 | ||
} | ||
continue | ||
} | ||
|
||
if idx != 0 { | ||
output.Write(find[:idx]) | ||
|
||
input.UnreadByte() | ||
|
||
idx = 0 | ||
continue | ||
} | ||
|
||
output.WriteByte(b) | ||
idx = 0 | ||
} | ||
} | ||
|
||
func algThree(data []byte, find []byte, repl []byte, output *bytes.Buffer) { | ||
|
||
input := bytes.NewBuffer(data) | ||
|
||
size := len(find) | ||
|
||
buf := make([]byte, size) | ||
end := size - 1 | ||
|
||
if n, err := input.Read(buf[:end]); err != nil { | ||
output.Write(buf[:n]) | ||
return | ||
} | ||
|
||
for { | ||
|
||
var err error | ||
buf[end:][0], err = input.ReadByte() | ||
if err != nil { | ||
output.Write(buf[:end]) | ||
return | ||
} | ||
// if _, err := io.ReadFull(input, buf[end:]); err != nil { | ||
// } | ||
|
||
if bytes.Compare(buf, find) == 0 { | ||
output.Write(repl) | ||
|
||
if n, err := input.Read(buf[:end]); err != nil { | ||
output.Write(buf[:n]) | ||
return | ||
} | ||
continue | ||
} | ||
output.WriteByte(buf[0]) | ||
copy(buf, buf[1:]) | ||
} | ||
} |
Binary file not shown.
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,53 @@ | ||
package stream | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func BenchmarkAlgOne(b *testing.B) { | ||
var output bytes.Buffer | ||
in := assembleInputStream() | ||
|
||
find := []byte("elvis") | ||
repl := []byte("Elvis") | ||
|
||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
output.Reset() | ||
algOne(in, find, repl, &output) | ||
} | ||
} | ||
|
||
func BenchmarkAlgTwo(b *testing.B) { | ||
|
||
var output bytes.Buffer | ||
in := assembleInputStream() | ||
|
||
find := []byte("elvis") | ||
repl := []byte("Elvis") | ||
|
||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
output.Reset() | ||
algTwo(in, find, repl, &output) | ||
} | ||
} | ||
|
||
func BenchmarkAlgThree(b *testing.B) { | ||
|
||
var output bytes.Buffer | ||
in := assembleInputStream() | ||
|
||
find := []byte("elvis") | ||
repl := []byte("Elvis") | ||
|
||
b.ResetTimer() | ||
|
||
for i := 0; i < b.N; i++ { | ||
output.Reset() | ||
algThree(in, find, repl, &output) | ||
} | ||
} |
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,22 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
// const MaxPacketSize = 4096 | ||
|
||
var bufPool = sync.Pool{ | ||
New: func() interface{} { | ||
return make([]byte, 2) | ||
}, | ||
} | ||
|
||
func main() { | ||
a := [...]int{0, 1, 2, 3, 4, 5, 6} | ||
b := bufPool.Get().([]byte) | ||
fmt.Println(len(b), cap(b)) | ||
b[0] = 1 | ||
b[1] = 2 | ||
} |
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,34 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
func main() { | ||
f, err := os.OpenFile("mmap.bin", os.O_RDWR|os.O_CREATE, 0644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
n, err := f.WriteAt([]byte{byte(0)}, 1<<8) | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println("n = ", n) | ||
data, err := syscall.Mmap(int(f.Fd()), 0, 1<<8, syscall.PROT_WRITE, syscall.MAP_SHARED) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
f.Close() | ||
|
||
for i, v := range []byte("hello syscall") { | ||
data[i] = v | ||
} | ||
|
||
fmt.Println(data) | ||
|
||
syscall.Munmap(data) | ||
} |
Binary file not shown.