mischief-ecs 0.2.1.0 → 0.2.2.0
raw patch · 4 files changed
+69/−19 lines, 4 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
+ Mischief.ECS.Archetypes.Graph: AllArchetypes :: ArchetypeQueryResult
+ Mischief.ECS.Archetypes.Graph: ArchetypeSet :: Set ArchetypeId -> ArchetypeQueryResult
+ Mischief.ECS.Archetypes.Graph: data ArchetypeQueryResult
+ Mischief.ECS.World.Query: filterAllArchetypes :: MonadSystem w m => QueryFilter -> m (Set ArchetypeId)
+ Mischief.ECS.World.Query: filterOnlyChanged :: MonadSystem w m => QueryFilter -> ArchetypeId -> m Bool
+ Mischief.ECS.World.Query: onlyChanged :: QueryFilter -> Bool
- Mischief.ECS.Archetypes.Graph: findMatchingArchetypes :: forall m w. MonadSystem w m => [(ComponentId, ComponentQuery)] -> m (Set ArchetypeId)
+ Mischief.ECS.Archetypes.Graph: findMatchingArchetypes :: forall m w. MonadSystem w m => [(ComponentId, ComponentQuery)] -> m ArchetypeQueryResult
- Mischief.ECS.World.Query: findArchetypes :: forall qd m w output. (Queryable qd output, MonadSystem w m) => qd -> m (Set ArchetypeId)
+ Mischief.ECS.World.Query: findArchetypes :: forall qd m w output. (Queryable qd output, MonadSystem w m) => qd -> m ArchetypeQueryResult
Files
- CHANGELOG.md +6/−1
- mischief-ecs.cabal +1/−1
- src/Mischief/ECS/Archetypes/Graph.hs +5/−3
- src/Mischief/ECS/World/Query.hs +57/−14
CHANGELOG.md view
@@ -1,6 +1,11 @@ # Revision history for mischief-ecs -## 0.2.0.1+## 0.2.2.0 ++* Performance improvements when chaining multiple Changed filters with Or. +* Performance improvements when querying for all archetypes and later filtering them. ++## 0.2.1.0 * Fixed bug related to ChildOf hierarchy traversal * Significantly improved query filter performance
mischief-ecs.cabal view
@@ -18,7 +18,7 @@ -- PVP summary: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.2.1.0+version: 0.2.2.0 -- A short (one-line) description of the package. synopsis: An archetype ECS engine built in Haskell. homepage: https://codeberg.org/mischief/mischief
src/Mischief/ECS/Archetypes/Graph.hs view
@@ -225,8 +225,10 @@ data ComponentQuery = ComponentQuery | RelationshipQueryAny | RelationshipQuery -findMatchingArchetypes :: forall m w. (MonadSystem w m) => [(ComponentId, ComponentQuery)] -> m (Set ArchetypeId)-findMatchingArchetypes [] = allArchetypes+data ArchetypeQueryResult = AllArchetypes | ArchetypeSet (Set ArchetypeId)++findMatchingArchetypes :: forall m w. (MonadSystem w m) => [(ComponentId, ComponentQuery)] -> m ArchetypeQueryResult+findMatchingArchetypes [] = pure AllArchetypes findMatchingArchetypes components = do !archetypes'' <- forM components $ \(ComponentId (# id, target #), q) -> do case q of@@ -244,7 +246,7 @@ pure $ fromMaybe Set.empty $ Map.lookup target x.pairs -- TODO: use Set.intersections from containers >=0.8- pure $ case archetypes'' of+ pure . ArchetypeSet $ case archetypes'' of [] -> Set.empty !h : !t -> foldr Set.intersection h t
src/Mischief/ECS/World/Query.hs view
@@ -23,7 +23,7 @@ import Mischief.ECS.World.Query.Queryable import Prelude hiding (and) -findArchetypes :: forall qd m w output. (Queryable qd output, MonadSystem w m) => qd -> m (Set ArchetypeId)+findArchetypes :: forall qd m w output. (Queryable qd output, MonadSystem w m) => qd -> m ArchetypeQueryResult findArchetypes !query = do !world <- unsafeGetWorld !components <-@@ -229,7 +229,10 @@ !world <- unsafeGetWorld !archetypes' <- findArchetypes qd - !archetypes <- filterArchetypes archetypes' qf+ !archetypes <- case archetypes' of+ AllArchetypes -> filterAllArchetypes qf+ ArchetypeSet s -> filterArchetypes s qf+ !x <- liftIO $ runQueryInternal (E, qd) (Set.toList archetypes) world pure $! mapMaybe (\case (_, False, _) -> Nothing; (_, True, x) -> Just x) x qrun (BuildQuery !qd !qf (Just [!entity])) = do@@ -409,6 +412,31 @@ x <- getComponentId c world.components pure $ fmap (,RelationshipQueryAny) x +filterAllArchetypes :: (MonadSystem w m) => QueryFilter -> m (Set ArchetypeId)+filterAllArchetypes NoFilter = allArchetypes+filterAllArchetypes (With x) = do+ world <- unsafeGetWorld+ f <- liftIO $ filterToQuery world x+ case f of+ Nothing -> pure Set.empty+ Just f -> archetypesWith f+filterAllArchetypes (Without x) = do+ world <- unsafeGetWorld+ f <- liftIO $ filterToQuery world x+ case f of+ Nothing -> pure Set.empty+ Just f -> archetypesWithout f+filterAllArchetypes (Changed x f1 f2) = do+ a <- allArchetypes+ filterArchetypes a (Changed x f1 f2)+filterAllArchetypes (Or a b) = do+ a' <- filterAllArchetypes a+ b' <- filterAllArchetypes b+ pure $ Set.union a' b'+filterAllArchetypes (And a b) = do+ a' <- filterAllArchetypes a+ filterArchetypes a' b+ filterArchetypes :: (MonadSystem w m) => Set ArchetypeId -> QueryFilter -> m (Set ArchetypeId) filterArchetypes prev NoFilter = pure prev filterArchetypes prev (With x) = do@@ -428,20 +456,35 @@ a <- archetypesWithout f pure $ Set.intersection prev a filterArchetypes prev (Changed x f1 f2) = do- world <- unsafeGetWorld -- TODO: use Set.filterA from containers>=0.8.1- Set.fromList- <$> filterM- ( \a -> do- let Tables tables = world.tables- table <- Vec.unsafeRead tables a.id- liftIO $ filterArchetype (Changed x f1 f2) world table- )- (Set.toList prev)+ Set.fromList <$> filterM (filterOnlyChanged (Changed x f1 f2)) (Set.toList prev) filterArchetypes prev (And x y) = do prev' <- filterArchetypes prev x filterArchetypes prev' y filterArchetypes prev (Or x y) = do- a <- filterArchetypes prev x- b <- filterArchetypes prev y- pure $ Set.union a b+ if onlyChanged (Or x y)+ then Set.fromList <$> filterM (filterOnlyChanged (Or x y)) (Set.toList prev)+ else do+ a <- filterArchetypes prev x+ b <- filterArchetypes prev y+ pure $ Set.union a b++onlyChanged :: QueryFilter -> Bool+onlyChanged NoFilter = True+onlyChanged Changed {} = True+onlyChanged (Or a b) = onlyChanged a && onlyChanged b+onlyChanged _ = False++filterOnlyChanged :: (MonadSystem w m) => QueryFilter -> ArchetypeId -> m Bool+filterOnlyChanged NoFilter _ = pure True+filterOnlyChanged (Changed x f1 f2) id = do+ world <- unsafeGetWorld+ let Tables tables = world.tables+ table <- Vec.unsafeRead tables id.id+ liftIO $ filterArchetype (Changed x f1 f2) world table+filterOnlyChanged (Or x y) id = do+ a <- filterOnlyChanged x id+ case a of+ True -> pure True+ False -> filterOnlyChanged y id+filterOnlyChanged _ _ = error "unreachable"