Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvement #107

Merged
merged 16 commits into from
Feb 13, 2024
30 changes: 18 additions & 12 deletions internal/rle/rle.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ import (

var encodedPool = sync.Pool{
New: func() interface{} {
return make([]uint8, 0, remarkable.ScreenHeight*remarkable.ScreenWidth)
return new(bytes.Buffer)
},
}

var bufferPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}

Expand Down Expand Up @@ -38,26 +44,26 @@ func (rlewriter *RLE) Write(data []byte) (int, error) {
if length == 0 {
return 0, nil
}
encoded := encodedPool.Get().([]uint8) // Borrow a slice from the pool
defer encodedPool.Put(encoded)
buf := bufferPool.Get().(*bytes.Buffer)
buf.Reset()
defer bufferPool.Put(buf)

current := data[0]
count := 0
count := uint8(0)

for _, datum := range data {
for i := 0; i < remarkable.ScreenWidth*remarkable.ScreenHeight; i++ {
datum := data[i*2]
if count < 254 && datum == current {
count++
} else {
encoded = append(encoded, uint8(count))
encoded = append(encoded, uint8(current))
buf.WriteByte(count)
buf.WriteByte(current)
current = datum
count = 1
}
}
buf.WriteByte(count)
buf.WriteByte(current)

encoded = append(encoded, uint8(count))
encoded = append(encoded, uint8(current))

n, err := io.Copy(rlewriter.sub, bytes.NewBuffer(encoded))
return int(n), err
return rlewriter.sub.Write(buf.Bytes())
}
31 changes: 17 additions & 14 deletions internal/stream/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (h *StreamHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer rawFrameBuffer.Put(rawData) // Return the slice to the pool when done
// the informations are int4, therefore store it in a uint8array to reduce data transfer
rleWriter := rle.NewRLE(w)
extractor := &oneOutOfTwo{rleWriter}
writing := true
stopWriting := time.NewTicker(2 * time.Second)
defer stopWriting.Stop()
Expand All @@ -102,20 +101,24 @@ func (h *StreamHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
writing = false
case <-ticker.C:
if writing {
_, err := h.file.ReadAt(rawData, h.pointerAddr)
if err != nil {
log.Println(err)
return
}
_, err = extractor.Write(rawData)
if err != nil {
log.Println("Error in writing", err)
return
}
if w, ok := w.(http.Flusher); ok {
w.Flush()
}
h.fetchAndSend(rleWriter, rawData)
}
}
}
}

func (h *StreamHandler) fetchAndSend(w io.Writer, rawData []uint8) {
_, err := h.file.ReadAt(rawData, h.pointerAddr)
if err != nil {
log.Println(err)
return
}
_, err = w.Write(rawData)
if err != nil {
log.Println("Error in writing", err)
return
}
if w, ok := w.(http.Flusher); ok {
w.Flush()
}
}
11 changes: 8 additions & 3 deletions internal/stream/oneoftwo.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package stream

import (
"encoding/binary"
"io"
"sync"

Expand All @@ -21,8 +20,14 @@ type oneOutOfTwo struct {
func (oneoutoftwo *oneOutOfTwo) Write(src []byte) (n int, err error) {
imageData := imagePool.Get().([]uint8)
defer imagePool.Put(imageData) // Return the slice to the pool when done
for i := 0; i < remarkable.ScreenHeight*remarkable.ScreenWidth; i++ {
imageData[i] = uint8(binary.LittleEndian.Uint16(src[i*2 : i*2+2]))
for i := range imageData {
imageData[i] = src[i*2] // Directly take the lower byte of each uint16 from src
}

/*
for i := 0; i < remarkable.ScreenHeight*remarkable.ScreenWidth; i++ {
imageData[i] = uint8(binary.LittleEndian.Uint16(src[i*2 : i*2+2]))
}
*/
return oneoutoftwo.w.Write(imageData)
}
Loading