Skip to content

Commit

Permalink
fix: Reduce reduntant memory loads in Malloc.free (#747)
Browse files Browse the repository at this point in the history
  • Loading branch information
cician committed Jul 4, 2021
1 parent 4c9636c commit a5817b1
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions stdlib/runtime/malloc.gr
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,27 @@ export let free = (ap: WasmI32) => {
// is actually already pointing to this node, so we don't do anything.
if (blockPtr != freePtr) {
// Find the location to insert this block into the free list
for ( ; !((blockPtr > p) && (blockPtr < getNext(p))); p = getNext(p) ) {
if ((p >= getNext(p)) && ((blockPtr > p) || (blockPtr < getNext(p)))) {
while (true) {
let nextp = getNext(p)
if ( ((blockPtr > p) && (blockPtr < nextp)) || ((p >= nextp) && ((blockPtr > p) || (blockPtr < nextp))) ) {
break
}
p = nextp
}

// Merge the block into the adjacent free list entry above, if needed
if (blockPtr + getSize(blockPtr) == getNext(p)) {
let next = getNext(p)
setSize(blockPtr, getSize(blockPtr) + getSize(next))
let blockPtrSize = getSize(blockPtr)
let next = getNext(p)
if (blockPtr + blockPtrSize == next) {
setSize(blockPtr, blockPtrSize + getSize(next))
setNext(blockPtr, getNext(next))
} else {
setNext(blockPtr, getNext(p))
setNext(blockPtr, next)
}
// Merge the previous (adjacent) free list entry into this block, if needed
if (p + getSize(p) == blockPtr) {
setSize(p, getSize(p) + getSize(blockPtr))
let pSize = getSize(p)
if (p + pSize == blockPtr) {
setSize(p, pSize + getSize(blockPtr))
setNext(p, getNext(blockPtr))
} else {
setNext(p, blockPtr)
Expand Down

0 comments on commit a5817b1

Please sign in to comment.