mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 21:39:07 +00:00
guard all inc and dec calls in zig by elements_refcounted to avoid noop loops
This commit is contained in:
parent
9052fbd09c
commit
2fd2c25c13
1 changed files with 52 additions and 36 deletions
|
@ -408,12 +408,14 @@ pub fn listReleaseExcessCapacity(
|
||||||
const dest_ptr = output.bytes orelse unreachable;
|
const dest_ptr = output.bytes orelse unreachable;
|
||||||
|
|
||||||
@memcpy(dest_ptr[0..(old_length * element_width)], source_ptr[0..(old_length * element_width)]);
|
@memcpy(dest_ptr[0..(old_length * element_width)], source_ptr[0..(old_length * element_width)]);
|
||||||
|
if (elements_refcounted) {
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (i < old_length) : (i += 1) {
|
while (i < old_length) : (i += 1) {
|
||||||
const element = source_ptr + i * element_width;
|
const element = source_ptr + i * element_width;
|
||||||
inc(element);
|
inc(element);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
list.decref(alignment, element_width, elements_refcounted, dec);
|
list.decref(alignment, element_width, elements_refcounted, dec);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
@ -539,12 +541,14 @@ pub fn listSublist(
|
||||||
if (list.isUnique()) {
|
if (list.isUnique()) {
|
||||||
// Decrement the reference counts of all elements.
|
// Decrement the reference counts of all elements.
|
||||||
if (list.bytes) |source_ptr| {
|
if (list.bytes) |source_ptr| {
|
||||||
|
if (elements_refcounted) {
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (i < size) : (i += 1) {
|
while (i < size) : (i += 1) {
|
||||||
const element = source_ptr + i * element_width;
|
const element = source_ptr + i * element_width;
|
||||||
dec(element);
|
dec(element);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var output = list;
|
var output = list;
|
||||||
output.length = 0;
|
output.length = 0;
|
||||||
|
@ -571,12 +575,14 @@ pub fn listSublist(
|
||||||
if (start == 0 and list.isUnique()) {
|
if (start == 0 and list.isUnique()) {
|
||||||
// The list is unique, we actually have to decrement refcounts to elements we aren't keeping around.
|
// The list is unique, we actually have to decrement refcounts to elements we aren't keeping around.
|
||||||
// Decrement the reference counts of elements after `start + keep_len`.
|
// Decrement the reference counts of elements after `start + keep_len`.
|
||||||
|
if (elements_refcounted) {
|
||||||
const drop_end_len = size_minus_start - keep_len;
|
const drop_end_len = size_minus_start - keep_len;
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (i < drop_end_len) : (i += 1) {
|
while (i < drop_end_len) : (i += 1) {
|
||||||
const element = source_ptr + (start + keep_len + i) * element_width;
|
const element = source_ptr + (start + keep_len + i) * element_width;
|
||||||
dec(element);
|
dec(element);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var output = list;
|
var output = list;
|
||||||
output.length = keep_len;
|
output.length = keep_len;
|
||||||
|
@ -631,8 +637,10 @@ pub fn listDropAt(
|
||||||
// were >= than `size`, and we know `size` fits in usize.
|
// were >= than `size`, and we know `size` fits in usize.
|
||||||
const drop_index: usize = @intCast(drop_index_u64);
|
const drop_index: usize = @intCast(drop_index_u64);
|
||||||
|
|
||||||
|
if (elements_refcounted) {
|
||||||
const element = source_ptr + drop_index * element_width;
|
const element = source_ptr + drop_index * element_width;
|
||||||
dec(element);
|
dec(element);
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE
|
// NOTE
|
||||||
// we need to return an empty list explicitly,
|
// we need to return an empty list explicitly,
|
||||||
|
@ -669,11 +677,13 @@ pub fn listDropAt(
|
||||||
const tail_size = (size - drop_index - 1) * element_width;
|
const tail_size = (size - drop_index - 1) * element_width;
|
||||||
@memcpy(tail_target[0..tail_size], tail_source[0..tail_size]);
|
@memcpy(tail_target[0..tail_size], tail_source[0..tail_size]);
|
||||||
|
|
||||||
|
if (elements_refcounted) {
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (i < output.len()) : (i += 1) {
|
while (i < output.len()) : (i += 1) {
|
||||||
const cloned_elem = target_ptr + i * element_width;
|
const cloned_elem = target_ptr + i * element_width;
|
||||||
inc(cloned_elem);
|
inc(cloned_elem);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
list.decref(alignment, element_width, elements_refcounted, dec);
|
list.decref(alignment, element_width, elements_refcounted, dec);
|
||||||
|
|
||||||
|
@ -817,11 +827,13 @@ pub fn listConcat(
|
||||||
@memcpy(source_a[(list_a.len() * element_width)..(total_length * element_width)], source_b[0..(list_b.len() * element_width)]);
|
@memcpy(source_a[(list_a.len() * element_width)..(total_length * element_width)], source_b[0..(list_b.len() * element_width)]);
|
||||||
|
|
||||||
// Increment refcount of all cloned elements.
|
// Increment refcount of all cloned elements.
|
||||||
|
if (elements_refcounted) {
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (i < list_b.len()) : (i += 1) {
|
while (i < list_b.len()) : (i += 1) {
|
||||||
const cloned_elem = source_b + i * element_width;
|
const cloned_elem = source_b + i * element_width;
|
||||||
inc(cloned_elem);
|
inc(cloned_elem);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// decrement list b.
|
// decrement list b.
|
||||||
list_b.decref(alignment, element_width, elements_refcounted, dec);
|
list_b.decref(alignment, element_width, elements_refcounted, dec);
|
||||||
|
@ -845,11 +857,13 @@ pub fn listConcat(
|
||||||
@memcpy(source_b[0..byte_count_a], source_a[0..byte_count_a]);
|
@memcpy(source_b[0..byte_count_a], source_a[0..byte_count_a]);
|
||||||
|
|
||||||
// Increment refcount of all cloned elements.
|
// Increment refcount of all cloned elements.
|
||||||
|
if (elements_refcounted) {
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (i < list_a.len()) : (i += 1) {
|
while (i < list_a.len()) : (i += 1) {
|
||||||
const cloned_elem = source_a + i * element_width;
|
const cloned_elem = source_a + i * element_width;
|
||||||
inc(cloned_elem);
|
inc(cloned_elem);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// decrement list a.
|
// decrement list a.
|
||||||
list_a.decref(alignment, element_width, elements_refcounted, dec);
|
list_a.decref(alignment, element_width, elements_refcounted, dec);
|
||||||
|
@ -869,6 +883,7 @@ pub fn listConcat(
|
||||||
@memcpy(target[(list_a.len() * element_width)..(total_length * element_width)], source_b[0..(list_b.len() * element_width)]);
|
@memcpy(target[(list_a.len() * element_width)..(total_length * element_width)], source_b[0..(list_b.len() * element_width)]);
|
||||||
|
|
||||||
// Increment refcount of all cloned elements.
|
// Increment refcount of all cloned elements.
|
||||||
|
if (elements_refcounted) {
|
||||||
var i: usize = 0;
|
var i: usize = 0;
|
||||||
while (i < list_a.len()) : (i += 1) {
|
while (i < list_a.len()) : (i += 1) {
|
||||||
const cloned_elem = source_a + i * element_width;
|
const cloned_elem = source_a + i * element_width;
|
||||||
|
@ -879,6 +894,7 @@ pub fn listConcat(
|
||||||
const cloned_elem = source_b + i * element_width;
|
const cloned_elem = source_b + i * element_width;
|
||||||
inc(cloned_elem);
|
inc(cloned_elem);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// decrement list a and b.
|
// decrement list a and b.
|
||||||
list_a.decref(alignment, element_width, elements_refcounted, dec);
|
list_a.decref(alignment, element_width, elements_refcounted, dec);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue