[gccrs COMMIT 1/5] gccrs: method resolution try predicate items first

gerris.rs@gmail.com gerris.rs@gmail.com
Sun Sep 6 19:15:23 GMT 2026


From: Philip Herron <herron.philip@googlemail.com>

This is part 1 of more series of patches to improve method resolution perf
because its incredibly expensive operation. This changes it so that we try
predicates up front before looking up anything.

gcc/rust/ChangeLog:

	* typecheck/rust-hir-dot-operator.cc (MethodResolver::select): look at predicates first

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: https://github.com/Rust-GCC/gccrs/commit/508a080f107cd1196cdf2e0aa7ede811ef2c29a1

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4847

 gcc/rust/typecheck/rust-hir-dot-operator.cc | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/typecheck/rust-hir-dot-operator.cc b/gcc/rust/typecheck/rust-hir-dot-operator.cc
index 227e5dce1..e42c4b90a 100644
--- a/gcc/rust/typecheck/rust-hir-dot-operator.cc
+++ b/gcc/rust/typecheck/rust-hir-dot-operator.cc
@@ -476,6 +476,11 @@ MethodResolver::select (TyTy::BaseType &receiver)
 	      receiver.debug_str ().c_str (),
 	      segment_name.to_string ().c_str ());
 
+  // Predicate candidates have the highest priority.  Try them before
+  // assembling impl candidates, which can trigger expensive trait resolution.
+  if (try_select_predicate_candidates (receiver))
+    return true;
+
   // Assemble candidates
   std::vector<impl_item_candidate> inherent_impl_fns;
   if (specified_trait == nullptr)
@@ -498,19 +503,15 @@ MethodResolver::select (TyTy::BaseType &receiver)
 
   // Try selection in the priority order defined by Rust's method resolution:
 
-  // 1. Try predicate candidates first (highest priority)
-  if (try_select_predicate_candidates (receiver))
-    return true;
-
-  // 2. Try inherent impl functions (non-trait impl blocks)
+  // 1. Try inherent impl functions (non-trait impl blocks)
   if (try_select_inherent_impl_candidates (receiver, inherent_impl_fns, false))
     return true;
 
-  // 3. Try inherent impl functions from trait impl blocks
+  // 2. Try inherent impl functions from trait impl blocks
   if (try_select_inherent_impl_candidates (receiver, inherent_impl_fns, true))
     return true;
 
-  // 4. Try trait functions (lowest priority)
+  // 3. Try trait functions (lowest priority)
   return try_select_trait_impl_candidates (receiver, trait_fns);
 }
 

base-commit: 8c50dd1b28bc7856b83a51d9d1a985aa76ec184e
-- 
2.55.0



More information about the Gcc-rust mailing list