Skip to content

Commit

Permalink
just small optimizations:
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
flashmob committed Nov 3, 2016
1 parent e85eb42 commit 7f7f4e6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
9 changes: 7 additions & 2 deletions save_mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ func saveMail() {
to = user + "@" + mainConfig.Primary_host
}
length = len(payload.client.data)
ts := strconv.FormatInt(time.Now().UnixNano(), 10);
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_head := ""
add_head += "Delivered-To: " + to + "\r\n"
Expand All @@ -75,7 +80,7 @@ func saveMail() {
payload.server.Config.Host_name + ";\r\n"
add_head += " " + time.Now().Format(time.RFC1123Z) + "\r\n"
// compress to save space
payload.client.data = compress(add_head + payload.client.data)
payload.client.data = compress(&add_head, &payload.client.data)
body = "gzencode"
redis_err = redisClient.redisConnection()
if redis_err == nil {
Expand Down
53 changes: 33 additions & 20 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (
"compress/zlib"
"crypto/md5"
"encoding/base64"
"encoding/hex"
"errors"
"github.com/sloonz/go-qprintable"
"gopkg.in/iconv.v1"
"io/ioutil"
"regexp"
"strings"
"io"
"fmt"
)

func validateEmailData(client *Client) (user string, host string, addr_err error) {
Expand All @@ -30,9 +31,10 @@ func validateEmailData(client *Client) (user string, host string, addr_err error
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) {
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])
name = matched[1]
} else {
Expand All @@ -46,12 +48,12 @@ func extractEmail(str string) (name string, host string, err error) {
}
return name, host, err
}

var mimeRegex, _ = regexp.Compile(`=\?(.+?)\?([QBqp])\?(.+?)\?=`)
// Decode strings in Mime header format
// eg. =?ISO-2022-JP?B?GyRCIVo9dztSOWJAOCVBJWMbKEI=?=
func mimeHeaderDecode(str string) string {
reg, _ := regexp.Compile(`=\?(.+?)\?([QBqp])\?(.+?)\?=`)
matched := reg.FindAllStringSubmatch(str, -1)

matched := mimeRegex.FindAllStringSubmatch(str, -1)
var charset, encoding, payload string
if matched != nil {
for i := 0; i < len(matched); i++ {
Expand Down Expand Up @@ -79,10 +81,10 @@ func mimeHeaderDecode(str string) string {
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 {
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 ""
Expand Down Expand Up @@ -133,17 +135,10 @@ func fromQuotedP(data string) string {
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 {
reg, _ := regexp.Compile(`[_:.\/\\]`)
fixed_charset := reg.ReplaceAllString(charset, "-")
fixed_charset := charsetRegex.ReplaceAllString(charset, "-")
// Fix charset
// borrowed from http://squirrelmail.svn.sourceforge.net/viewvc/squirrelmail/trunk/squirrelmail/include/languages.php?revision=13765&view=markup
// OE ks_c_5601_1987 > cp949
Expand All @@ -164,9 +159,27 @@ func fixCharset(charset string) string {
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.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{})
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()
}

0 comments on commit 7f7f4e6

Please sign in to comment.