Merge pull request #248 from guzba/master

sort changes, faster
This commit is contained in:
treeform 2021-07-18 21:11:59 -07:00 committed by GitHub
commit 5c11ddb900
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 23 additions and 28 deletions

View file

@ -1103,15 +1103,26 @@ proc getIndexForY(partitioning: Partitioning, y: int): uint32 {.inline.} =
partitioning.partitions.high.uint32 partitioning.partitions.high.uint32
) )
proc quickSort(a: var seq[(float32, int16)], inl, inr: int) = proc insertionSort(a: var seq[(float32, int16)], lo, hi: int) {.inline.} =
## Sorts in place + faster than standard lib sort. for i in lo + 1 .. hi:
var var
r = inr j = i - 1
l = inl k = i
let n = r - l + 1 while j >= 0 and a[j][0] > a[k][0]:
if n < 2: swap(a[j + 1], a[j])
dec j
dec k
proc sort(a: var seq[(float32, int16)], inl, inr: int) =
## Quicksort + insertion sort, in-place and faster than standard lib sort.
let n = inr - inl + 1
if n < 32:
insertionSort(a, inl, inr)
return return
let p = a[l + 3 * n div 4][0] var
l = inl
r = inr
let p = a[l + n div 2][0]
while l <= r: while l <= r:
if a[l][0] < p: if a[l][0] < p:
inc l inc l
@ -1121,18 +1132,8 @@ proc quickSort(a: var seq[(float32, int16)], inl, inr: int) =
swap(a[l], a[r]) swap(a[l], a[r])
inc l inc l
dec r dec r
quickSort(a, inl, r) sort(a, inl, r)
quickSort(a, l, inr) sort(a, l, inr)
proc insertionSort(s: var seq[(float32, int16)], hi: int) {.inline.} =
for i in 1 .. hi:
var
j = i - 1
k = i
while j >= 0 and s[j][0] > s[k][0]:
swap(s[j + 1], s[j])
dec j
dec k
proc shouldFill(windingRule: WindingRule, count: int): bool {.inline.} = proc shouldFill(windingRule: WindingRule, count: int): bool {.inline.} =
## Should we fill based on the current winding rule and count? ## Should we fill based on the current winding rule and count?
@ -1213,10 +1214,7 @@ proc computeCoverages(
hits[numHits] = (min(at.x, size.x), winding) hits[numHits] = (min(at.x, size.x), winding)
inc numHits inc numHits
if hits.len > 32: sort(hits, 0, numHits - 1)
quickSort(hits, 0, numHits - 1)
else:
insertionSort(hits, numHits - 1)
if aa: if aa:
for (prevAt, at, count) in hits.walk(numHits, windingRule, y, size): for (prevAt, at, count) in hits.walk(numHits, windingRule, y, size):
@ -1939,10 +1937,7 @@ proc overlaps(
if segment.to != at: if segment.to != at:
hits.add((at.x, winding)) hits.add((at.x, winding))
if hits.len > 32: sort(hits, 0, hits.high)
quickSort(hits, 0, hits.high)
else:
insertionSort(hits, hits.high)
var count: int var count: int
for i in 0 ..< hits.len: # For gc:arc for i in 0 ..< hits.len: # For gc:arc

Binary file not shown.

Before

Width:  |  Height:  |  Size: 982 B

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 260 KiB

After

Width:  |  Height:  |  Size: 260 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 27 KiB

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 23 KiB

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 357 KiB

After

Width:  |  Height:  |  Size: 358 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 30 KiB

After

Width:  |  Height:  |  Size: 31 KiB