Implement helper for a thread-safe shared cache of weakly-owned values [chromium/src : main]

0 views
Skip to first unread message

Daniel Cheng (Gerrit)

unread,
Apr 14, 2026, 2:32:10 PM (4 days ago) Apr 14
to Daniel Cheng, Ben Wagner, android-bu...@system.gserviceaccount.com, Hans Wennborg, Heron Yang, Mangesh Ghiware, Hiroki Nakagawa, Hu, Ningxin, Jiewei Qian, Jeremy Roman, Nico Weber, David Benjamin, chromiu...@luci-project-accounts.iam.gserviceaccount.com, chromium...@chromium.org, net-r...@chromium.org, blink-re...@chromium.org, blink-rev...@chromium.org, blink-revi...@chromium.org, blink-rev...@chromium.org, blink-re...@chromium.org, kinuko...@chromium.org, mac-r...@chromium.org, tommyw+w...@chromium.org, video-networking...@google.com, zol...@webkit.org, blink-revi...@chromium.org, blink-...@chromium.org, feature-me...@chromium.org, gavinwill+sc...@chromium.org, gcasto+w...@chromium.org, halliwe...@chromium.org, horo+...@chromium.org, ipc-securi...@chromium.org, kinuko+ser...@chromium.org, oshima...@chromium.org, servicewor...@chromium.org, shimazu+se...@chromium.org, toyosh...@chromium.org, vasilii+watchlis...@chromium.org
Attention needed from Ben Wagner, David Benjamin, Jeremy Roman and Nico Weber

Daniel Cheng voted and added 1 comment

Votes added by Daniel Cheng

Commit-Queue+1

1 comment

Patchset-level comments
File-level comment, Patchset 12:
Ben Wagner . unresolved

While I would prefer to just allow the proper building blocks and land https://chromium-review.googlesource.com/c/chromium/src/+/7694752 instead, this weak value map is fine as well since weak map values are what std::weak_ptr is generally used for anyway. Also b/492374380 is a real issue that should be fixed.

Daniel Cheng

Yes, definitely understood. FWIW I did chat to C++ style owners, and the general consensus was that it didn't seem terrible to allow `std::weak_ptr` (and `std::shared_ptr` for these kinds of use cases), but with the caveat that having both refcounting systems in common use didn't seem ideal.

I do think there's a future where we might end up standardizing on `std::shared_ptr`, but I think that's some distance away. I've added a bunch of comments, added an additional test, and restore a trace event (that got moved to the `MappedFontFile` dtor); PTAL.

Open in Gerrit

Related details

Attention is currently required from:
  • Ben Wagner
  • David Benjamin
  • Jeremy Roman
  • Nico Weber
Submit Requirements:
  • requirement satisfiedCode-Coverage
  • requirement satisfiedCode-Owners
  • requirement is not satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: chromium/src
Gerrit-Branch: main
Gerrit-Change-Id: If903b247109e11be2fb6440297a59e78d20d4ac7
Gerrit-Change-Number: 7706875
Gerrit-PatchSet: 13
Gerrit-Owner: Daniel Cheng <dch...@chromium.org>
Gerrit-Reviewer: Ben Wagner <bung...@google.com>
Gerrit-Reviewer: Daniel Cheng <dch...@chromium.org>
Gerrit-Reviewer: David Benjamin <davi...@chromium.org>
Gerrit-Reviewer: Jeremy Roman <jbr...@chromium.org>
Gerrit-Reviewer: Nico Weber <tha...@chromium.org>
Gerrit-CC: Hans Wennborg <ha...@chromium.org>
Gerrit-CC: Heron Yang <hero...@google.com>
Gerrit-CC: Hiroki Nakagawa <nhi...@chromium.org>
Gerrit-CC: Hu, Ningxin <ningx...@intel.com>
Gerrit-CC: Jiewei Qian <q...@chromium.org>
Gerrit-CC: Mangesh Ghiware <mghi...@google.com>
Gerrit-Attention: Nico Weber <tha...@chromium.org>
Gerrit-Attention: Jeremy Roman <jbr...@chromium.org>
Gerrit-Attention: David Benjamin <davi...@chromium.org>
Gerrit-Attention: Ben Wagner <bung...@google.com>
Gerrit-Comment-Date: Tue, 14 Apr 2026 18:32:02 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Comment-In-Reply-To: Ben Wagner <bung...@google.com>
satisfied_requirement
unsatisfied_requirement
open
diffy

Daniel Cheng (Gerrit)

