Răsfoiți Sursa

just small optimizations:
- regexes are compiled once
- md5 hashing & compressing functions use less memory (data is streamed into buffers)
- avoid string concatenation when passing to the hashing & compressing functions
- pass strings by reference to md5 & compress functions

flashmob 9 ani în urmă
părinte
comite
7f7f4e68f3
2 a modificat fișierele cu 40 adăugiri și 22 ștergeri
  1. 7 2
      save_mail.go
  2. 33 20
      util.go

+ 7 - 2
save_mail.go

@@ -65,8 +65,13 @@ func saveMail() {
 			to = user + "@" + mainConfig.Primary_host
 			to = user + "@" + mainConfig.Primary_host
 		}
 		}
 		length = len(payload.client.data)
 		length = len(payload.client.data)
+		ts := strconv.FormatInt(time.Now().UnixNano(), 10);
 		payload.client.subject = mimeHeaderDecode(payload.client.subject)
 		payload.client.subject = mimeHeaderDecode(payload.client.subject)
-		payload.client.hash = md5hex(to + payload.client.mail_from + payload.client.subject + strconv.FormatInt(time.Now().UnixNano(), 10))
+		payload.client.hash = md5hex(
+			&to,
+			&payload.client.mail_from,
+			&payload.client.subject,
+			&ts)
 		// Add extra headers
 		// Add extra headers
 		add_head := ""
 		add_head := ""
 		add_head += "Delivered-To: " + to + "\r\n"
 		add_head += "Delivered-To: " + to + "\r\n"
