Optimize memory allocation of the apat algorithms

Former-commit-id: 5010c5a666b322715b3b81c1078d325e1f647ede
This commit is contained in:
2023-03-28 19:37:05 +07:00
parent 21819cd41e
commit 988ae79989
19 changed files with 117 additions and 180 deletions

View File

@@ -61,15 +61,20 @@ type BioSequence struct {
}
// MakeEmptyBioSequence() creates a new BioSequence object with no data
func MakeEmptyBioSequence() BioSequence {
func MakeEmptyBioSequence(preallocate int) BioSequence {
atomic.AddInt32(&_NewSeq, 1)
atomic.AddInt32(&_InMemSeq, 1)
seq := []byte(nil)
if preallocate > 0 {
seq = GetSlice(preallocate)
}
return BioSequence{
id: "",
definition: "",
source: "",
sequence: nil,
sequence: seq,
qualities: nil,
feature: nil,
paired: nil,
@@ -78,8 +83,8 @@ func MakeEmptyBioSequence() BioSequence {
}
// `NewEmptyBioSequence()` returns a pointer to a new empty BioSequence
func NewEmptyBioSequence() *BioSequence {
s := MakeEmptyBioSequence()
func NewEmptyBioSequence(preallocate int) *BioSequence {
s := MakeEmptyBioSequence(preallocate)
return &s
}
@@ -87,7 +92,7 @@ func NewEmptyBioSequence() *BioSequence {
func MakeBioSequence(id string,
sequence []byte,
definition string) BioSequence {
bs := MakeEmptyBioSequence()
bs := MakeEmptyBioSequence(0)
bs.SetId(id)
bs.SetSequence(sequence)
bs.SetDefinition(definition)
@@ -127,7 +132,7 @@ func (sequence *BioSequence) Recycle() {
// Copying the BioSequence.
func (s *BioSequence) Copy() *BioSequence {
newSeq := MakeEmptyBioSequence()
newSeq := MakeEmptyBioSequence(0)
newSeq.id = s.id
newSeq.definition = s.definition

View File

@@ -34,14 +34,22 @@ func MakeBioSequenceSlice(size ...int) BioSequenceSlice {
return *NewBioSequenceSlice(size...)
}
func (s *BioSequenceSlice) Recycle() {
func (s *BioSequenceSlice) Recycle(including_seq bool) {
if s == nil {
log.Panicln("Trying too recycle a nil pointer")
}
// Code added to potentially limit memory leaks
for i := range *s {
(*s)[i] = nil
if including_seq {
for i := range *s {
(*s)[i] .Recycle()
(*s)[i] = nil
}
} else {
for i := range *s {
(*s)[i] = nil
}
}
*s = (*s)[:0]

View File

@@ -213,7 +213,7 @@ func (sequences BioSequenceSlice) Merge(na string, statsOn []string) *BioSequenc
}
}
sequences.Recycle()
sequences.Recycle(false)
return seq
}

View File

@@ -20,7 +20,9 @@ func RecycleSlice(s *[]byte) {
if cap(*s) == 0 {
log.Panicln("trying to store a NIL slice in the pool", s == nil, *s == nil, cap(*s))
}
_BioSequenceByteSlicePool.Put(s)
if cap(*s) <= 1024 {
_BioSequenceByteSlicePool.Put(s)
}
}
}
@@ -28,7 +30,10 @@ func RecycleSlice(s *[]byte) {
//
// the slice can be prefilled with the provided values
func GetSlice(capacity int) []byte {
p := _BioSequenceByteSlicePool.Get().(*[]byte)
p := (*[]byte)(nil)
if capacity <= 1024 {
p = _BioSequenceByteSlicePool.Get().(*[]byte)
}
if p == nil || *p == nil || cap(*p) < capacity {
s := make([]byte, 0, capacity)

View File

@@ -8,7 +8,6 @@ import (
// Returns a sub sequence start from position 'from' included,
// to position 'to' excluded. Coordinates start at position 0.
func (sequence *BioSequence) Subsequence(from, to int, circular bool) (*BioSequence, error) {
if from >= to && !circular {
return nil, errors.New("from greater than to")
}
@@ -24,10 +23,11 @@ func (sequence *BioSequence) Subsequence(from, to int, circular bool) (*BioSeque
var newSeq *BioSequence
if from < to {
newSeq = NewEmptyBioSequence()
newSeq.Write(sequence.Sequence()[from:to])
newSeq = NewEmptyBioSequence(0)
newSeq.sequence = CopySlice(sequence.Sequence()[from:to])
if sequence.HasQualities() {
newSeq.qualities = CopySlice(sequence.Qualities()[from:to])
newSeq.WriteQualities(sequence.Qualities()[from:to])
}