Skip to content

Commit

Permalink
[WASM] Add const in C++ where applicable (tensorflow#2324)
Browse files Browse the repository at this point in the history
DEV
  • Loading branch information
dsmilkov authored Nov 4, 2019
1 parent 6b11ae1 commit 5d8ac5f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
11 changes: 5 additions & 6 deletions tfjs-backend-wasm/src/cc/backend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ int xnn_operator_count = 0;
// Registers a disposal callback for a tensor id with a given callback function.
void register_disposal_callback(int tensor_id, DisposeFunction dispose_fn) {
if (disposal_callbacks.count(tensor_id) == 0) {
auto callbacks = std::vector<DisposeFunction>{dispose_fn};
// We move callbacks to avoid a copy.
disposal_callbacks.insert({tensor_id, std::move(callbacks)});
disposal_callbacks.insert({tensor_id, {dispose_fn}});
} else {
auto &callbacks = disposal_callbacks[tensor_id];
callbacks.push_back(dispose_fn);
Expand Down Expand Up @@ -84,8 +83,8 @@ void dispose_data(int tensor_id) {
// Call all disposal callbacks for this tensor id.
auto disposal_callback_idx = disposal_callbacks.find(tensor_id);
if (disposal_callback_idx != disposal_callbacks.end()) {
auto callbacks = disposal_callback_idx->second;
for (auto dispose_function : callbacks) {
auto &callbacks = disposal_callback_idx->second;
for (auto &dispose_function : callbacks) {
dispose_function(tensor_id);
}

Expand All @@ -100,10 +99,10 @@ void dispose() {
// We have to create a separate vector of tensor ids because we erase from the
// map while we're iterating it.
std::vector<int> tensor_ids_to_dispose;
for (auto const &element : data) {
for (const auto &element : data) {
tensor_ids_to_dispose.push_back(element.first);
}
for (auto const tensor_id : tensor_ids_to_dispose) {
for (const auto tensor_id : tensor_ids_to_dispose) {
dispose_data(tensor_id);
}

Expand Down
2 changes: 1 addition & 1 deletion tfjs-backend-wasm/src/cc/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace tfjs {
namespace util {

std::vector<int> compute_strides(const std::vector<int> shape) {
const std::vector<int> compute_strides(const std::vector<int> shape) {
int rank = shape.size();
std::vector<int> strides(rank - 1);
// Last dimension has implicit stride of 1, thus having D-1 (instead of D)
Expand Down
8 changes: 4 additions & 4 deletions tfjs-backend-wasm/src/cc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ inline void warn(const char* format, ...) {
template <class T>
inline void log_vector(const std::vector<T>& v) {
print_log("[", 0);
for (auto const& value : v) {
for (const auto& value : v) {
print_log("%d,", value);
}
print_log("]\n", 0);
Expand All @@ -83,8 +83,8 @@ inline int size_from_shape(const std::vector<int>& shape) {
}

// Returns the indices of an n-dim tensor given the flat offset and its strides.
inline std::vector<int> offset_to_loc(int index,
const std::vector<int>& strides) {
inline const std::vector<int> offset_to_loc(int index,
const std::vector<int>& strides) {
int rank = strides.size() + 1;
std::vector<int> loc(rank);
if (rank == 0) {
Expand Down Expand Up @@ -139,7 +139,7 @@ inline int offset(int i1, int i2, int i3, int i4, int i5, int s1, int s2,

// Returns the strides of a tensor given its shape. Note that the strides
// are of length R-1 where R is the rank of the tensor.
std::vector<int> compute_strides(const std::vector<int> shape);
const std::vector<int> compute_strides(const std::vector<int> shape);

} // namespace util
} // namespace tfjs
Expand Down

0 comments on commit 5d8ac5f

Please sign in to comment.