Skip to content

Speed up name placement 1000x by using bitmaps to detect collisions#1

Open
makomk wants to merge 3 commits intoWorkVerbDesign:masterfrom
makomk:faster-name-placement
Open

Speed up name placement 1000x by using bitmaps to detect collisions#1
makomk wants to merge 3 commits intoWorkVerbDesign:masterfrom
makomk:faster-name-placement

Conversation

@makomk
Copy link

@makomk makomk commented Dec 29, 2018

I managed to speed the name placement up by almost 1000x whilst still using PIL, standard Python, and exactly the same completely random placement. Instead of comparing one pixel at a time, I use PIL to convert the images to bitmaps with one bit per pixel that's in use then store them as one long integer per row, meaning I can just shift and AND the integers to test a whole row at once. Poopbutt's evil twin can still sometimes cause several minutes of constipation even with this speed improvement, but it really is a lot faster and not all that much more complicated.

For context, my last run of this placed all the unplaced names in the currently-uploaded version of the database in about three minutes on my desktop, though obviously this varies a lot depending on luck.

Instead of checking for collisions one pixel at a time, convert
the occupied pixels to bitmaps stored as one long integer per line
and compare them using shift-then-and. This isn't quite a thousand
times faster, but it's close. Keep the old code as a final check.
We can get a decent speed-up when placing lots of names on an almost
empty board by updating the existing backing bitmap directly, rather
than recreating the whole thing again from the image. This is
especially worthwhile if the slower, redundant pixel-by-pixel check
for overlaps can be removed too.
@makomk
Copy link
Author

makomk commented Dec 29, 2018

Huh. It looks like the name constipation issues I was seeing were made worse by a weird bug where totalEntries wasn't actually updated for subsequent names in a batch, I think due to SQLite making the result read-consistent with the query reading the names, so they were drawn too big. Stuff works a lot better after working around that. I'd definitely suggest checking for and dealing with this before you get any more big sub bombs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant