[PyTorch] remove string_view::operator[] bounds check (#64670)
authorScott Wolchok <swolchok@fb.com>
Wed, 15 Sep 2021 16:55:02 +0000 (09:55 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Wed, 15 Sep 2021 16:57:58 +0000 (09:57 -0700)
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

c10/test/util/string_view_test.cpp
c10/util/string_view.h

index ee9f0dc..f63bd1e 100644 (file)
@@ -177,34 +177,18 @@ static_assert('o' == hello.at(4), "");
 
 TEST(StringViewTest, whenCallingAccessOperatorOutOfRange_thenThrows) {
   expectThrows<std::out_of_range>(
-      [] { string_view("")[1]; },
-      "string_view::operator[] or string_view::at() out of range. Index: 1, size: 0");
-
-  expectThrows<std::out_of_range>(
       [] { string_view("").at(1); },
       "string_view::operator[] or string_view::at() out of range. Index: 1, size: 0");
 
   expectThrows<std::out_of_range>(
-      [] { string_view("hello")[5]; },
-      "string_view::operator[] or string_view::at() out of range. Index: 5, size: 5");
-
-  expectThrows<std::out_of_range>(
       [] { string_view("hello").at(5); },
       "string_view::operator[] or string_view::at() out of range. Index: 5, size: 5");
 
   expectThrows<std::out_of_range>(
-      [] { string_view("hello")[100]; },
-      "string_view::operator[] or string_view::at() out of range. Index: 100, size: 5");
-
-  expectThrows<std::out_of_range>(
       [] { string_view("hello").at(100); },
       "string_view::operator[] or string_view::at() out of range. Index: 100, size: 5");
 
   expectThrows<std::out_of_range>(
-      [] { string_view("hello")[string_view::npos]; },
-      "string_view::operator[] or string_view::at() out of range. Index: 18446744073709551615, size: 5");
-
-  expectThrows<std::out_of_range>(
       [] { string_view("hello").at(string_view::npos); },
       "string_view::operator[] or string_view::at() out of range. Index: 18446744073709551615, size: 5");
 }
index 9824002..4859c30 100644 (file)
@@ -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 {