@@ -75,7 +80,7 @@ func saveMail() {
 			payload.server.Config.Host_name + ";\r\n"
 			payload.server.Config.Host_name + ";\r\n"
 		add_head += "	" + time.Now().Format(time.RFC1123Z) + "\r\n"
 		add_head += "	" + time.Now().Format(time.RFC1123Z) + "\r\n"
 		// compress to save space
 		// compress to save space
-		payload.client.data = compress(add_head + payload.client.data)
+		payload.client.data = compress(&add_head, &payload.client.data)
 		body = "gzencode"
 		body = "gzencode"
 		redis_err = redisClient.redisConnection()
 		redis_err = redisClient.redisConnection()
 		if redis_err == nil {
 		if redis_err == nil {

+ 33 - 20
util.go

@@ -5,13 +5,14 @@ import (
 	"compress/zlib"
 	"compress/zlib"
 	"crypto/md5"
 	"crypto/md5"
 	"encoding/base64"
 	"encoding/base64"
-	"encoding/hex"
 	"errors"
 	"errors"
 	"github.com/sloonz/go-qprintable"
 	"github.com/sloonz/go-qprintable"
 	"gopkg.in/iconv.v1"
 	"gopkg.in/iconv.v1"
 	"io/ioutil"
 	"io/ioutil"
 	"regexp"
 	"regexp"
 	"strings"
 	"strings"
+	"io"
+	"fmt"
 )
 )
 
 
 func validateEmailData(client *Client) (user string, host string, addr_err error) {
 func validateEmailData(client *Client) (user string, host string, addr_err error) {
@@ -30,9 +31,10 @@ func validateEmailData(client *Client) (user string, host string, addr_err error
 	return user, host, addr_err
 	return user, host, addr_err
 }
 }
 
 
+var extractEmailRegex, _ = regexp.Compile(`<(.+?)@(.+?)>`) // go home regex, you're drunk!
+
 func extractEmail(str string) (name string, host string, err error) {
 func extractEmail(str string) (name string, host string, err error) {
-	re, _ := regexp.Compile(`<(.+?)@(.+?)>`) // go home regex, you're drunk!
-	if matched := re.FindStringSubmatch(str); len(matched) > 2 {
+	if matched := extractEmailRegex.FindStringSubmatch(str); len(matched) > 2 {
 		host = validHost(matched[2])
 		host = validHost(matched[2])
 		name = matched[1]
 		name = matched[1]
 	} else {
 	} else {
@@ -46,12 +48,12 @@ func extractEmail(str string) (name string, host string, err error) {
 	}
 	}
 	return name, host, err
 	return name, host, err
 }
 }
-
+var mimeRegex, _ = regexp.Compile(`=\?(.+?)\?([QBqp])\?(.+?)\?=`)
 // Decode strings in Mime header format
 // Decode strings in Mime header format
 // eg. =?ISO-2022-JP?B?GyRCIVo9dztSOWJAOCVBJWMbKEI=?=
 // eg. =?ISO-2022-JP?B?GyRCIVo9dztSOWJAOCVBJWMbKEI=?=
 func mimeHeaderDecode(str string) string {
 func mimeHeaderDecode(str string) string {
-	reg, _ := regexp.Compile(`=\?(.+?)\?([QBqp])\?(.+?)\?=`)
-	matched := reg.FindAllStringSubmatch(str, -1)
+
+	matched := mimeRegex.FindAllStringSubmatch(str, -1)
 	var charset, encoding, payload string
 	var charset, encoding, payload string
 	if matched != nil {
 	if matched != nil {
 		for i := 0; i < len(matched); i++ {
 		for i := 0; i < len(matched); i++ {
@@ -79,10 +81,10 @@ func mimeHeaderDecode(str string) string {
 	return str
 	return str
 }
 }
 
 
+var valihostRegex, _ = regexp.Compile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`)
 func validHost(host string) string {
 func validHost(host string) string {
 	host = strings.Trim(host, " ")
 	host = strings.Trim(host, " ")
-	re, _ := regexp.Compile(`^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$`)
-	if re.MatchString(host) {
+	if valihostRegex.MatchString(host) {
 		return host
 		return host
 	}
 	}
 	return ""
 	return ""
@@ -133,17 +135,10 @@ func fromQuotedP(data string) string {
 	return string(res)
 	return string(res)
 }
 }
 
 
-func compress(s string) string {
-	var b bytes.Buffer
-	w, _ := zlib.NewWriterLevel(&b, zlib.BestSpeed) // flate.BestCompression
-	w.Write([]byte(s))
-	w.Close()
-	return b.String()
-}
 
 
+var charsetRegex, _ = regexp.Compile(`[_:.\/\\]`)
 func fixCharset(charset string) string {
 func fixCharset(charset string) string {
-	reg, _ := regexp.Compile(`[_:.\/\\]`)
-	fixed_charset := reg.ReplaceAllString(charset, "-")
+	fixed_charset := charsetRegex.ReplaceAllString(charset, "-")
 	// Fix charset
 	// Fix charset
 	// borrowed from http://squirrelmail.svn.sourceforge.net/viewvc/squirrelmail/trunk/squirrelmail/include/languages.php?revision=13765&view=markup
 	// borrowed from http://squirrelmail.svn.sourceforge.net/viewvc/squirrelmail/trunk/squirrelmail/include/languages.php?revision=13765&view=markup
 	// OE ks_c_5601_1987 > cp949
 	// OE ks_c_5601_1987 > cp949
@@ -164,9 +159,27 @@ func fixCharset(charset string) string {
 	return charset
 	return charset
 }
 }
 
 
-func md5hex(str string) string {
+// returns an md5 hash as string of hex characters
+func md5hex(stringArguments ...*string) string {
 	h := md5.New()
 	h := md5.New()
-	h.Write([]byte(str))
+	var r *strings.Reader
+	for i:=0; i < len(stringArguments); i++ {
+		r = strings.NewReader(*stringArguments[i])
+		io.Copy(h, r)
+	}
 	sum := h.Sum([]byte{})
 	sum := h.Sum([]byte{})
-	return hex.EncodeToString(sum)
+	return fmt.Sprintf("%x", sum)
+}
+
+// concatenate & compress all strings  passed in
+func compress(stringArguments ...*string) string {
+	var b bytes.Buffer
+	var r *strings.Reader
+	w, _ := zlib.NewWriterLevel(&b, zlib.BestSpeed)
+	for i:=0; i < len(stringArguments); i++ {
+		r = strings.NewReader(*stringArguments[i])
+		io.Copy(w, r)
+	}
+	w.Close()
+	return b.String()
 }
 }