Skip to content

Commit 000cfeb

Browse files
swolchokpytorchmergebot
authored andcommitted
[PyTorch] Optimize TupleType::annotation_str_impl (pytorch#96497)
stringstream is expensive to create, we used stringstream instead of ostringstream, and we can easily specialize the empty tuple. Also, anybody compiling with C++20 support can move out of the stringstream and it shouldn't hurt people without C++20 support to do so. I would consider specializing the 1-element case as well but I don't have evidence that that's necessary right now. Differential Revision: [D43882402](https://our.internmc.facebook.com/intern/diff/D43882402/) Pull Request resolved: pytorch#96497 Approved by: https://github.com/Skylion007
1 parent 33dfded commit 000cfeb

File tree

2 files changed

+26
-17
lines changed

2 files changed

+26
-17
lines changed

aten/src/ATen/core/type.cpp

+20-17
Original file line numberDiff line numberDiff line change
@@ -887,26 +887,29 @@ std::string TupleType::str() const {
887887
return ss.str();
888888
}
889889
std::string TupleType::annotation_str_impl(TypePrinter printer) const {
890-
std::stringstream ss;
891890
if (schema_ && name()) {
892-
ss << name()->qualifiedName();
893-
} else {
894-
ss << "Tuple[";
895-
if (elements().empty()) {
896-
// `typing.Tuple` special-cases the annotation syntax for empty tuple
897-
// with `typing.Tuple[()]`. See
898-
// https://docs.python.org/3/library/typing.html#typing.Tuple
899-
ss << "()";
900-
} else {
901-
for (size_t i = 0; i < elements().size(); ++i) {
902-
if (i > 0)
903-
ss << ", ";
904-
ss << elements()[i]->annotation_str(printer);
905-
}
891+
return name()->qualifiedName();
892+
}
893+
894+
if (elements().empty()) {
895+
// `typing.Tuple` special-cases the annotation syntax for empty tuple
896+
// with `typing.Tuple[()]`. See
897+
// https://docs.python.org/3/library/typing.html#typing.Tuple
898+
return "Tuple[()]";
899+
}
900+
901+
std::ostringstream ss;
902+
ss << "Tuple[";
903+
size_t i = 0;
904+
for (const auto& element: elements()) {
905+
if (i > 0) {
906+
ss << ", ";
906907
}
907-
ss << "]";
908+
ss << element->annotation_str(printer);
909+
i++;
908910
}
909-
return ss.str();
911+
ss << ']';
912+
return std::move(ss).str();
910913
}
911914

912915
InterfaceTypePtr InterfaceType::create(QualifiedName qualifiedName, bool is_module) {

test/test_jit.py

+6
Original file line numberDiff line numberDiff line change
@@ -11522,6 +11522,12 @@ def test_empty_tuple_str(self):
1152211522
python_type = eval(empty_tuple_type.annotation_str, g)
1152311523
assert python_type is typing.Tuple[()]
1152411524

11525+
def test_tuple_str(self):
11526+
tuple1_type = torch._C.TupleType([torch._C.StringType.get()])
11527+
self.assertEqual(tuple1_type.annotation_str, "Tuple[str]")
11528+
tuple2_type = torch._C.TupleType([torch._C.StringType.get(), torch._C.StringType.get()])
11529+
self.assertEqual(tuple2_type.annotation_str, "Tuple[str, str]")
11530+
1152511531
def test_none_type_str(self):
1152611532
none_type = torch._C.NoneType.get()
1152711533
g = {'NoneType' : type(None)}

0 commit comments

Comments
 (0)