Skip to content

Commit 2490097

Browse files
authored
Port insertion sort improvement of libgd 2.0.26 (GH-17342)
Possibly a minor performance improvement, but at least in sync with upstream this way.
1 parent 209e0d6 commit 2490097

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

ext/gd/libgd/gd.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,8 +2723,6 @@ void gdImageOpenPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
27232723
}
27242724
}
27252725

2726-
int gdCompareInt (const void *a, const void *b);
2727-
27282726
/* THANKS to Kirsten Schulz for the polygon fixes! */
27292727

27302728
/* The intersection finding technique of this code could be improved
@@ -2736,6 +2734,8 @@ int gdCompareInt (const void *a, const void *b);
27362734
void gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
27372735
{
27382736
int i;
2737+
int j;
2738+
int index;
27392739
int y;
27402740
int miny, maxy, pmaxy;
27412741
int x1, y1;
@@ -2840,8 +2840,21 @@ void gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
28402840
im->polyInts[ints++] = x2;
28412841
}
28422842
}
2843-
qsort(im->polyInts, ints, sizeof(int), gdCompareInt);
2844-
2843+
/*
2844+
2.0.26: polygons pretty much always have less than 100 points,
2845+
and most of the time they have considerably less. For such trivial
2846+
cases, insertion sort is a good choice. Also a good choice for
2847+
future implementations that may wish to indirect through a table.
2848+
*/
2849+
for (i = 1; (i < ints); i++) {
2850+
index = im->polyInts[i];
2851+
j = i;
2852+
while ((j > 0) && (im->polyInts[j - 1] > index)) {
2853+
im->polyInts[j] = im->polyInts[j - 1];
2854+
j--;
2855+
}
2856+
im->polyInts[j] = index;
2857+
}
28452858
for (i = 0; i < ints - 1; i += 2) {
28462859
gdImageLine(im, im->polyInts[i], y, im->polyInts[i + 1], y, fill_color);
28472860
}
@@ -2853,11 +2866,6 @@ void gdImageFilledPolygon (gdImagePtr im, gdPointPtr p, int n, int c)
28532866
}
28542867
}
28552868

2856-
int gdCompareInt (const void *a, const void *b)
2857-
{
2858-
return (*(const int *) a) - (*(const int *) b);
2859-
}
2860-
28612869
void gdImageSetStyle (gdImagePtr im, int *style, int noOfPixels)
28622870
{
28632871
if (im->style) {

0 commit comments

Comments
 (0)