Bug 233381

Summary: Restore navigator.hardwareConcurrency
Product: WebKit Reporter: Tadeu Zagallo <tzagallo>
Component: WebCore Misc.Assignee: Tadeu Zagallo <tzagallo>
Status: RESOLVED FIXED    
Severity: Normal CC: ap, cdumez, cpeterson, esprehn+autocc, ews-watchlist, jonlee, kondapallykalyan, saam, sam, webkit-bug-importer, wilander
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch
none
Patch none

Description Tadeu Zagallo 2021-11-19 13:48:24 PST
Navigator.hardwareConcurrency was removed in r219379, but it is now used by emscripten to implement std::thread::hardware_concurrency()

<rdar://85023911>
Comment 1 Alexey Proskuryakov 2021-11-19 15:09:47 PST
r219379 doesn't explain the reasons why it was never enabled in WebKit, but there are multiple.

- Various privacy concerns (fingerprinting, socioeconomic targeting).

- Inconsistent meaning of return value ("logical processors" is very different when there is or isn't hyperthreading, or any other non-uniformity).

- Inherits the downsides of native API, which itself is harmful (no accounting for system load or other prioritization decisions that the system may be making, unlike with higher level APIs).
Comment 2 Tadeu Zagallo 2021-11-19 15:55:52 PST
Created attachment 444866 [details]
Patch
Comment 3 Saam Barati 2021-11-19 16:04:20 PST
Comment on attachment 444866 [details]
Patch

r=me
Comment 4 Tadeu Zagallo 2021-12-02 10:09:07 PST
Created attachment 445732 [details]
Patch
Comment 5 Tadeu Zagallo 2021-12-02 13:07:50 PST
Created attachment 445767 [details]
Patch
Comment 6 Alexey Proskuryakov 2021-12-02 13:15:15 PST
Comment on attachment 445767 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=445767&action=review

> Source/WebCore/page/NavigatorBase.cpp:188
> +        // If machines with more than 8 cores become commonplace, we should bump this number.
> +        // see https://bugs.webkit.org/show_bug.cgi?id=132588 for the
> +        // rationale behind this decision.

That is a bug with 100 comments to go through. What is the specific rationale for not always returning 2 or 4, for example? Why do we need even one bit of fingerprinting information?
Comment 7 Tadeu Zagallo 2021-12-02 16:19:02 PST
(In reply to Alexey Proskuryakov from comment #6)
> That is a bug with 100 comments to go through. What is the specific
> rationale for not always returning 2 or 4, for example? Why do we need even
> one bit of fingerprinting information?

That comment was kept from when I reverted the patch that deleted the original implementation, maybe it could use an update, although I think it's still mostly accurate. The information can be used through the pthread API to determine the number of workers to be started, so it's a compromise between giving an answer that is at least somewhat useful while mitigating fingerprinting issues.

Regarding your other points, I read most of the discussion in the original bug (https://bugs.webkit.org/show_bug.cgi?id=132588), it looks like the same discussion was had back in 2014 and I believe the conclusion was that regardless of other (better or not) approaches to concurrency, there's still a need for this API.
Comment 8 Alexey Proskuryakov 2021-12-02 16:49:03 PST
> it's a compromise between giving an answer that is at least somewhat useful while mitigating fingerprinting issues.

This makes sense in the abstract. However, we usually make performance decisions based on measurements, and trading off some amount of privacy for an unknown amount of performance feels icky. We also have a wider mix of desktop computers with and without multithreading (because of Apple Silicon Macs), making it harder for developers to guess what to do with the number of cores reported by this API.

It is true that this is partly a re-hash of the prior discussion. That one hasn't had a clear conclusion though - the code was added as an experimental feature, but never enabled by default AFAIK.

The fact that no one seems to have looked into creating a better API since 2014 could mean that this one is good enough, or that it hasn't become impactful enough to show its limitations in other browsers.
Comment 9 Tadeu Zagallo 2021-12-02 18:11:07 PST
(In reply to Alexey Proskuryakov from comment #8)
 
> However, we usually make performance decisions based on measurements, and trading off some amount of privacy for an unknown amount of performance feels icky.

This isn't a performance decision though, it's a compatibility issue. I arrived at this API by debugging a website not working. WebAssembly was trapping because it was using the result of this API which was unexpectedly 0 (well, undefined which was eventually converted to 0).
Comment 10 Alexey Proskuryakov 2021-12-02 18:20:50 PST
This part was specifically about returning values 4 or 8, and why we cannot return the same value always. That seems like a performance vs. privacy tradeoff to me.
Comment 11 Saam Barati 2021-12-03 11:10:01 PST
(In reply to Alexey Proskuryakov from comment #10)
> This part was specifically about returning values 4 or 8, and why we cannot
> return the same value always. That seems like a performance vs. privacy
> tradeoff to me.

Yes, it is. We decided this bit of information is an ok tradeoff to make. I'll link you to the discussion we had with John and Brent.
Comment 12 Alexey Proskuryakov 2021-12-03 11:34:51 PST
Right, what I'm saying is that this decision appears to have been made in a way that appears inconsistent with how we usually make performance related decisions. To be clear, I'm not stopping you, but I'm not feeling good about it.
Comment 13 Sam Weinig 2021-12-05 18:18:49 PST
(In reply to Saam Barati from comment #11)
> (In reply to Alexey Proskuryakov from comment #10)
> > This part was specifically about returning values 4 or 8, and why we cannot
> > return the same value always. That seems like a performance vs. privacy
> > tradeoff to me.
> 
> Yes, it is. We decided this bit of information is an ok tradeoff to make.
> I'll link you to the discussion we had with John and Brent.

Can you link it here? That seems like useful information to be in the bug and ChangeLog.
Comment 14 Saam Barati 2021-12-06 10:22:08 PST
(In reply to Sam Weinig from comment #13)
> (In reply to Saam Barati from comment #11)
> > (In reply to Alexey Proskuryakov from comment #10)
> > > This part was specifically about returning values 4 or 8, and why we cannot
> > > return the same value always. That seems like a performance vs. privacy
> > > tradeoff to me.
> > 
> > Yes, it is. We decided this bit of information is an ok tradeoff to make.
> > I'll link you to the discussion we had with John and Brent.
> 
> Can you link it here? That seems like useful information to be in the bug
> and ChangeLog.

Unfortunately it was on the Apple internal slack (and probably shouldn't have been). Maybe John can comment in this bug and share his opinion?
Comment 15 EWS 2021-12-06 10:46:10 PST
Committed r286550 (244881@main): <https://commits.webkit.org/244881@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 445767 [details].