Skip to content

Commit

Permalink
Only create extractors on first row/col request.
Browse files Browse the repository at this point in the history
This avoids the overhead of extractor creation when the NumericMatrix is not
going to be queried from JS, e.g., because it's an intermediate. The downside
is that the row/col getters are no longer thread-safe, but they can only
be called from JS and that should be single-threaded anyway.
  • Loading branch information
LTLA committed Jul 22, 2023
1 parent 1613573 commit 57ddaf8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/NumericMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

NumericMatrix::NumericMatrix() {}

NumericMatrix::NumericMatrix(const tatami::NumericMatrix* p) : NumericMatrix(std::shared_ptr<const tatami::NumericMatrix>(p)) {}
NumericMatrix::NumericMatrix(const tatami::NumericMatrix* p) : ptr(p) {}

NumericMatrix::NumericMatrix(std::shared_ptr<const tatami::NumericMatrix> p) : ptr(std::move(p)), by_row(ptr->dense_row()), by_column(ptr->dense_column()) {}
NumericMatrix::NumericMatrix(std::shared_ptr<const tatami::NumericMatrix> p) : ptr(std::move(p)) {}

template<class Vector_>
tatami::NumericMatrix* create_NumericMatrix(int nr, int nc, Vector_ vec, bool colmajor) {
Expand All @@ -18,8 +18,8 @@ tatami::NumericMatrix* create_NumericMatrix(int nr, int nc, Vector_ vec, bool co

void NumericMatrix::reset_ptr(std::shared_ptr<const tatami::NumericMatrix> p) {
ptr = std::move(p);
by_row = ptr->dense_row();
by_column = ptr->dense_column();
by_row.reset();
by_column.reset();
}

NumericMatrix::NumericMatrix(int nr, int nc, uintptr_t values, bool colmajor, bool copy) {
Expand All @@ -40,14 +40,20 @@ int NumericMatrix::ncol() const {
return ptr->ncol();
}

void NumericMatrix::row(int r, uintptr_t values) const {
void NumericMatrix::row(int r, uintptr_t values) {
double* buffer = reinterpret_cast<double*>(values);
if (!by_row) {
by_row = ptr->dense_row();
}
by_row->fetch_copy(r, buffer);
return;
}

void NumericMatrix::column(int c, uintptr_t values) const {
void NumericMatrix::column(int c, uintptr_t values) {
double* buffer = reinterpret_cast<double*>(values);
if (!by_column) {
by_column = ptr->dense_column();
}
by_column->fetch_copy(c, buffer);
return;
}
Expand Down
11 changes: 7 additions & 4 deletions src/NumericMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ struct NumericMatrix {
int ncol() const;

public:
void row(int r, uintptr_t values) const;

void column(int c, uintptr_t values) const;

bool sparse() const;

NumericMatrix clone() const;

// Not thread-safe! by_row and by_column are initialized
// on demand when particular rows and columns are requested
// in Javascript. Don't use these functions from C++.
void row(int r, uintptr_t values);

void column(int c, uintptr_t values);

public:
std::shared_ptr<const tatami::NumericMatrix> ptr;

Expand Down

0 comments on commit 57ddaf8

Please sign in to comment.