Skip to content

Commit db3be06

Browse files
committed
Vastly improved performance of large catalog loading (x50 faster)
1 parent 21a325f commit db3be06

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

src/fast++-read_input.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -456,8 +456,31 @@ bool read_fluxes(const options_t& opts, input_state_t& state) {
456456
return false;
457457
}
458458

459+
if (opts.verbose) {
460+
note("reading fluxes...");
461+
}
462+
463+
// Read all lines to determine the number of galaxies
464+
uint_t ngal = 0; {
465+
std::ifstream in(catalog_file);
466+
std::string line;
467+
while (std::getline(in, line)) {
468+
line = trim(line);
469+
if (line.empty() || line[0] == '#') continue;
470+
471+
++ngal;
472+
}
473+
}
474+
475+
// Resize arrays now to avoid reallocation later
476+
state.id.resize(ngal);
477+
state.zspec = replicate(fnan, ngal);
478+
state.flux = replicate(fnan, ngal, state.no_filt.size());
479+
state.eflux = replicate(fnan, ngal, state.no_filt.size());
480+
459481
// Now read the catalog itself, only keeping the columns we are interested in
460482
uint_t l = 0;
483+
uint_t gid = 0;
461484
std::ifstream in(catalog_file);
462485
std::string line;
463486
while (std::getline(in, line)) {
@@ -468,7 +491,7 @@ bool read_fluxes(const options_t& opts, input_state_t& state) {
468491
vec1s spl = split_any_of(line, " \t\n\r");
469492

470493
// Read the ID
471-
state.id.push_back(spl[col_id]);
494+
state.id.safe[gid] = spl[col_id];
472495

473496
// Read the zspec if any
474497
if (col_zspec != npos) {
@@ -484,7 +507,7 @@ bool read_fluxes(const options_t& opts, input_state_t& state) {
484507
tz = fnan;
485508
}
486509

487-
state.zspec.push_back(tz);
510+
state.zspec.safe[gid] = tz;
488511
}
489512

490513
// Read the fluxes and uncertainties
@@ -524,17 +547,16 @@ bool read_fluxes(const options_t& opts, input_state_t& state) {
524547

525548
// Flag bad values
526549
vec1u idb = where(err < 0 || !is_finite(flx) || !is_finite(err));
527-
err[idb] = finf; flx[idb] = 0;
550+
err.safe[idb] = finf; flx.safe[idb] = 0;
528551

529552
// Save flux and uncertainties in the input state
530-
append<0>(state.flux, reform(flx, 1, flx.size()));
531-
append<0>(state.eflux, reform(err, 1, err.size()));
553+
state.flux.safe(gid,_) = flx;
554+
state.eflux.safe(gid,_) = err;
555+
556+
++gid;
532557
}
533558

534-
if (col_zspec == npos) {
535-
// If no zspec column, give no zspec to all galaxies
536-
state.zspec = replicate(fnan, state.id.size());
537-
} else {
559+
if (col_zspec != npos) {
538560
// Check that zspecs are covered by the redshift grid
539561
if (min(state.zspec) < opts.z_min) {
540562
error("the smallest z_spec is outside of the grid (", min(state.zspec),

0 commit comments

Comments
 (0)