From: Scott Wolchok Date: Wed, 15 Sep 2021 16:55:02 +0000 (-0700) Subject: [PyTorch] remove string_view::operator[] bounds check (#64670) X-Git-Tag: accepted/tizen/8.0/unified/20231005.095509~195 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=54cdf651fd6432f1a33de2e2b2b1724ac2671cdd;p=platform%2Fupstream%2Fpytorch.git [PyTorch] remove string_view::operator[] bounds check (#64670) Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/64670 Bounds checking is not required for `std::string_view`, and the checking hoses performance for the following performance prototype diff. ghstack-source-id: 138037531 Test Plan: CI Reviewed By: ezyang, bhosmer Differential Revision: D30747515 fbshipit-source-id: 1f4374415a82dfdccce76ea2c6885c13cb93d369 --- diff --git a/c10/test/util/string_view_test.cpp b/c10/test/util/string_view_test.cpp index ee9f0dc..f63bd1e 100644 --- a/c10/test/util/string_view_test.cpp +++ b/c10/test/util/string_view_test.cpp @@ -177,34 +177,18 @@ static_assert('o' == hello.at(4), ""); TEST(StringViewTest, whenCallingAccessOperatorOutOfRange_thenThrows) { expectThrows( - [] { string_view("")[1]; }, - "string_view::operator[] or string_view::at() out of range. Index: 1, size: 0"); - - expectThrows( [] { string_view("").at(1); }, "string_view::operator[] or string_view::at() out of range. Index: 1, size: 0"); expectThrows( - [] { string_view("hello")[5]; }, - "string_view::operator[] or string_view::at() out of range. Index: 5, size: 5"); - - expectThrows( [] { string_view("hello").at(5); }, "string_view::operator[] or string_view::at() out of range. Index: 5, size: 5"); expectThrows( - [] { string_view("hello")[100]; }, - "string_view::operator[] or string_view::at() out of range. Index: 100, size: 5"); - - expectThrows( [] { string_view("hello").at(100); }, "string_view::operator[] or string_view::at() out of range. Index: 100, size: 5"); expectThrows( - [] { string_view("hello")[string_view::npos]; }, - "string_view::operator[] or string_view::at() out of range. Index: 18446744073709551615, size: 5"); - - expectThrows( [] { string_view("hello").at(string_view::npos); }, "string_view::operator[] or string_view::at() out of range. Index: 18446744073709551615, size: 5"); } diff --git a/c10/util/string_view.h b/c10/util/string_view.h index 9824002..4859c30 100644 --- a/c10/util/string_view.h +++ b/c10/util/string_view.h @@ -103,7 +103,8 @@ class basic_string_view final { } constexpr const_reference operator[](size_type pos) const { - return at(pos); + // TODO: split out + return at_(pos); } constexpr const_reference at(size_type pos) const {