liveBook · Manning

Riptide

As explained in JavaScript in Depth

Riptide is a book-specific garbage-collection design that performs most marking work on background threads.

Riptide is JavaScriptCore’s non-moving garbage collector. Objects remain in place rather than moving between semi-spaces, making its design fundamentally different from V8’s movement-based collector. section 9.3

Its defining optimization is concurrency: most marking runs on background threads while application code continues. A write barrier can force already visited objects back into consideration, creating a retreating wavefront and, under heavy mutation of old objects, a potential death spiral. section 9.3.4

A space-time scheduler prevents nontermination by linking allocation pressure to pause length. JSC reserves headroom, progressively gives the collector more CPU time as allocations consume it, and stops the application if the budget is exhausted. section 9.3.4

A cache that pushes marking backward

A long-lived sessions Map stores old-generation session objects. Each request mutates those objects with fresh request data, copied headers, timestamps, and truncated history. Under sustained allocation and mutation, the collector may revisit the same objects repeatedly; latency rises as JSC throttles the application to finish marking. section 9.3.4

const sessions = new Map();  #A
function handleRequest(req) {
  let session = sessions.get(req.sessionId);
  if (!session) {
    session = { id: req.sessionId, created: Date.now() };
    sessions.set(req.sessionId, session);
  }
 
  // Every request mutates the long-lived session object
  // with fresh short-lived objects
  session.lastAccess = Date.now();
  session.lastRequest = {  #B
    url: req.url,
    headers: { ...req.headers },
    timestamp: Date.now(),
  };
  session.history = (session.history || [])
    .slice(-99)
    .concat([req.url]);  #C
 
  return buildResponse(req, session);
}

Inspect the request handler to trace how persistent session state receives newly allocated objects on every request. section 9.3.4

How JavaScript in Depth covers it

How deep it goes
Descends from non-moving objects to concurrent marking, revisit barriers, headroom, scheduling, throttling, and death-spiral behavior.
Examples
Uses a persistent sessions Map that receives fresh request objects, copied headers, timestamps, and rolling history.
What you can do after
Diagnose latency increases and allocation patterns caused by heavy writes to long-lived objects in JavaScriptCore.
How it connects
Ties garbage collection to JavaScriptCore, V8, incremental marking, concurrent marking, old generation, and write barriers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of JavaScript in Depth. The section links point to the parts of the book it draws on.