Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve performance when scanning for quests #4469

Merged
merged 3 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ElementFilterExpression(
internal val elementExprRoot: BooleanExpression<ElementFilter, Element>?
) {
/* Performance improvement: Allows to skip early on elements that have no tags at all */
private val mayEvaluateToTrueWithNoTags = elementExprRoot?.mayEvaluateToTrueWithNoTags ?: true
val mayEvaluateToTrueWithNoTags = elementExprRoot?.mayEvaluateToTrueWithNoTags ?: true

/** returns whether the given element is found through (=matches) this expression */
fun matches(element: Element): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import de.westnordost.streetcomplete.data.osm.mapdata.Element
import de.westnordost.streetcomplete.data.osm.mapdata.ElementKey
import de.westnordost.streetcomplete.data.osm.mapdata.LatLon
import de.westnordost.streetcomplete.data.osm.mapdata.MapDataWithGeometry
import de.westnordost.streetcomplete.data.osm.mapdata.MutableMapDataWithGeometry
import de.westnordost.streetcomplete.data.osmnotes.Note
import de.westnordost.streetcomplete.data.osmnotes.edits.NotesWithEditsSource
import de.westnordost.streetcomplete.data.quest.OsmQuestKey
Expand Down Expand Up @@ -146,6 +147,12 @@ class OsmQuestController internal constructor(

val countryBoundaries = countryBoundariesFuture.get()

// Remove elements without tags, to be used for quests that are never applicable without
// tags. These quests are usually OsmFilterQuestType, where questType.filter.mayEvaluateToTrueWithNoTags
// guarantees we can skip elements without tags completely. Also those quests don't use geometry.
// This shortcut reduces time for creating quests by ~15-30%.
val onlyElementsWithTags = MutableMapDataWithGeometry(mapDataWithGeometry.filter { it.tags.isNotEmpty() }, emptyList())

val deferredQuests: List<Deferred<List<OsmQuest>>> = questTypes.map { questType ->
scope.async {
val questsForType = ArrayList<OsmQuest>()
Expand All @@ -156,7 +163,11 @@ class OsmQuestController internal constructor(
} else {
val questTime = currentTimeMillis()
var questCount = 0
for (element in questType.getApplicableElements(mapDataWithGeometry)) {
val mapDataToUse = if (questType is OsmFilterQuestType && !questType.filter.mayEvaluateToTrueWithNoTags)
onlyElementsWithTags
else
mapDataWithGeometry
for (element in questType.getApplicableElements(mapDataToUse)) {
val geometry = mapDataWithGeometry.getGeometry(element.type, element.id)
?: continue
if (!mayCreateQuest(questType, geometry, bbox)) continue
Expand Down