<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>30922</bug_id>
          
          <creation_ts>2009-10-29 13:14:59 -0700</creation_ts>
          <short_desc>ThreadIdentifier abstraction is unnecessarily slow</short_desc>
          <delta_ts>2009-10-29 13:33:18 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>JavaScriptCore</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>Mac</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>DUPLICATE</resolution>
          <dup_id>25348</dup_id>
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Enhancement</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Jens Alfke">jens</reporter>
          <assigned_to name="Jens Alfke">jens</assigned_to>
          
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>159027</commentid>
    <comment_count>0</comment_count>
    <who name="Jens Alfke">jens</who>
    <bug_when>2009-10-29 13:14:59 -0700</bug_when>
    <thetext>Threading.h uses a &apos;ThreadIdentifier&apos; type to abstract away platform-specific thread types like pthread_t. But the implementation of this type for pthreads, found in ThreadingPthreads.cpp, is suboptimal.  This is causing measurable performance impact in Chrome*; I haven&apos;t checked Safari.

ThreadIdentifiers are consecutive integers which are mapped from pthread_t&apos;s via a HashMap. This means that looking up the identifier of a thread (including the current thread) means first acquiring a mutex, then a hashtable lookup. And looking up a thread by its identifier requires a mutex plus a linear scan of the table.

I can think of two much faster ways to do this:
(1) Define ThreadIdentifier as a typedef of pthread_t, eliminating the need for the mapping altogether. Drawback is that pthread_t is probably a pointer, making it 64 bits wide in a 64-bit process, which increases the size of a ThreadIdentifier. Does this matter? We probably don&apos;t store enough of these to make a difference.
(2) Use a pthread_t[ ] array instead of a dictionary. The ThreadIdentifier is just the array index where that pthread_t is found. This makes lookup quite fast. If the array is fixed-size then no locking is needed, you can do an atomic check-and-test to append to the array. Drawback is that the array is fixed-size and thread IDs are never removed, so the array could overflow if a lot of threads are created.
(3) Use pthread_getspecific et al to associate the identifier. I don&apos;t know how well this performs compared to what exists.

Option 1 seems like a winner to me. It&apos;s very fast and allows at least a page of code to be ripped out of ThreadingPthreads.cpp.

* Here&apos;s some analysis from Shark, running the Dromaeo core DOM benchmarks, focused on the V8 GC entry point (so it shows up as 100%). Note that 9.6% of the GC time is spent in WTF::currentThread, becuase DOMData::ensureDeref has to check whether the object is being released on its native thread or not.
	0.0%	100.0%	Chromium Framework	v8::internal::Heap::CollectGarbage(int, v8::internal::AllocationSpace)
	0.0%	100.0%	Chromium Framework	 v8::internal::Heap::PerformGarbageCollection(v8::internal::AllocationSpace, v8::internal::GarbageCollector, v8::internal::GCTracer*)
	10.8%	63.8%	Chromium Framework	  v8::internal::GlobalHandles::PostGarbageCollectionProcessing()
	0.2%	41.6%	Chromium Framework	   WebCore::DOMDataStore::weakDOMObjectCallback(v8::Persistent&lt;v8::Value&gt;, void*)
	1.6%	40.4%	Chromium Framework	    void WebCore::DOMData::handleWeakObject&lt;void&gt;(WebCore::DOMDataStore::DOMWrapperMapType, v8::Handle&lt;v8::Object&gt;, void*)
	7.1%	19.7%	Chromium Framework	     WebCore::DOMData::ensureDeref(WebCore::V8ClassIndex::V8WrapperType, void*)
	0.3%	9.6%	Chromium Framework	      WTF::currentThread()
	5.7%	9.1%	Chromium Framework	       _ZN3WTFL25identifierByPthreadHandleERKP17_opaque_pthread_t</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>159039</commentid>
    <comment_count>1</comment_count>
    <who name="Mark Rowe (bdash)">mrowe</who>
    <bug_when>2009-10-29 13:33:18 -0700</bug_when>
    <thetext>

*** This bug has been marked as a duplicate of bug 25348 ***</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>