Skip to content

Commit

Permalink
avoid repeated allocations during xtc_write
Browse files Browse the repository at this point in the history
  • Loading branch information
stefdoerr committed Mar 26, 2024
1 parent 5bd2db9 commit 58b40ca
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions moleculekit/fileformats/xtc/src/xtc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ void xtc_read_new(char *filename, float *coords_arr, float *box_arr, float *time
int step;
float prec;
matrix box;
int nf3 = nframes * 3; // Precalculate 3 * nframes for the coordinate lookup macro
// int nf3 = nframes * 3; // Precalculate 3 * nframes for the coordinate lookup macro

if (!natoms)
{
Expand Down Expand Up @@ -415,7 +415,7 @@ int xtc_write(char *filename, int natoms, int nframes, int *step, float *timex,
unsigned long long int xidx, yidx, zidx;
matrix b;
float prec = 1000;
int nf3 = nframes * 3;
// int nf3 = nframes * 3;

// if( step <=0 ) {
// xd = xdrfile_open( filename, "w" );
Expand All @@ -436,10 +436,10 @@ int xtc_write(char *filename, int natoms, int nframes, int *step, float *timex,
return 1;
}

p = (rvec *)malloc(sizeof(rvec) * natoms * 3);

for (f = 0; f < nframes; f++)
{
p = (rvec *)malloc(sizeof(rvec) * natoms * 3);

b[0][0] = box[0 * nframes + f];
b[0][1] = 0.;
b[0][2] = 0.;
Expand All @@ -465,10 +465,11 @@ int xtc_write(char *filename, int natoms, int nframes, int *step, float *timex,
}
// printf("Frame: %d Step: %d Time: %f\n", f, step[f], timex[f]);
write_xtc(xd, natoms, (unsigned int)step[f], (float)timex[f], b, p, prec);

condfree((void *)p);
p = NULL;
}

condfree((void *)p);
p = NULL;

xdrfile_close(xd);

return 0;
Expand All @@ -488,9 +489,6 @@ void xtc_read_frame(char *filename, float *coords_arr, float *box_arr, float *ti

char index_file[PATH_MAX + 1];

int reading_index = 0;
int writing_index = 0;

if (frame < 0)
{
fprintf(stderr, "xtc_read_frame(): Frame <0\n");
Expand Down

0 comments on commit 58b40ca

Please sign in to comment.