Re: [PATCH 3/7] perf: order iterators for visit_groups_merge into a min-heap

From: Peter Zijlstra
Date: Mon Jul 08 2019 - 12:36:41 EST


On Mon, Jul 01, 2019 at 11:59:51PM -0700, Ian Rogers wrote:

> +/* Sift the perf_event at pos down the heap. */
> +static void min_heapify(struct perf_event_heap *heap, int pos)
> +{
> + int left_child, right_child;
> +
> + while (pos > heap->num_elements / 2) {

Did that want to be 'pos < head->num_elements / 2' ?

> + left_child = pos * 2;
> + right_child = pos * 2 + 1;
> + if (heap->storage[pos]->group_index >
> + heap->storage[left_child]->group_index) {
> + min_heap_swap(heap, pos, left_child);
> + pos = left_child;
> + } else if (heap->storage[pos]->group_index >
> + heap->storage[right_child]->group_index) {
> + min_heap_swap(heap, pos, right_child);
> + pos = right_child;
> + } else {
> + break;
> + }
> + }
> +}
> +
> +/* Floyd's approach to heapification that is O(n). */
> +static void min_heapify_all(struct perf_event_heap *heap)
> +{
> + int i;
> +
> + for (i = heap->num_elements / 2; i > 0; i--)
> + min_heapify(heap, i);
> +}

Otherwise I don't actually see it do anything at all. Also, when pos >
nr/2, then pos * 2 is silly.