From: Scott Wolchok Date: Wed, 15 Sep 2021 16:55:02 +0000 (-0700) Subject: [PyTorch] Avoid extra std::vector in parseSchemaOrName (#64678) X-Git-Tag: accepted/tizen/8.0/unified/20231005.095509~197 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=bdbc622988ad9abb22622235bf3f4ad7a9e7ea30;p=platform%2Fupstream%2Fpytorch.git [PyTorch] Avoid extra std::vector in parseSchemaOrName (#64678) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/64678 We know we only want one declaration, so let's not create an excess std::vector (and thus a heap allocation) for that. ghstack-source-id: 138036978 Test Plan: CI Reviewed By: dhruvbird, tugsbayasgalan Differential Revision: D30813785 fbshipit-source-id: c67e0100cdef5d894282939fb6d39a57309bc240 --- diff --git a/torch/csrc/jit/frontend/function_schema_parser.cpp b/torch/csrc/jit/frontend/function_schema_parser.cpp index 1656dda..927eae2 100644 --- a/torch/csrc/jit/frontend/function_schema_parser.cpp +++ b/torch/csrc/jit/frontend/function_schema_parser.cpp @@ -122,6 +122,13 @@ struct SchemaParser { return results; } + either parseExactlyOneDeclaration() { + auto result = parseDeclaration(); + L.nextIf(TK_NEWLINE); + L.expect(TK_EOF); + return result; + } + Argument parseArgument(size_t idx, bool is_return, bool kwarg_only) { auto p = type_parser.parseType(); auto type = std::move(p.first); @@ -319,7 +326,7 @@ struct SchemaParser { C10_EXPORT either parseSchemaOrName( const std::string& schemaOrName) { - return SchemaParser(schemaOrName).parseDeclarations().at(0); + return SchemaParser(schemaOrName).parseExactlyOneDeclaration(); } C10_EXPORT FunctionSchema parseSchema(const std::string& schema) {