MOAR SPEED

this needs some severe cleaning
master
Jordan Orelli 10 years ago
parent fc5db3defb
commit af39963304

@ -80,15 +80,20 @@ func main() {
sent := make(chan value, options.buffer) sent := make(chan value, options.buffer)
go streamValues(infile, c) go streamValues(infile, c)
go func() { go func() {
w := bufio.NewWriterSize(conn, 4048) w := bufio.NewWriterSize(conn, 16384)
defer func() { defer func() {
close(sent) close(sent)
fmt.Println("All data transferred. Waiting for the last reply...") fmt.Println("All data transferred. Waiting for the last reply...")
}() }()
count := 0
for r := range c { for r := range c {
count++
if r.ok() { if r.ok() {
r.val().Write(w) r.val().Write(w)
if count == 100 {
w.Flush() w.Flush()
count = 0
}
sent <- r.val() sent <- r.val()
} else { } else {
fmt.Fprintf(os.Stderr, "InputError: %v\n", r.err()) fmt.Fprintf(os.Stderr, "InputError: %v\n", r.err())

@ -125,7 +125,11 @@ func readString(b []byte) (value, error) {
} }
func (s String) Write(w io.Writer) (int, error) { func (s String) Write(w io.Writer) (int, error) {
return fmt.Fprintf(w, "+%s\r\n", s) w.Write([]byte{'+'})
w.Write([]byte(s))
w.Write([]byte{'\r', '\n'})
return 0, nil
// return fmt.Fprintf(w, "+%s\r\n", s)
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@ -137,7 +141,11 @@ func readError(b []byte) (value, error) {
} }
func (e Error) Write(w io.Writer) (int, error) { func (e Error) Write(w io.Writer) (int, error) {
return fmt.Fprintf(w, "-%s\r\n") w.Write([]byte{'-'})
w.Write([]byte(e))
w.Write([]byte{'\r', '\n'})
return 0, nil
// return fmt.Fprintf(w, "-%s\r\n")
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@ -153,7 +161,11 @@ func readInteger(b []byte) (value, error) {
} }
func (i Integer) Write(w io.Writer) (int, error) { func (i Integer) Write(w io.Writer) (int, error) {
return fmt.Fprintf(w, ":%d\r\n", i) w.Write([]byte{':'})
w.Write([]byte(strconv.Itoa(int(i))))
w.Write([]byte{'\r', '\n'})
return 0, nil
// return fmt.Fprintf(w, ":%d\r\n", i)
} }
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
@ -198,7 +210,13 @@ func readBulkString(prefix []byte, r io.Reader) (value, error) {
} }
func (s BulkString) Write(w io.Writer) (int, error) { func (s BulkString) Write(w io.Writer) (int, error) {
return fmt.Fprintf(w, "$%d\r\n%s\r\n", len(s), s) w.Write([]byte{'$'})
w.Write([]byte(strconv.Itoa(len(s))))
w.Write([]byte{'\r', '\n'})
w.Write([]byte(s))
w.Write([]byte{'\r', '\n'})
return 0, nil
// return fmt.Fprintf(w, "$%d\r\n%s\r\n", len(s), s)
} }
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
@ -230,21 +248,26 @@ func readArray(prefix []byte, r *bufio.Reader) (value, error) {
} }
func (a Array) Write(w io.Writer) (int, error) { func (a Array) Write(w io.Writer) (int, error) {
n, err := fmt.Fprintf(w, "*%d\r\n", len(a)) w.Write([]byte{'*'})
if err != nil { w.Write([]byte(strconv.Itoa(len(a))))
return n, err w.Write([]byte{'\r', '\n'})
} // n, err := fmt.Fprintf(w, "*%d\r\n", len(a))
// if err != nil {
var ( // return n, err
nn int // }
e error
) // var (
// nn int
// e error
// )
for i := 0; i < len(a); i++ { for i := 0; i < len(a); i++ {
nn, e = a[i].Write(w) a[i].Write(w)
n += nn // nn, e = a[i].Write(w)
if e != nil { // n += nn
return n, e // if e != nil {
} // return n, e
// }
} }
return n, nil // return n, nil
return 0, nil
} }

Loading…
Cancel
Save