Fix memory usage when loading a model + some other minor fixes to avoid unnecessary heap allocations. (#4318)

This commit is contained in:
Pranav Sharma 2020-06-24 00:23:11 -07:00 committed by GitHub
parent 5dd3ebb3b1
commit 44f06ec480
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 6 deletions

View file

@ -15,7 +15,7 @@ inline bool AreIntervalsOverlap(int start1, int end1, int start2, int end2) {
template <typename T>
inline bool AreVectorsOverlap(const std::vector<T>& v1, const std::vector<T>& v2) {
for (T type : v1) {
for (const T& type : v1) {
if (std::find(v2.begin(), v2.end(), type) != v2.end()) {
return true;
}
@ -44,7 +44,7 @@ bool KernelDef::IsConflict(const KernelDef& other) const {
//only one case they don't conflict:
//There is a type_constraint, it exists in both hands, but they don't overlap
//check types
auto other_types = other.TypeConstraints();
const auto& other_types = other.TypeConstraints();
bool type_has_conflict = true;
for (const auto& it : type_constraints_) {
auto iter = other_types.find(it.first);

View file

@ -26,8 +26,6 @@ using namespace ONNX_NAMESPACE;
using namespace onnxruntime;
using namespace onnxruntime::common;
static constexpr int protobuf_block_size_in_bytes = 4 * 1024 * 1024;
namespace onnxruntime {
Model::Model(const std::string& graph_name,
bool is_onnx_domain_only,
@ -239,7 +237,7 @@ Status Model::Load(std::istream& model_istream, ModelProto* p_model_proto) {
if (!p_model_proto) {
return Status(ONNXRUNTIME, INVALID_ARGUMENT, "Null model_proto ptr.");
}
google::protobuf::io::IstreamInputStream zero_copy_input(&model_istream, protobuf_block_size_in_bytes);
google::protobuf::io::IstreamInputStream zero_copy_input(&model_istream);
const bool result = p_model_proto->ParseFromZeroCopyStream(&zero_copy_input) && model_istream.eof();
if (!result) {
return Status(ONNXRUNTIME, INVALID_PROTOBUF, "Failed to load model because protobuf parsing failed.");
@ -449,7 +447,7 @@ Status Model::Load(int fd, ONNX_NAMESPACE::ModelProto& model_proto) {
}
#if GOOGLE_PROTOBUF_VERSION >= 3002000
FileInputStream input(fd, protobuf_block_size_in_bytes);
FileInputStream input(fd);
const bool result = model_proto.ParseFromZeroCopyStream(&input) && input.GetErrno() == 0;
if (!result) {
return Status(ONNXRUNTIME, INVALID_PROTOBUF, "Protobuf parsing failed.");