Skip to content

Optimize Entity.WriteTo when encoding/charset are unchanged#87

Open
emersion wants to merge 1 commit into
masterfrom
optimize-writeto
Open

Optimize Entity.WriteTo when encoding/charset are unchanged#87
emersion wants to merge 1 commit into
masterfrom
optimize-writeto

Conversation

@emersion

Copy link
Copy Markdown
Owner

Closes: #86

@codecov-io

Copy link
Copy Markdown

Codecov Report

Merging #87 into master will increase coverage by 0.20%.
The diff coverage is 95.65%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #87      +/-   ##
==========================================
+ Coverage   79.49%   79.70%   +0.20%     
==========================================
  Files          15       15              
  Lines         995     1005      +10     
==========================================
+ Hits          791      801      +10     
  Misses        128      128              
  Partials       76       76              
Impacted Files Coverage Δ
writer.go 72.54% <83.33%> (+1.12%) ⬆️
entity.go 93.22% <100.00%> (+1.06%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f828d02...584bd27. Read the comment docs.

@emersion

emersion commented Apr 29, 2020

Copy link
Copy Markdown
Owner Author

This doesn't work if Entity.Body is set to something else by the library user.

Comment thread entity.go
Comment on lines +124 to +125
} else if w.encoding == e.encoding && w.charset == e.charset {
_, err = io.Copy(w.rawWriter, e.originalBody)

@foxcpp foxcpp Apr 29, 2020

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps then this branch can also check whether e.Body was replaced by the user? Or, add a getter/setter pair for Body that will invalidate originalBody.

tienipia added a commit to saeroun-ai/emlkit that referenced this pull request Jun 9, 2026
…unchanged

WriteTo always decoded and re-encoded the body, which could change base64 line
wrapping or QP representation and alter DKIM body hashes. When the output
transfer encoding and charset match the input (case-insensitively), copy the
original undecoded bytes through verbatim, extending byte-preserving
round-tripping from the header to the body.

- encoding/charset are compared with EqualFold (case-insensitive per RFC)
- an Entity built without New (nil originalBody) falls back to the normal path
  to avoid a nil-reader panic
- originalBody shares the underlying reader with Body, so WriteTo must be called
  before reading Body (documented)

Originally by Simon Ser (emersion#87).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize Entity.WriteTo

3 participants