Closed Bug 565229 Opened 16 years ago Closed 16 years ago

TM: bulk clear GC bitmaps when GC starts instead of after the GC ends

Categories

(Core :: JavaScript Engine, defect)

x86
macOS
defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: gal, Assigned: gal)

References

Details

(Whiteboard: fixed-in-tracemonkey)

Attachments

(1 file)

The motivation is to have the mark bitmap survive the GC. This might also have a performance benefit. We have large chunks and thus large bitmaps now, which we can bulk-clear with SSE2/3. This will also warm up the cache and have the bitmaps prefetched. Clearing them after the GC should yield strictly worse cache behavior since we don't touch them any more. With this change we should be able to remove the special case code that treats double bitmaps special and clears them on the fly since now all bitmaps will be clear. Keeping the mark bit to indicate that anything in the arena was live at all might still be a good idea.
Blocks: 565217
Test case to evaluate this with: for (var i = 0; i < 4; ++i) { var a = []; for (n = 0; n < 512 * 1024; ++n) { var j = (i == 3) ? Math.floor(Math.random()*4) : i; a.push((i == 0) ? ({}) : ((i == 1) ? (n + 0.5) : ("x" + i))); } var t = Date.now(); for (n = 0; n < 1000; ++n) gc(); print(Date.now() - t); }
Summary: TM: bulk delete GC bitmaps when GC starts instead after the GC → TM: bulk clear GC bitmaps when GC starts instead after the GC
Summary: TM: bulk clear GC bitmaps when GC starts instead after the GC → TM: bulk clear GC bitmaps when GC starts instead of after the GC ends
Assignee: general → gal
Attached patch patch — — Splinter Review
host-6-104:src gal$ ./Darwin_OPT.OBJ/js x.js (with) 1379 298 464 490 host-6-104:src gal$ ./Darwin_OPT.OBJ/js x.js (without) 1373 345 467 489 The patch is a win for doubles, and neutral otherwise. I want the surviving mark bits, so definitely committing this.
Attachment #445513 - Flags: review?(anygregor)
Comment on attachment 445513 [details] [diff] [review] patch >diff --git a/js/src/jsgc.cpp b/js/src/jsgc.cpp > >+static bool >+HasMarkedDoubles(JSGCArena *a) >+{ >+ jsbitmap *markBitmap = a->getMarkBitmap(); >+ jsbitmap *lastMarkWord = markBitmap + GC_MARK_BITMAP_WORDS - 1; >+ >+ for (jsbitmap *m = markBitmap; m <= lastMarkWord; ++m) >+ if (*m) >+ return true; >+ return false; >+} doubles are typically short-lived and do not survive a single GC. The above loop optimizes against that as for all-dead doubles it would do GC_MARK_BITMAP_WORDS * 2 or 16 useless compare operations per 64 doubles. It is better to replace it with something like: jsbitmap all = markBitma[0] | markBitmap[1] ... | markBitmap[8] together with a static assert that GC_MARK_BITMAP_WORDS is 8.
The uses comparisons in the loop are strictly faster than your clever flag trick we had before. Your alternative proposal does exactly the same number of useless comparisons, the only difference is that you suggest unrolling the loop. Here are the results with your suggested change: whale:src gal$ ./Darwin_OPT.OBJ/js x.js 1367 298 465 490 In other words this improves exactly nothing. While a software unrolled loop might be minimally faster than the unrolling super scalar hardware does, in the grand scheme of things thats simply noise. GCing is dominated by L2 cache misses. Machines are highly complex beasts and impossible to predict using gut feeling. What feels clever or fast often isn't. We need more profiling and less complex code.
(In reply to comment #5) > whale:src gal$ ./Darwin_OPT.OBJ/js x.js The test measures the speed of double marking. We need a test that measures a bulk double release, like this one: var a = []; for (var i = 0; i != 512 * 1024; ++i) a.push(i + 0.1); var t = Date.now(); a = null; gc(); t = Date.now() - t; Could you run this with and without the patch?
whale:src gal$ ./Darwin_OPT.OBJ/js xx.js 0 That's with the patch. I assume its ok if I don't try it without.
Sorry for the bogus test, I meant to use the clock numbers. Here is what I have based on rdtsc counter for the following test: var a = []; for (var i = 0; i != 512 * 1024; ++i) a.push(i + 0.1); a = null; gc(); Number of cycles before the patch: 19149 After the patch: 34383 So the patch makes the finalization of doubles almost 2 times slower.
What clock numbers? You mean cycles?
The "cycles" in the above comment mean the number of rdtsc cycles spend in SweepDoubles.
If I change HasMarkedDoubles as suggested in the comment 4 the number of cycles spent in SweepDoubles drops from 35000 down to 29000. Which is still higher than 19000 without the patch.
The difference is 15,000 cycles. On my machine thats a slowdown of 5us if I am doing the math right in my head. So if the heap only contains dead doubles, we get 5us slower. If doubles survive (testcase above), we get over a millisecond (1ms) faster. Neither matters at all in a realistic GC, in particular since we are on track to eliminate heap doubles.
You do realize that our average GC is close to half a billion cycles, right? This is a completely pointless micro optimization that doesn't win anything. Show me a shark profile of a realistic test case hitting this and we talk.
(In reply to comment #13) > You do realize that our average GC is close to half a billion cycles, right? > This is a completely pointless micro optimization that doesn't win anything. The optimization was not pointless when the GC arena contained both doubles and their flags. AFAICS from the benchmark results the bigger chunks with separated flag bitmap make scanning of double bitmap 15 times faster. Before that on N270 ATOM CPU it took closer to 15ms to scan the full heap of doubles (about 5 millions of them, 10 times more than the benchmark from the comment 8 generates) without using the hasMarkedDoubles optimization. The latter speeded up that almost twice while slowing down the marking by a significantly smaller amount. But you right that now the optimization becomes pointless as saving 100K - 1500K of cycles is not something that we should worry about and the code simplicity wins especially since the changes are needed for another bug. > We need more profiling and less complex code. Before I have added the hasMarkedDouble optimization I considered to use a loop like in the proposed patch. But even after replacing jsbitmap *lastMarkWord = markBitmap + GC_MARK_BITMAP_WORDS - 1; for (jsbitmap *m = markBitmap; m <= lastMarkWord; ++m) if (*m) return true; return false; with (which is IMO simpler to perceive than the loop above) jsbitmap *mb = a->getMarkBitmap(); JS_STATIC_ASSERT(GC_MARK_BITMAP_WORDS == 8); return !!(mb[0] | mb[1] | mb[2] | mb[3] | mb[4] | mb[5] | mb[6] | mb[7]); the code could not beat the per arena flag. It is also interesting that unrolling the loop on top of the patch is still faster both for the case of all-dead doubles and all-live ones at least on i5 CPU for the following test case: var a = []; for (var i = 0; i != 5 * 1024 * 1024; ++i) a.push(i + 0.1); gc(); a = null; gc(); In particular the number of cycles for the two GC calls is reduced from 342 and 305 thousands down to 333 and 241 thousands.
Comment on attachment 445513 [details] [diff] [review] patch >@@ -2049,20 +2035,16 @@ js_CallGCMarker(JSTracer *trc, void *thi > /* > * Optimize for string and double as their size is known and their tracing > * is not recursive. > */ > switch (kind) { > case JSTRACE_DOUBLE: { > JSGCArenaInfo *ainfo = JSGCArenaInfo::fromGCThing(thing); > JS_ASSERT(!ainfo->list); >- if (!ainfo->hasMarkedDoubles) { >- ainfo->hasMarkedDoubles = true; >- JSGCArena::fromGCThing(thing)->clearMarkBitmap(); >- } In an optimized build the compiler warns with the patch that ainfo is unused.
Comment on attachment 445513 [details] [diff] [review] patch > >+inline void >+JSGCChunkInfo::clearMarkBitmap() >+{ >+ PodZero(reinterpret_cast<jsbitmap *>(getChunk() + GC_MARK_BITMAP_ARRAY_OFFSET), >+ GC_MARK_BITMAP_WORDS * GC_ARENAS_PER_CHUNK); You need another line break if the limit is still 80. >@@ -2049,20 +2035,16 @@ js_CallGCMarker(JSTracer *trc, void *thi > /* > * Optimize for string and double as their size is known and their tracing > * is not recursive. > */ > switch (kind) { > case JSTRACE_DOUBLE: { > JSGCArenaInfo *ainfo = JSGCArenaInfo::fromGCThing(thing); > JS_ASSERT(!ainfo->list); >- if (!ainfo->hasMarkedDoubles) { >- ainfo->hasMarkedDoubles = true; >- JSGCArena::fromGCThing(thing)->clearMarkBitmap(); >- } Yeah this gives a warning in debug mode.
Attachment #445513 - Flags: review?(anygregor) → review+
Landed with igor's unrolled loop and the dead variable removed. We no longer have an 80 columns limit. http://hg.mozilla.org/tracemonkey/rev/955bfaadaebb
Whiteboard: fixed-in-tracemonkey
Blocks: 566578
This must be in m-c by now, right?
Yes, this was merged for sure.
Status: NEW → RESOLVED
Closed: 16 years ago
Resolution: --- → FIXED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: