Skip to content

Commit

Permalink
[GC] Move post batch allocate thread bin maintenance logic in the thr…
Browse files Browse the repository at this point in the history
…ead bin.
  • Loading branch information
deadalnix committed Dec 4, 2024
1 parent 2381be3 commit 929606d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 45 deletions.
48 changes: 4 additions & 44 deletions sdlib/d/gc/bin.d
Original file line number Diff line number Diff line change
Expand Up @@ -35,51 +35,11 @@ struct Bin {
"Invalid arena or sizeClass!");
assert(slotSize == binInfos[sizeClass].slotSize, "Invalid slot size!");

void** insert;

{
mutex.lock();
scope(exit) mutex.unlock();

insert = (cast(Bin*) &this)
.batchAllocateImpl(filler, emap, sizeClass, top, bottom,
slotSize);
}

/**
* Note: If we are worried about security, we might want to shuffle
* our allocations around. This makes the uses of techniques
* like Heap Feng Shui difficult.
* We do not think it is worth the complication and performance
* hit in the general case, but something we might want to add
* in the future for security sensitive applications.
*
* http://www.phreedom.org/research/heap-feng-shui/heap-feng-shui.html
*/

// We filled the whole stack, done.
if (likely(insert is top)) {
return bottom;
}

/**
* We could simplify this code by inserting from top to bottom,
* in order to avoid moving all the elements when the stack has not
* been filled.
*
* However, because we allocate from the best slab to the worse one,
* this would result in a stack that allocate from the worse slab
* before the best ones.
*
* So we allocate from the bottom to the top, and move the whole stack
* if we did not quite reach the top.
*/
while (insert > bottom) {
*(--top) = *(--insert);
}
mutex.lock();
scope(exit) mutex.unlock();

assert(bottom <= top);
return top;
return (cast(Bin*) &this)
.batchAllocateImpl(filler, emap, sizeClass, top, bottom, slotSize);
}

uint batchFree(const(void*)[] worklist, PageDescriptor* pds,
Expand Down
38 changes: 37 additions & 1 deletion sdlib/d/gc/tbin.d
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,44 @@ public:
auto insert = _head - nfill;
assert(available <= insert);

_head = arena.batchAllocSmall(emap, sizeClass, _head, insert, slotSize);
auto from =
arena.batchAllocSmall(emap, sizeClass, _head, insert, slotSize);
state.refilled = true;

/**
* Note: If we are worried about security, we might want to shuffle
* our allocations around. This makes the uses of techniques
* like Heap Feng Shui difficult.
* We do not think it is worth the complication and performance
* hit in the general case, but something we might want to add
* in the future for security sensitive applications.
*
* http://www.phreedom.org/research/heap-feng-shui/heap-feng-shui.html
*/

// The whole sopace was filled. We are done.
if (likely(from is _head)) {
_head = insert;
return;
}

/**
* We could simplify this code by inserting from top to bottom,
* in order to avoid moving all the elements when the stack has not
* been filled.
*
* However, because we allocate from the best slab to the worse one,
* this would result in a stack that allocate from the worse slab
* before the best ones.
*
* So we allocate from the bottom to the top, and move the whole stack
* if we did not quite reach the top.
*/
while (from > insert) {
*(--_head) = *(--from);
}

assert(insert <= _head);
}

bool free(void* ptr) {
Expand Down

0 comments on commit 929606d

Please sign in to comment.