unread,
Apr 17, 2026, 1:58:31 PM (yesterday) Apr 17
to Daniel Cheng, Sean Maher, Ben Wagner, android-bu...@system.gserviceaccount.com, Hans Wennborg, Heron Yang, Mangesh Ghiware, Hiroki Nakagawa, Hu, Ningxin, Jiewei Qian, Jeremy Roman, Nico Weber, David Benjamin, Chromium LUCI CQ, chromium...@chromium.org, net-r...@chromium.org, blink-re...@chromium.org, blink-rev...@chromium.org, blink-revi...@chromium.org, blink-rev...@chromium.org, blink-re...@chromium.org, kinuko...@chromium.org, mac-r...@chromium.org, tommyw+w...@chromium.org, video-networking...@google.com, zol...@webkit.org, blink-revi...@chromium.org, blink-...@chromium.org, feature-me...@chromium.org, gavinwill+sc...@chromium.org, gcasto+w...@chromium.org, halliwe...@chromium.org, horo+...@chromium.org, ipc-securi...@chromium.org, kinuko+ser...@chromium.org, oshima...@chromium.org, servicewor...@chromium.org, shimazu+se...@chromium.org, toyosh...@chromium.org, vasilii+watchlis...@chromium.org
Attention needed from Ben Wagner, David Benjamin, Jeremy Roman, Nico Weber and Sean Maher

Daniel Cheng added 1 comment

Patchset-level comments
File-level comment, Patchset 15 (Latest):
Daniel Cheng . resolved

@spvm who volunteered to help review the atomic bits

Open in Gerrit

Related details

Attention is currently required from:
  • Ben Wagner
  • David Benjamin
  • Jeremy Roman
  • Nico Weber
  • Sean Maher
Submit Requirements:
  • requirement satisfiedCode-Coverage
  • requirement satisfiedCode-Owners
  • requirement is not satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: chromium/src
Gerrit-Branch: main
Gerrit-Change-Id: If903b247109e11be2fb6440297a59e78d20d4ac7
Gerrit-Change-Number: 7706875
Gerrit-PatchSet: 15
Gerrit-Owner: Daniel Cheng <dch...@chromium.org>
Gerrit-Reviewer: Ben Wagner <bung...@google.com>
Gerrit-Reviewer: Daniel Cheng <dch...@chromium.org>
Gerrit-Reviewer: David Benjamin <davi...@chromium.org>
Gerrit-Reviewer: Jeremy Roman <jbr...@chromium.org>
Gerrit-Reviewer: Nico Weber <tha...@chromium.org>
Gerrit-Reviewer: Sean Maher <sp...@chromium.org>
Gerrit-CC: Hans Wennborg <ha...@chromium.org>
Gerrit-CC: Heron Yang <hero...@google.com>
Gerrit-CC: Hiroki Nakagawa <nhi...@chromium.org>
Gerrit-CC: Hu, Ningxin <ningx...@intel.com>
Gerrit-CC: Jiewei Qian <q...@chromium.org>
Gerrit-CC: Mangesh Ghiware <mghi...@google.com>
Gerrit-Attention: Nico Weber <tha...@chromium.org>
Gerrit-Attention: Jeremy Roman <jbr...@chromium.org>
Gerrit-Attention: David Benjamin <davi...@chromium.org>
Gerrit-Attention: Sean Maher <sp...@chromium.org>
Gerrit-Attention: Ben Wagner <bung...@google.com>
Gerrit-Comment-Date: Fri, 17 Apr 2026 17:58:19 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
satisfied_requirement
unsatisfied_requirement
open
diffy

Sean Maher (Gerrit)

unread,
11:34 AM (9 hours ago) 11:34 AM
to Daniel Cheng, Ben Wagner, android-bu...@system.gserviceaccount.com, Hans Wennborg, Heron Yang, Mangesh Ghiware, Hiroki Nakagawa, Hu, Ningxin, Jiewei Qian, Jeremy Roman, Nico Weber, David Benjamin, Chromium LUCI CQ, chromium...@chromium.org, net-r...@chromium.org, blink-re...@chromium.org, blink-rev...@chromium.org, blink-revi...@chromium.org, blink-rev...@chromium.org, blink-re...@chromium.org, kinuko...@chromium.org, mac-r...@chromium.org, tommyw+w...@chromium.org, video-networking...@google.com, zol...@webkit.org, blink-revi...@chromium.org, blink-...@chromium.org, feature-me...@chromium.org, gavinwill+sc...@chromium.org, gcasto+w...@chromium.org, halliwe...@chromium.org, horo+...@chromium.org, ipc-securi...@chromium.org, kinuko+ser...@chromium.org, oshima...@chromium.org, servicewor...@chromium.org, shimazu+se...@chromium.org, toyosh...@chromium.org, vasilii+watchlis...@chromium.org
Attention needed from Ben Wagner, Daniel Cheng, David Benjamin, Jeremy Roman and Nico Weber

