Skip to content

Commit b50e8df

Browse files
committed
Updated the VCFV4 class to extend the SharedPtrPool class
1 parent 5c95081 commit b50e8df

2 files changed

Lines changed: 17 additions & 75 deletions

File tree

libtiledbvcf/src/vcf/vcf_v4.cc

Lines changed: 14 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace tiledb {
3030
namespace vcf {
3131

3232
VCFV4::VCFV4(SharingMode mode)
33-
: mode_(mode)
33+
: SharedPtrPool<bcf1_t>(mode)
3434
, open_(false)
3535
, inited_(false)
3636
, max_record_buffer_size_(10000)
@@ -102,7 +102,7 @@ void VCFV4::open(const std::string& file, const std::string& index_file) {
102102
void VCFV4::close() {
103103
// Clear the record queue and associated allocation pool.
104104
std::queue<SafeSharedBCFRec>().swap(record_queue_);
105-
std::queue<SafeSharedBCFRec>().swap(record_queue_pool_);
105+
clear_ptr_pool();
106106

107107
if (index_hts_ != nullptr) {
108108
hts_idx_destroy(index_hts_);
@@ -156,13 +156,7 @@ void VCFV4::pop_record() {
156156
}
157157

158158
void VCFV4::return_record(SafeSharedBCFRec& record) {
159-
if (mode_ == SharingMode::MANUAL) {
160-
record_queue_pool_.emplace(std::move(record));
161-
} else {
162-
LOG_ERROR(
163-
"VCFV4::return_record records cannot be manually returned for mode {}",
164-
modeToString(mode_));
165-
}
159+
return_ptr_to_pool(record);
166160
}
167161

168162
std::string VCFV4::contig_name(bcf1_t* const r) const {
@@ -291,21 +285,6 @@ bool VCFV4::seek(const std::string& contig_name, uint32_t pos) {
291285
return !record_queue_.empty();
292286
}
293287

294-
void VCFV4::create_record(SafeBCFRec& tmp_r) {
295-
SafeSharedBCFRec r(bcf_dup(tmp_r.get()), bcf_destroy);
296-
bcf_unpack(r.get(), BCF_UN_ALL);
297-
record_queue_.emplace(std::move(r));
298-
}
299-
300-
void VCFV4::reuse_record(SafeBCFRec& tmp_r) {
301-
SafeSharedBCFRec r = record_queue_pool_.front();
302-
record_queue_pool_.pop();
303-
// Use `bcf_copy` to destroy (free) the stale data to prevent memory leaks
304-
bcf_copy(r.get(), tmp_r.get());
305-
bcf_unpack(r.get(), BCF_UN_ALL);
306-
record_queue_.emplace(std::move(r));
307-
}
308-
309288
void VCFV4::read_records() {
310289
if (!record_queue_.empty())
311290
std::queue<SafeSharedBCFRec>().swap(record_queue_);
@@ -322,21 +301,17 @@ void VCFV4::read_records() {
322301
break;
323302
}
324303

325-
if (!record_queue_pool_.empty() && mode_ == SharingMode::MANUAL) {
326-
reuse_record(tmp_r);
327-
} else if (!record_queue_pool_.empty() && mode_ == SharingMode::AUTOMATIC) {
328-
// Check if the record at the front of the pool is stale, i.e. the pool
329-
// has the only copy
330-
SafeSharedBCFRec& r = record_queue_pool_.front();
331-
if (r.use_count() == 1) {
332-
reuse_record(tmp_r);
333-
} else {
334-
// Resuse a stale record
335-
create_record(tmp_r);
336-
}
337-
record_queue_pool_.push(record_queue_.front());
304+
// Add a new record to the pool or reuse a record from the pool
305+
if (ptr_pool_empty()) {
306+
SafeSharedBCFRec r = create_pool_ptr(bcf_dup(tmp_r.get()), bcf_destroy);
307+
bcf_unpack(r.get(), BCF_UN_ALL);
308+
record_queue_.emplace(std::move(r));
338309
} else {
339-
create_record(tmp_r);
310+
SafeSharedBCFRec r = reuse_pool_ptr();
311+
// Use `bcf_copy` to destroy (free) the stale data to prevent memory leaks
312+
bcf_copy(r.get(), tmp_r.get());
313+
bcf_unpack(r.get(), BCF_UN_ALL);
314+
record_queue_.emplace(std::move(r));
340315
}
341316
record_buffer_size += sizeof(bcf1_t) + record_queue_.front()->shared.m +
342317
record_queue_.front()->indiv.m;
@@ -351,12 +326,11 @@ void VCFV4::read_records() {
351326
}
352327

353328
void VCFV4::swap(VCFV4& other) {
354-
std::swap(mode_, other.mode_);
329+
SharedPtrPool<bcf1_t>::swap(other);
355330
std::swap(open_, other.open_);
356331
std::swap(path_, other.path_);
357332
std::swap(index_path_, other.index_path_);
358333
std::swap(record_queue_, other.record_queue_);
359-
std::swap(record_queue_pool_, other.record_queue_pool_);
360334
record_iter_.swap(other.record_iter_);
361335
std::swap(hdr_, other.hdr_);
362336
std::swap(index_tbx_, other.index_tbx_);

libtiledbvcf/src/vcf/vcf_v4.h

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <vector>
4343

4444
#include "utils/logger_public.h"
45+
#include "utils/shared_ptr_pool.h"
4546
#include "utils/utils.h"
4647
#include "vcf/htslib_value.h"
4748
#include "vcf/region.h"
@@ -53,12 +54,9 @@ namespace vcf {
5354
/**
5455
* Class wrapping a BCF/VCF file to allow iteration over records.
5556
*/
56-
class VCFV4 {
57+
class VCFV4 : public SharedPtrPool<bcf1_t> {
5758
public:
58-
enum SharingMode {
59-
MANUAL,
60-
AUTOMATIC,
61-
};
59+
typedef SharedPtrPool::SharingMode SharingMode;
6260

6361
/**
6462
* Constructor that determines how `SafeSharedBCFRec` records are managed. In
@@ -154,15 +152,6 @@ class VCFV4 {
154152
void set_max_record_buff_size(uint64_t max_record_buffer_size);
155153

156154
private:
157-
inline const std::string modeToString(SharingMode mode) {
158-
switch (mode) {
159-
case MANUAL:
160-
return "MANUAL";
161-
case AUTOMATIC:
162-
return "AUTOMATIC";
163-
}
164-
LOG_ERROR("VCFV4::modeToString {} is not a valid SharingMode", mode);
165-
}
166155
/** BCF/VCF iterator wrapper. */
167156
class Iter {
168157
public:
@@ -194,9 +183,6 @@ class VCFV4 {
194183
kstring_t tmps_ = {0, 0, nullptr};
195184
};
196185

197-
/** The mode used to manage the SafeSharedBCFRec pointers. */
198-
SharingMode mode_;
199-
200186
/** True if the file is open. */
201187
bool open_;
202188

@@ -212,9 +198,6 @@ class VCFV4 {
212198
/** The buffered records. */
213199
std::queue<SafeSharedBCFRec> record_queue_;
214200

215-
/** Stale records available for re-use in `record_queue_`. */
216-
std::queue<SafeSharedBCFRec> record_queue_pool_;
217-
218201
/** The BCF/TBX record iterator. */
219202
Iter record_iter_;
220203

@@ -230,21 +213,6 @@ class VCFV4 {
230213
/** The HTS index handle, if the index format is HTS. */
231214
hts_idx_t* index_hts_;
232215

233-
/**
234-
* Creates a new record and adds it to the `record_queue_`.
235-
*
236-
* @param tmp_r The record to wrap
237-
*/
238-
void create_record(SafeBCFRec& tmp_r);
239-
240-
/**
241-
* Pops the first record from `record_queue_pool_` and reuses it to add a
242-
* record to `record_queue_`.
243-
*
244-
* @param tmp_r The record to wrap
245-
*/
246-
void reuse_record(SafeBCFRec& tmp_r);
247-
248216
/** Reads records into the record buffer using `iter_`. */
249217
void read_records();
250218

0 commit comments

Comments
 (0)