[tessellator] Use a combsort for sorting the event queue.

In my experiments using cairo-perf, the inlined combsort is ~20% quicker
than the library qsort.
This commit is contained in:
Chris Wilson 2008-10-07 22:09:37 +01:00
parent b146130841
commit ef9e0a5d1d
3 changed files with 92 additions and 19 deletions

View file

@ -57,6 +57,7 @@ cairo_private = \
cairo-atomic-private.h \
cairo-cache-private.h \
cairo-clip-private.h \
cairo-combsort-private.h \
cairo-compiler-private.h \
cairo-fixed-private.h \
cairo-fixed-type-private.h \

View file

@ -38,6 +38,7 @@
#include "cairo-skiplist-private.h"
#include "cairo-freelist-private.h"
#include "cairo-combsort-private.h"
#define DEBUG_VALIDATE 0
#define DEBUG_PRINT_STATE 0
@ -630,21 +631,18 @@ cairo_bo_event_compare_abstract (void *list,
}
static int
cairo_bo_event_compare_pointers (void const *voida,
void const *voidb)
cairo_bo_event_compare_pointers (const cairo_bo_event_t *a,
const cairo_bo_event_t *b)
{
cairo_bo_event_t const * const *a = voida;
cairo_bo_event_t const * const *b = voidb;
if (*a != *b) {
int cmp = cairo_bo_event_compare (*a, *b);
if (cmp)
return cmp;
if (*a < *b)
return -1;
if (*a > *b)
return +1;
}
return 0;
int cmp;
if (a == b)
return 0;
cmp = cairo_bo_event_compare (a, b);
if (cmp)
return cmp;
return a - b;
}
static inline cairo_int64_t
@ -957,6 +955,10 @@ _cairo_bo_event_dequeue (cairo_bo_event_queue_t *event_queue)
return intersection;
}
CAIRO_COMBSORT_DECLARE (_cairo_bo_event_queue_sort,
cairo_bo_event_t *,
cairo_bo_event_compare_pointers)
static cairo_status_t
_cairo_bo_event_queue_init (cairo_bo_event_queue_t *event_queue,
cairo_bo_edge_t *edges,
@ -989,8 +991,8 @@ _cairo_bo_event_queue_init (cairo_bo_event_queue_t *event_queue,
event_queue->next_startstop_event_index = 0;
for (i = 0; i < num_edges; i++) {
sorted_event_ptrs[2*i] = &events[2*i];
sorted_event_ptrs[2*i+1] = &events[2*i+1];
sorted_event_ptrs[i] = &events[2*i];
sorted_event_ptrs[i+num_edges] = &events[2*i+1];
/* Initialize "middle" to top */
edges[i].middle = edges[i].top;
@ -1006,9 +1008,8 @@ _cairo_bo_event_queue_init (cairo_bo_event_queue_t *event_queue,
edges[i].bottom);
}
qsort (sorted_event_ptrs, num_events,
sizeof(cairo_bo_event_t *),
cairo_bo_event_compare_pointers);
_cairo_bo_event_queue_sort (sorted_event_ptrs, num_events);
return CAIRO_STATUS_SUCCESS;
}

View file

@ -0,0 +1,71 @@
/*
* Copyright © 2008 Chris Wilson
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is Chris Wilson
*
* Contributor(s):
* Chris Wilson <chris@chris-wilson.co.uk>
*/
/* This fragment implements a comb sort (specifically combsort11) */
#ifndef _HAVE_CAIRO_COMBSORT_NEWGAP
#define _HAVE_CAIRO_COMBSORT_NEWGAP
static inline unsigned int
_cairo_combsort_newgap (unsigned int gap)
{
gap = 10 * gap / 13;
if (gap == 9 || gap == 10)
gap = 11;
if (gap < 1)
gap = 1;
return gap;
}
#endif
#define CAIRO_COMBSORT_DECLARE(NAME, TYPE, CMP) \
static void \
NAME (TYPE *base, unsigned int nmemb) \
{ \
unsigned int gap = nmemb; \
unsigned int i, j; \
int swapped; \
do { \
gap = _cairo_combsort_newgap (gap); \
swapped = 0; \
for (i = 0; i < nmemb-gap ; i++) { \
j = i + gap; \
if (CMP (base[i], base[j]) > 0 ) { \
TYPE tmp; \
tmp = base[i]; \
base[i] = base[j]; \
base[j] = tmp; \
swapped = 1; \
} \
} \
} while (gap > 1 || swapped); \
}