← Back to Pull requests

[interop] Cache subclass checks #116

Open
guitargeek opened this pull request with 15 lines diff across 1 files
View on GitHub
guitargeek guitargeek commented on Sep 27, 2026 09:27 UTC
Author

Passing an object of a derived class to a formal base-class argument, as well as binding a method call whose 'self' is an instance of a class derived from the method's declaring class, makes the bindings check interop::IsSubclass() on every call, and the clang derivation check is not free.

Memoize IsSubclass() per class pair in the interop wrapper, where the calls are already serialized by the interop lock. A class that is still incomplete can gain bases once its definition is loaded, so a negative answer is only cached when both classes are complete.

For example, calling a C++ function that takes a base-class reference with a derived instance:

import time
import cppjit

cppjit.cppdef(r"""
struct SubBase {
virtual ~SubBase() = default;
virtual int value() const { return 1; }
};
struct SubDerived : SubBase {
int value() const override { return 2; }
};
int take_base(const SubBase& b) { return b.value(); }
""")

d = cppjit.gbl.SubDerived()
cppjit.gbl.take_base(d) # warm-up
start = time.perf_counter()
for _ in range(200000):
cppjit.gbl.take_base(d)
print(f"200k calls: {time.perf_counter() - start:.2f} s")

This takes 0.08 s instead of 0.22 s per 200k calls in a pristine build (0.18 s with only the parent commit's caches applied; ~2x further). Release build against the pinned CppInterOp, min of 7 repetitions, interleaved A/B. An instrumented build confirms the converter queries IsSubclass() once per call on this path, so the saved cost is the clang derivation check itself.

Diff: 15 lines (+14 / -1) across 1 files Reviewer: vgvassilev Review duration: 30986m
All Vouch review confidence checks passed
Residual risk 0.02 = Change Risk (0.20) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.20 · 15 lines diff across 1 files
0.20
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 3 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
90%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.166
Real PR Conversation Timeline
4 items
vgvassilev vgvassilev commented on code on Sep 27, 2026 09:39 UTC
Code Review
src/interop/interop_wrapper.cxx:1
@@ -1132,7 +1132,20 @@ interop::TCppScope_t interop::GetBaseScope(TCppScope_t klass,
 
 bool interop::IsSubclass(TCppScope_t derived, TCppScope_t base) {
   std::lock_guard<std::recursive_mutex> Lock(InterOpMutex);
-  return Cpp::IsSubclass(derived, base);
+  // Checked on every method call that receives 'self' as its first argument
+  // (e.g. from pythonizations and protocol slots), so memoize per class
+  // pair. A class that is still incomplete can gain bases once its
+  // definition is loaded, so a negative answer is only cached for complete
+  // classes.
+  static std::map<std::pair<const void*, const void*>, bool> s_subclass_cache;

The challenge with these cases is error recovery and code reloading/undo. I think this is fine but nominally we should have that in CppInterOp which also should know better when to invalidate these caches. Can we track this in an issue somewhere?

vgvassilev vgvassilev commented on code on Sep 27, 2026 09:40 UTC
Code Review
src/interop/interop_wrapper.cxx:1
@@ -1132,7 +1132,20 @@ interop::TCppScope_t interop::GetBaseScope(TCppScope_t klass,
 
 bool interop::IsSubclass(TCppScope_t derived, TCppScope_t base) {
   std::lock_guard<std::recursive_mutex> Lock(InterOpMutex);
-  return Cpp::IsSubclass(derived, base);
+  // Checked on every method call that receives 'self' as its first argument
+  // (e.g. from pythonizations and protocol slots), so memoize per class
+  // pair. A class that is still incomplete can gain bases once its
+  // definition is loaded, so a negative answer is only cached for complete
+  // classes.
+  static std::map<std::pair<const void*, const void*>, bool> s_subclass_cache;
+  const auto cacheKey = std::make_pair(derived.data, base.data);
+  auto cached = s_subclass_cache.find(cacheKey);
+  if (cached != s_subclass_cache.end())
+    return cached->second;
+  bool result = Cpp::IsSubclass(derived, base);
+  if (result || (Cpp::IsComplete(derived) && Cpp::IsComplete(base)))

Here IsComplete is only expensive the first time for ROOT then should be cheap. Is that not the case?

guitargeek guitargeek commented on Sep 27, 2026 09:53 UTC
Author

Thanks for the comments! I'll address them later. For now, what is also important to know is that the PRs I'm opening today all address significant performance regressions in the ROOT tutorials or CMS Combine workflows with the cppjit upgrade in ROOT.

Reviewers
Assignees
guitargeek guitargeek
Labels
risk: low confidence: 90% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 19.6% (LOW) · Higher = larger blast radius
19.6%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 90% · Lower = rushed or shallow review
90%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 2% · Safe to merge
2%