Improve attachment memory allocation (#105)

* Improve attachment memory allocation

* added explicit garbage collection

* redundant GC

* added GCs in attachment processor and a FreeOSMemory helper

* added changelog

* linting

* Remove commented call to debug.FreeOSMemory

Co-authored-by: marin thiercelin <marin.thiercelin@pm.me>
Co-authored-by: marinthiercelin <marin.thiercelin@epfl.ch>
This commit is contained in:
wussler 2020-12-08 17:52:50 +01:00 committed by GitHub
parent 75f27fd1df
commit 6105a3bcc0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 2 deletions

View file

@ -4,12 +4,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Added
- `helper.FreeOSMemory()` to explicitly call the GC and release the memory to the OS
### Changed
- added new calls to `runtime.GC()` in the low memory attachment processor
## [2.1.2] 2020-12-01
### Added
- `SetKeyGenerationOffset` to add an offset in key generation time and prevent not-yet-valid keys.
### Changed
- Improved canonicalization performance
- Reduced attachment memory allocation
## [2.1.1] 2020-11-16
### Changed

View file

@ -29,6 +29,9 @@ func (ap *AttachmentProcessor) Process(plainData []byte) {
if _, err := (*ap.w).Write(plainData); err != nil {
panic(err)
}
if ap.garbageCollector > 0 {
defer runtime.GC()
}
}
// Finish closes the attachment and returns the encrypted data.
@ -36,19 +39,29 @@ func (ap *AttachmentProcessor) Finish() (*PGPSplitMessage, error) {
if ap.err != nil {
return nil, ap.err
}
if err := (*ap.w).Close(); err != nil {
return nil, errors.Wrap(err, "gopengpp: unable to close writer")
}
if ap.garbageCollector > 0 {
ap.w = nil
runtime.GC()
}
if err := (*ap.pipe).Close(); err != nil {
return nil, errors.Wrap(err, "gopengpp: unable to close pipe")
}
ap.done.Wait()
splitMsg := ap.split
if ap.garbageCollector > 0 {
runtime.GC()
ap.pipe = nil
ap.split = nil
defer runtime.GC()
}
return ap.split, nil
return splitMsg, nil
}
// newAttachmentProcessor creates an AttachmentProcessor which can be used to encrypt

View file

@ -3,6 +3,7 @@ package helper
import (
"encoding/json"
goerrors "errors"
"runtime/debug"
"github.com/ProtonMail/gopenpgp/v2/crypto"
"github.com/pkg/errors"
@ -124,3 +125,10 @@ func EncryptSignBinaryDetachedMobile(
EncryptedSignatureArmored: encryptedSignature,
}, nil
}
// FreeOSMemory can be used to explicitly
// call the garbage collector and
// return the unused memory to the OS.
func FreeOSMemory() {
debug.FreeOSMemory()
}