@@ -222,7 +222,9 @@ func (this *ImapServer) parseArgsConent(format string, data db.Mail) (string, er
222222 format = strings .Trim (format , "()" )
223223
224224 inputN := strings .Split (format , " " )
225- list := make (map [string ]interface {})
225+
226+ // 预分配足够的空间
227+ list := make (map [string ]interface {}, len (inputN ))
226228
227229 // 使用 bufio.NewReader 因为 component.ReadHeader 要求这个类型
228230 bufferedBody := bufio .NewReader (strings .NewReader (content ))
@@ -237,28 +239,34 @@ func (this *ImapServer) parseArgsConent(format string, data db.Mail) (string, er
237239 for i := 0 ; i < len (inputN ); i ++ {
238240
239241 if strings .EqualFold (inputN [i ], "uid" ) {
240- uid_id := fmt .Sprintf ("%d" , id )
242+ // 使用 strconv.FormatInt 避免 fmt.Sprintf 的开销
243+ uid_id := strconv .FormatInt (id , 10 )
241244 list [inputN [i ]] = uid_id
242245 }
243246
244247 if strings .EqualFold (inputN [i ], "flags" ) {
245- flags := "("
248+ // 预分配 flags 字符串
249+ flagsBuf := tools .BufferPoolInstance .Get ()
250+ defer tools .BufferPoolInstance .Put (flagsBuf )
251+
252+ flagsBuf .WriteString ("(" )
246253 if data .IsRead {
247- flags += "\\ SEEN"
254+ flagsBuf . WriteString ( "\\ SEEN" )
248255 } else {
249- flags += "\\ UNSEEN"
256+ flagsBuf . WriteString ( "\\ UNSEEN" )
250257 }
251258
252259 if data .IsFlags {
253- flags += "\\ Flagged"
260+ flagsBuf . WriteString ( "\\ Flagged" )
254261 }
255262
256- flags += ")"
257- list [inputN [i ]] = flags
263+ flagsBuf . WriteString ( ")" )
264+ list [inputN [i ]] = flagsBuf . String ()
258265 }
259266
260267 if strings .EqualFold (inputN [i ], "rfc822.size" ) {
261- rfc822_size := fmt .Sprintf ("%d" , len (content ))
268+ // 使用 strconv.Itoa 避免 fmt.Sprintf 的开销
269+ rfc822_size := strconv .Itoa (len (content ))
262270 list [inputN [i ]] = rfc822_size
263271 }
264272
@@ -270,11 +278,22 @@ func (this *ImapServer) parseArgsConent(format string, data db.Mail) (string, er
270278 // 为 header 重新创建 reader,避免重置缓冲区的开销
271279 headerReader := bufio .NewReader (strings .NewReader (content ))
272280 headerString , _ := component .ReadHeaderString (headerReader )
273- list ["body[header]" ] = fmt .Sprintf ("{%d}\r \n %s" , len (headerString ), headerString )
281+
282+ // 预分配缓冲区
283+ headerBuf := tools .BufferPoolInstance .Get ()
284+ defer tools .BufferPoolInstance .Put (headerBuf )
285+
286+ fmt .Fprintf (headerBuf , "{%d}\r \n %s" , len (headerString ), headerString )
287+ list ["body[header]" ] = headerBuf .String ()
274288 }
275289
276290 if strings .EqualFold (inputN [i ], "body.peek[]" ) {
277- list ["body[]" ] = fmt .Sprintf ("{%d}\r \n %s" , len (content ), content )
291+ // 预分配缓冲区
292+ bodyBuf := tools .BufferPoolInstance .Get ()
293+ defer tools .BufferPoolInstance .Put (bodyBuf )
294+
295+ fmt .Fprintf (bodyBuf , "{%d}\r \n %s" , len (content ), content )
296+ list ["body[]" ] = bodyBuf .String ()
278297 db .MailSeenById (id )
279298 }
280299 }
@@ -292,7 +311,8 @@ func (this *ImapServer) parseArgsConent(format string, data db.Mail) (string, er
292311 }
293312 }
294313
295- out := fmt .Sprintf ("(%s)" , outBuf .String ())
314+ // 直接构造输出字符串
315+ out := "(" + outBuf .String () + ")"
296316 return out , nil
297317}
298318
0 commit comments