Sean Maher added 5 comments

Patchset-level comments
Sean Maher . resolved

Some comments on the atomic impl. Let me know if I've misread anything or have mistaken assumptions about how it should work! I'm not 100% that I've completely internalized the API yet.

File base/containers/weak_value_table.h
Line 142, Patchset 15 (Latest): return scoped_refptr<ValueT>(it->second.get());
Sean Maher . unresolved

Why doesn't this path `AddRefChecked(): { if (fetch_add(count) != 0) { return } }` before returning it, guaranteeing that the returned value won't hit the CHECK in AddRef() and will stay alive for at least a little while?

Line 59, Patchset 15 (Latest): CHECK(count_.fetch_add(1, std::memory_order_relaxed) != 0);
Sean Maher . unresolved

this CHECK() could occur. in fact, it occurs whenever an AddRef() occurs in just before ReleaseAndRemovIfUnused() in Release(), no?

Line 59, Patchset 15 (Latest): CHECK(count_.fetch_add(1, std::memory_order_relaxed) != 0);
Sean Maher . unresolved

can you talk about why you believe that it makes sense to have memory_order_relaxed here? I'm not sure that I've yet figured out a case that it could cause problems yet, but I'm not convinced that it's necessarily the right idea given that the compiler could plausibly reorder it into locked operations on `table_` (though acq/rel wouldn't stop the compiler from doing that given that `table_` isn't operating on it.

To tie into my other comment on FindOrCreate(): it could plausibly reorder it before other operations in the Callable() in there that, while I can't imagine actually posing a problem, could maybe pose a problem if you engineered it to.

To my understanding according to the spec, the table lock and the atomic can be mutually reordered to the compiler's and CPU's whims. it feels like you're assuming that they can't.

Line 21, Patchset 15 (Latest):// supported operation is `FindOrInsert()`, which either:
Sean Maher . unresolved

FindOrCreate?

Open in Gerrit

Related details

Attention is currently required from:
  • Ben Wagner
  • Daniel Cheng
  • David Benjamin
  • Jeremy Roman
  • Nico Weber
Gerrit-Attention: Ben Wagner <bung...@google.com>
Gerrit-Attention: Daniel Cheng <dch...@chromium.org>
Gerrit-Comment-Date: Sat, 18 Apr 2026 15:34:06 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
satisfied_requirement
unsatisfied_requirement
open
diffy

Daniel Cheng (Gerrit)

unread,
5:19 PM (4 hours ago) 5:19 PM
to Daniel Cheng, Sean Maher, Ben Wagner, android-bu...@system.gserviceaccount.com, Hans Wennborg, Heron Yang, Mangesh Ghiware, Hiroki Nakagawa, Hu, Ningxin, Jiewei Qian, Jeremy Roman, Nico Weber, David Benjamin, Chromium LUCI CQ, chromium...@chromium.org, net-r...@chromium.org, blink-re...@chromium.org, blink-rev...@chromium.org, blink-revi...@chromium.org, blink-rev...@chromium.org, blink-re...@chromium.org, kinuko...@chromium.org, mac-r...@chromium.org, tommyw+w...@chromium.org, video-networking...@google.com, zol...@webkit.org, blink-revi...@chromium.org, blink-...@chromium.org, feature-me...@chromium.org, gavinwill+sc...@chromium.org, gcasto+w...@chromium.org, halliwe...@chromium.org, horo+...@chromium.org, ipc-securi...@chromium.org, kinuko+ser...@chromium.org, oshima...@chromium.org, servicewor...@chromium.org, shimazu+se...@chromium.org, toyosh...@chromium.org, vasilii+watchlis...@chromium.org
Attention needed from Ben Wagner, David Benjamin, Jeremy Roman, Nico Weber and Sean Maher

Daniel Cheng added 4 comments

File base/containers/weak_value_table.h
Line 142, Patchset 15: return scoped_refptr<ValueT>(it->second.get());
Sean Maher . unresolved

Why doesn't this path `AddRefChecked(): { if (fetch_add(count) != 0) { return } }` before returning it, guaranteeing that the returned value won't hit the CHECK in AddRef() and will stay alive for at least a little while?

Daniel Cheng

Can you clarify how this would hit the `CHECK()`?

If we acquire the lock, no thread is currently in the critical section of `ReleaseAndRemoveIfUnused()`, which should mean there are no entries with a refcount of zero in the table.

Line 59, Patchset 15: CHECK(count_.fetch_add(1, std::memory_order_relaxed) != 0);
Sean Maher . unresolved

this CHECK() could occur. in fact, it occurs whenever an AddRef() occurs in just before ReleaseAndRemovIfUnused() in Release(), no?

Daniel Cheng

It can't because a precondition of entering `ReleaseAndRemoveIfUnused()` is the caller thinks the refcount is potentially 1 (but definitely not zero). At that point, only two scenarios are possible:
1. `ReleaseAndRemoveIfUnused()` wins the race and the entry is removed from the table. Nothing else can possible reference it, because the caller to `ReleaseAndRemoveIfUnused()` had the sole refcount, so the refcount cannot increase from zero.

2. `ReleaseAndRemoveIfUnused()` loses the race and `AddRef()` occurs first because someone else looked up the entry and obtained a ref to it. In this case, the refcount never goes to 0: it was at 1, became 2, and then returns to 1 when `ReleaseAndRemoveIfUnused()`'s `fetch_sub()` executes.

Line 59, Patchset 15: CHECK(count_.fetch_add(1, std::memory_order_relaxed) != 0);
Sean Maher . unresolved

can you talk about why you believe that it makes sense to have memory_order_relaxed here? I'm not sure that I've yet figured out a case that it could cause problems yet, but I'm not convinced that it's necessarily the right idea given that the compiler could plausibly reorder it into locked operations on `table_` (though acq/rel wouldn't stop the compiler from doing that given that `table_` isn't operating on it.

To tie into my other comment on FindOrCreate(): it could plausibly reorder it before other operations in the Callable() in there that, while I can't imagine actually posing a problem, could maybe pose a problem if you engineered it to.

To my understanding according to the spec, the table lock and the atomic can be mutually reordered to the compiler's and CPU's whims. it feels like you're assuming that they can't.

Daniel Cheng

To be perfectly honest, I didn't write the original code. But RefCountedThreadSafe and blink's StringImpl::AddRef use relaxed semantics for the add and depend on the acquire/release barrier in Release() for refcount synchronization, and I can't think of a scenario where this is actually problematic.

Upon acquiring `table_lock_`, the invariant is every entry in `table_` has a refcount of at least 1 (I've added this to the comments)

it could plausibly reorder it before other operations in the Callable() in there

while this type aims to be robust and correct, I don't think it needs to guard against a callable that is intentionally being malicious.

Line 21, Patchset 15:// supported operation is `FindOrInsert()`, which either:
Sean Maher . resolved

FindOrCreate?

Daniel Cheng

Done

Open in Gerrit

Related details

Attention is currently required from:
  • Ben Wagner
  • David Benjamin
  • Jeremy Roman
  • Nico Weber
  • Sean Maher
Submit Requirements:
  • requirement satisfiedCode-Coverage
  • requirement satisfiedCode-Owners
  • requirement is not satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: chromium/src
Gerrit-Branch: main
Gerrit-Change-Id: If903b247109e11be2fb6440297a59e78d20d4ac7
Gerrit-Change-Number: 7706875
Gerrit-PatchSet: 16
Gerrit-Owner: Daniel Cheng <dch...@chromium.org>
Gerrit-Reviewer: Ben Wagner <bung...@google.com>
Gerrit-Reviewer: Daniel Cheng <dch...@chromium.org>
Gerrit-Reviewer: David Benjamin <davi...@chromium.org>
Gerrit-Reviewer: Jeremy Roman <jbr...@chromium.org>
Gerrit-Reviewer: Nico Weber <tha...@chromium.org>
Gerrit-Reviewer: Sean Maher <sp...@chromium.org>
Gerrit-CC: Hans Wennborg <ha...@chromium.org>
Gerrit-CC: Heron Yang <hero...@google.com>
Gerrit-CC: Hiroki Nakagawa <nhi...@chromium.org>
Gerrit-CC: Hu, Ningxin <ningx...@intel.com>
Gerrit-CC: Jiewei Qian <q...@chromium.org>
Gerrit-CC: Mangesh Ghiware <mghi...@google.com>
Gerrit-Attention: Nico Weber <tha...@chromium.org>
Gerrit-Attention: Jeremy Roman <jbr...@chromium.org>
Gerrit-Attention: David Benjamin <davi...@chromium.org>
Gerrit-Attention: Sean Maher <sp...@chromium.org>
Gerrit-Attention: Ben Wagner <bung...@google.com>
Gerrit-Comment-Date: Sat, 18 Apr 2026 21:19:10 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Sean Maher <sp...@chromium.org>
satisfied_requirement
unsatisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages