I am working on a fasta file reader, and trying to find the best memory allocation strategy for it. Currently I am using the GeneralPurposeAllocator following the allocators guide since I don’t know if I really need a specialized one, but I am allocating anywhere between 30,000 bytes to upwards of 800MB for massive files.
Example code: (has some hand written string functions)
const Fasta = struct {
id:?[]u8,
strand:?[]u8,
length:u64,
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
var fasta:[]Fasta = allocator.alloc(Fasta, 1);
//omitting code for brevity
if (id)line {
if (first_id) {
fasta[i].id = try allocator.alloc(u8, file_line.len);
string_copy(fasta[i].id, &file_line, file_line.len);
}
else {
fasta = allocator.realloc(Fasta, i);
fasta[i].id = try allocator.alloc(u8, file_line.len);
string_copy(fasta[i].id, &file_line, file_line.len);
}
}
if (new_strand) {
fasta[i].length = file_line.len;
fasta[i].strand = allocator.alloc(u8, file_line.len);
string_copy(fasta[i].strand, &file_line, file_line.len);
}
if (current_strand) {
fasta[i].length += file_line.len;
allocator.realloc(u8, fasta[i].length);
concat(fasta[i].strand[fasta[i].length - file_line.len], &file_line);
}
So I am frequently calling alloc and realloc. In the worst case scenario I have, I allocate memory about 1,000,000 times. Is there a better allocator for this type of heap usage in terms of speed? I am willing to accept there isn’t (avoiding c_allocator), but I don’t know enough about page_allocator to really know if it could help.