fireplace
fireplace copied to clipboard
Re-implement selectors as filters
@djkaty @jleclanche
While we're on the topic of selector optimizations, right now the reason the selectors build up lists of entities and tear them down again is because while most selectors are just simple filters on entities, there are some that aren't, such as FuncSelector
and RandomSelector
.
This sucks, because what I'd like to do is simply (pseudo-code follows):
class Selector:
__init__(self, filter: Callable[Entity, Entity, bool])
self.filter = filter
def evaluate(self, source, entities):
return [e for e in entities if self.filter(source, e)]
and so set operations are basically free:
class AndSelector:
__init__(self, left, right)
self.filter = lambda s, e: left.filter(s, e) and right.filter(s, e)
No intermediate lists at all. As a first guess, this will lead to at least a 2-3x speedup, because selectors like FRIENDLY_MINIONS
do at least 2x the work.
This will require changing the DSL a bit though -- thoughts?
I'm good with a reimplementation to turn them into filters. The hard part is implementing selector operations (adding/removing selectors, etc).
As a sidenote, I may have mentioned this before, I've been thinking about a way to hijack random selectors in order to force a random outcome to select something specific. eg. hijack(RANDOM_ENEMY_CHARACTER, ENEMY_HERO)
would replace one selector with another. This would be immensely useful for forcing rng test outcomes without preseeding and it'd be good if a rewrite keeps this in mind.
Sounds good, I'll take a stab at doing this.
hijack
sounds tricky to implement in a safely overridable way (if we wanted hijack(RANDOM_ENEMY_CHARACTER, SELF)
to fail), but otherwise it doesn't seem too problematic.
We wouldn't want it to fail.
But then you won't catch improper use of hijack
. Shouldn't be a problem I guess, if it's just for testing.
Yeah that's completely fine.
I'm going to test it but those set operations will be far from free. A 3-way And requires 5 function calls per tested entity.
https://github.com/djkaty/fireplace/blob/work/selector-tests/tests/selector_tests_readme.md