[PyTorch] Use std::find in the JIT lexer (#64652)
authorScott Wolchok <swolchok@fb.com>
Fri, 10 Sep 2021 01:53:36 +0000 (18:53 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Fri, 10 Sep 2021 01:59:27 +0000 (18:59 -0700)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/64652

If nothing else, it is slightly clearer code.
ghstack-source-id: 137608456

Test Plan: CI

Reviewed By: dhruvbird

Differential Revision: D30739239

fbshipit-source-id: bc7917b59883ca4a33fc6916b4e422bad79cf04b

torch/csrc/jit/frontend/lexer.h

index 63041c3..c2bd32f 100644 (file)
@@ -337,16 +337,12 @@ struct TORCH_API SharedParserData {
       // rather the
       // identifier 'max'
       if (cur) {
-        size_t child_offset = 0;
-        for (size_t e = cur->child_chars.size(); child_offset < e;
-             ++child_offset) {
-          if (cur->child_chars[child_offset] == str[pos + i])
-            break;
-        }
+        const auto begin_it = cur->child_chars.begin();
+        const auto end_it = cur->child_chars.end();
+        const auto ch_it = std::find(begin_it, end_it, str[pos + i]);
 
-        cur = (child_offset == cur->child_chars.size())
-            ? nullptr
-            : cur->child_tries[child_offset].get();
+        cur = (ch_it == end_it) ? nullptr
+                                : cur->child_tries[ch_it - begin_it].get();
 
         if (cur && cur->kind != 0) {
           matched = true;