Files
pytorch/torch/csrc/jit/interpreter.h
Peter Goldsborough 7a61306031 Enable all clang-tidy performance checks (#15198)
Summary:
This PR adds the final set of clang-tidy checks we should add for our codebase: a last set of performance-related checks. Most fixes here are around changing `auto` to `const auto&` in a few places where unnecessary copies were made, and adding `reserve()` calls before loops doing repeated `push_back()`. Also a few cases of calling `std::string::find` with a single-character string literal instead of a single char, which uses a less efficient string search algorithm meant for searching larger substrings.

![image](https://user-images.githubusercontent.com/6429851/49978940-adc1a780-ff01-11e8-99da-a4e431361f07.png)

ezyang apaszke
Pull Request resolved: https://github.com/pytorch/pytorch/pull/15198

Differential Revision: D13468797

Pulled By: goldsborough

fbshipit-source-id: 2bed1ea1c7c162b7f3e0e1026f17125e88c4d5b2
2018-12-14 13:32:47 -08:00

87 lines
2.1 KiB
C++

#pragma once
#include <memory>
#include <vector>
#include <c10/util/Optional.h>
#include <torch/csrc/jit/ivalue.h>
#include <torch/csrc/WindowsTorchApiMacro.h>
namespace at {
class Tensor;
}
namespace c10 {
struct IValue;
}
namespace torch { namespace jit {
// The interpreter run Graphs with Tensor inputs and Tensor outputs
// a separate component in the autograd handles unwrapping and wrapping
// variable objects for use in the interpreter.
struct Node;
struct GraphExecutor;
struct CodeImpl;
struct InterpreterStateImpl;
struct Graph;
struct Node;
using Stack = std::vector<c10::IValue>;
struct TORCH_API Code {
Code()
: pImpl(nullptr) {}
explicit Code(const std::shared_ptr<Graph>& graph);
~Code();
const std::vector<GraphExecutor*>& grad_executors();
explicit operator bool() const {
return pImpl != nullptr;
}
private:
std::shared_ptr<CodeImpl> pImpl;
friend struct InterpreterStateImpl;
friend std::ostream & operator<<(std::ostream & out, const Code & code);
};
struct InterpreterState {
InterpreterState(const Code & code);
void run(Stack& stack);
c10::intrusive_ptr<Future> runAsync(Stack& stack);
c10::intrusive_ptr<Future> getFuture();
~InterpreterState();
private:
InterpreterState(c10::intrusive_ptr<c10::intrusive_ptr_target> pImpl);
// Ideally we should use c10::intrusive_ptr<InterpreterStateImpl> for pImpl;
// but intrusive_ptr requires full definition of InterpreterStateImpl,
// which we need to hide in the header.
c10::intrusive_ptr<c10::intrusive_ptr_target> pImpl;
friend struct InterpreterStateImpl;
};
// Created by wait()
struct Suspend : public std::exception {
const char* what() const noexcept override {
return "Suspend";
}
explicit Suspend(c10::intrusive_ptr<Future> future_)
: future(std::move(future_)) {}
c10::intrusive_ptr<Future> future;
};
struct InterpreterContinuation {
InterpreterContinuation(InterpreterState state_, Stack stack_)
: state(state_), stack(std::move(stack_)) {}
void operator()() {
state.runAsync(stack);
}
private:
InterpreterState state;
Stack stack;
};
}}