Эх сурвалжийг харах

More quotedPrintEncode speed-ups

Starting at a baseline of around 4600 ns/op:
 (1) Expanding "append(buf[:0], c)" into "buf[0] = c; nextOut = buf[:1]"
     shaved off about 1000 ns/op.
 (2) Using a [256]byte table lookup for isPrintable shaved off another
     600 ns/op
Shaving a little more than 1/3 of the time off on average, at least for
our test message.
Jed Denlea 11 жил өмнө
parent
commit
af35596614
1 өөрчлөгдсөн 16 нэмэгдсэн , 5 устгасан
  1. 16 5
      email.go

+ 16 - 5
email.go

@@ -226,8 +226,9 @@ func quotePrintEncode(w io.Writer, body []byte) error {
 		}
 
 		var nextOut []byte
-		if isPrintable(c) {
-			nextOut = append(buf[:0], c)
+		if isPrintable[c] {
+			buf[0] = c
+			nextOut = buf[:1]
 		} else {
 			nextOut = buf[:]
 			qpEscape(nextOut, c)
@@ -254,9 +255,19 @@ func quotePrintEncode(w io.Writer, body []byte) error {
 	return nil
 }
 
-// isPrintable returns true if the rune given is "printable" according to RFC 2045, false otherwise
-func isPrintable(c byte) bool {
-	return (c >= '!' && c <= '<') || (c >= '>' && c <= '~') || (c == ' ' || c == '\n' || c == '\t')
+// isPrintable holds true if the byte given is "printable" according to RFC 2045, false otherwise
+var isPrintable [256]bool
+
+func init() {
+	for c := '!'; c <= '<'; c++ {
+		isPrintable[c] = true
+	}
+	for c := '>'; c <= '~'; c++ {
+		isPrintable[c] = true
+	}
+	isPrintable[' '] = true
+	isPrintable['\n'] = true
+	isPrintable['\t'] = true
 }
 
 // qpEscape is a helper function for quotePrintEncode which escapes a