Closed Bug 710883 Opened 14 years ago Closed 14 years ago

Create js::BufferUnderloaded for use in JS engine

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED

People

(Reporter: cdleary, Assigned: n.nethercote)

References

Details

(Whiteboard: [MemShrink:P3])

Nick, per our discussion in bug 708873 comment 6, it would be very helpful to have a function internal to the JS engine that gave a conservative approximation for undesirable buffer slop to avoid triggering the memory reporter assertion. This would give us a central heuristic to tune in order to avoid unnecessary reallocs, but still avoid the memory allocator assertion for all practical purposes. As we noted in that bug, StringBuffer can create strings from numerous places in the engine with various lengths and load factors (thanks to |Vector::reserve| and the vector space doubling behavior). If you could write a heuristic that we could slot into places like |StringBuffer::extractWellSized| that would be quite useful, so we could just say: if (length > CharBuffer::sMaxInlineStorage && BufferUnderloaded(length * sizeof(jschar), capacity * sizeof(jschar))) { // ... realloc the buffer to size ... }
I can't see a way to do this without using moz_malloc_usable_size or equivalent, and that's not doable within the JS engine. (I tried defining a JS-specific equivalent a while ago and hit all sorts of troubles, I can't remember exactly why now, but they were bad enough that I found a different way to solve the problem.) The only way forward is can see is to create a JSAPI function that lets you register a moz_malloc_usable_size-like function with the engine, and use that if it's been registered. The browser would register just after the JS engine starts up.
How fast is moz_malloc_usable_size?
(In reply to Luke Wagner [:luke] from comment #2) > How fast is moz_malloc_usable_size? Not that fast... it's similar to realloc() when it shrinks, judging from jemalloc's code. Do we have evidence that unconditional realloc'ing is too slow? They'll always be shrinking reallocs, which is the fastest case.
Does realloc() always take a global lock?
It appears not to for non-huge allocations. You can check for yourself in memory/jemalloc/jemalloc.c.
(In reply to Nicholas Nethercote [:njn] from comment #5) > You can check for yourself in memory/jemalloc/jemalloc.c. You can do that ?!! ;-) The path does look reasonably fast (the non-moving-small-realloc path is surprisingly easy to follow). Given that extractWellSized() only reallocs when length > sMaxInlineLength and, for large strings, the cost of the the lock should be amortized by the cost of building the actual string, I think you're right that realloc() makes sense.
Whiteboard: [MemShrink] → [MemShrink:P3]
P3 or just WONTFIX?
We need to do something to create the right-sized strings to avoid the assertion failure in the memory reporters.
There's a simpler option. If you pass computedSize=0 as the second argument to mallocSizeOf() the size checking isn't performed. The downside of this is that on platforms where moz_malloc_usable_size always returns 0, the string won't be counted at all. But such platforms are very unusual. So we could just pass computedSize=0 into the two calls to mallocSizeOf() in JSString::charsHeapSize().
Depends on: 715453
This assertion was just removed in bug 715453.
Status: NEW → RESOLVED
Closed: 14 years ago
Resolution: --- → FIXED
Assignee: general → n.nethercote
You need to log in before you can comment on or make changes to this bug.