Skip to content

Commit 94d3205

Browse files
committed
Vastly improved performance of large catalog loading (x50 faster)
1 parent 79e88b3 commit 94d3205

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
@@ -648,8 +648,31 @@ bool read_fluxes(const options_t& opts, input_state_t& state) {
648648
return false;
649649
}
650650

651+
if (opts.verbose) {
652+
note("reading fluxes...");
653+
}
654+
655+
// Read all lines to determine the number of galaxies
656+
uint_t ngal = 0; {
657+
std::ifstream in(catalog_file);
658+
std::string line;
659+
while (std::getline(in, line)) {
660+
line = trim(line);
661+
if (line.empty() || line[0] == '#') continue;
662+
663+
++ngal;
664+
}
665+
}
666+
667+
// Resize arrays now to avoid reallocation later
668+
state.id.resize(ngal);
669+
state.zspec = replicate(fnan, ngal);
670+
state.flux = replicate(fnan, ngal, state.no_filt.size());
671+
state.eflux = replicate(fnan, ngal, state.no_filt.size());
672+
651673
// Now read the catalog itself, only keeping the columns we are interested in
652674
uint_t l = 0;
675+
uint_t gid = 0;
653676
std::ifstream in(catalog_file);
654677
std::string line;
655678
while (std::getline(in, line)) {
@@ -665,7 +688,7 @@ bool read_fluxes(const options_t& opts, input_state_t& state) {
665688
}
666689

667690
// Read the ID
668-
state.id.push_back(spl[col_id]);
691+
state.id.safe[gid] = spl[col_id];
669692

670693
// Read the zspec if any
671694
if (col_zspec != npos) {
@@ -681,7 +704,7 @@ bool read_fluxes(const options_t& opts, input_state_t& state) {
681704
tz = fnan;
682705
}
683706

684-
state.zspec.push_back(tz);
707+
state.zspec.safe[gid] = tz;
685708
}
686709

687710
// Read the fluxes and uncertainties
@@ -721,17 +744,16 @@ bool read_fluxes(const options_t& opts, input_state_t& state) {
721744

722745
// Flag bad values
723746
vec1u idb = where(err < 0 || !is_finite(flx) || !is_finite(err));
724-
err[idb] = finf; flx[idb] = 0;
747+
err.safe[idb] = finf; flx.safe[idb] = 0;
725748

726749
// Save flux and uncertainties in the input state
727-
append<0>(state.flux, reform(flx, 1, flx.size()));
728-
append<0>(state.eflux, reform(err, 1, err.size()));
750+
state.flux.safe(gid,_) = flx;
751+
state.eflux.safe(gid,_) = err;
752+
753+
++gid;
729754
}
730755

731-
if (col_zspec == npos) {
732-
// If no zspec column, give no zspec to all galaxies
733-
state.zspec = replicate(fnan, state.id.size());
734-
} else {
756+
if (col_zspec != npos) {
735757
// Check that zspecs are covered by the redshift grid
736758
if (min(state.zspec) < opts.z_min) {
737759
error("the smallest z_spec is outside of the grid (", min(state.zspec),

0 commit comments

Comments
 (0)