<?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>170343</bug_id>
          
          <creation_ts>2017-03-31 09:11:17 -0700</creation_ts>
          <short_desc>WebAssembly: recycle fast memories more better</short_desc>
          <delta_ts>2017-08-07 10:00: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>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=163600</see_also>
    
    <see_also>https://bugs.webkit.org/show_bug.cgi?id=175150</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          <blocked>159775</blocked>
          <everconfirmed>1</everconfirmed>
          <reporter name="JF Bastien">jfbastien</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>clopez</cc>
    
    <cc>fpizlo</cc>
    
    <cc>ggaren</cc>
    
    <cc>jfbastien</cc>
    
    <cc>keith_miller</cc>
    
    <cc>mark.lam</cc>
    
    <cc>msaboff</cc>
    
    <cc>saam</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1293175</commentid>
    <comment_count>0</comment_count>
    <who name="JF Bastien">jfbastien</who>
    <bug_when>2017-03-31 09:11:17 -0700</bug_when>
    <thetext>We could be smarter about how WasmMemory.cpp recycles fast memories. Right now we:

 - Put up to 4 of them on a free list
 - memset to 0
 - PROT_NONE the entire range

A few random ideas:

 1. We do this synchronously and I&apos;m not sure that&apos;s a great idea. Maybe it&apos;s fine, it would be good to measure.
 2. Is it even worth considering bzero versus memset 0?
 3. Move memset 0 to re-allocation, instead of de-allocation (we still need to PROT_NONE, and remember how many pages were dirty, on reallocation un-PROT_NONE the range, zero it out, then honor the new allocation&apos;s wishes, ugh).
 4. We could then consider using madvise. On MacOS we have MADV_ZERO_WIRED_PAGES which sounds pretty interesting. See kern_mman.c, it maps to VM_BEHAVIOR_ZERO_WIRED_PAGES in vm_map.c:vm_map_behavior_set this will set zero_wired_pages which will asynchronously cause vm_fault.c:vm_fault_unwire to pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(result_page)).
 5. We could return the fast memories if we GC a few times and never reuse them, or if we get a &quot;low-memory&quot; signal from the system.

I think 4. and 5. are really the interesting ones, but then we need to:

 - For 4.: figure out if, when we&apos;re ready to reuse a fast memory, the pages haven&apos;t been zero&apos;d out.
 - For 5.: is holding on to virtually-allocated but not physically wired pages that bad? It will cause the kernel to use more memory per page table and potentially make TLB misses a tiny bit slower.

Let&apos;s measure whether that&apos;s important at all:

 - Is deallocation measurably slow right now?
 - Does it get faster?
 - Do small-memory systems like us more because our number of dirty pages goes down faster?

I&apos;ve measure dirty pages on a limited-memory system in a test that does work and reuses memories, and the number of dirty pages definitely goes up / down between tests, so we&apos;re not fairing that bad right now.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1293275</commentid>
    <comment_count>1</comment_count>
    <who name="Geoffrey Garen">ggaren</who>
    <bug_when>2017-03-31 13:09:04 -0700</bug_when>
    <thetext>For large allocations that need zero-backing, we generally prefer MADV_FREE_REUSABLE with explicit zeroing because it avoids the (very large) cost of page faults.

JF pointed out that Emscripten doesn&apos;t support grow-on-demand heaps, so lots of WASM programs demand very large heaps that they don&apos;t use. If that&apos;s true, WASM may be an exception to our general strategy, and it may benefit from MADV_ZERO_WIRED_PAGES or some other madvise/mmap API that forces on-demand page-fault-and-zero-fill behavior, since the memory cost of huge overcommits may be too high.

&gt;  5. We could return the fast memories if we GC a few times and never reuse them, or if we get a &quot;low-memory&quot; signal from the system.

There&apos;s no point to unmapping cached fast memories in response to low memory warnings because they don&apos;t hold any physical pages.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>