Game-Entities

 view release on metacpan or  search on metacpan

README.md  view on Meta::CPAN

possible. Specifically, this means that it needs to be fast enough to be used
in game development, which is the natural use case for ECS designs.

To this end, the library caches component iterators which are invalidated
every time one of the components relevant to that iterator is either added
or removed from any entity. This should make the common case of systems
operating over sets of components that tend to be relatively stable
(eg. across game frames) as fast as possible.

The distribution includes two tests in its extended suite to test the
performance with iterations over large number of entities
(`xt/short-loops.t`), and many iterations over small numbers of entities
(`xt/long-loops.t`). Please refer to these files for accurate estimations.

# SEE ALSO

- [EnTT](https://skypjack.github.io/entt)

    Much of the design and API of this distribution is based on that of the
    entity registry in EnTT (famously used in Minecraft). A significant part
    of the credit for the algorithms and data structures used by Game::Entities
    falls on the EnTT developers and [the blog posts](https://skypjack.github.io)

lib/Game/Entities.pod  view on Meta::CPAN

possible. Specifically, this means that it needs to be fast enough to be used
in game development, which is the natural use case for ECS designs.

To this end, the library caches component iterators which are invalidated
every time one of the components relevant to that iterator is either added
or removed from any entity. This should make the common case of systems
operating over sets of components that tend to be relatively stable
(eg. across game frames) as fast as possible.

The distribution includes two tests in its extended suite to test the
performance with iterations over large number of entities
(C<xt/short-loops.t>), and many iterations over small numbers of entities
(C<xt/long-loops.t>). Please refer to these files for accurate estimations.

=head1 SEE ALSO

=over

=item L<EnTT|https://skypjack.github.io/entt>

Much of the design and API of this distribution is based on that of the
entity registry in EnTT (famously used in Minecraft). A significant part

xt/short-loops.t  view on Meta::CPAN

use  experimental 'signatures';

package A { sub new ($class, %args) { bless \%args, $class } }
@B::ISA = @C::ISA = 'A';

my $sets = 3;

subtest 'Baseline' => sub {
    my $start;
    my $R = Game::Entities->new;
    my $iterations = 10_000;

    diag '';
    diag 'Baseline: using an array ref directly';
    for ( 1 ..$sets ) {
        $start = time;
        my $n = 10 ** $_ - 1;
        diag "Testing $n entities";

        $R->clear;

        my $entities = [ map { $_ => [] } 0 .. ( $n - 1 ) ];

        my $elems;
        for ( 0 .. $iterations ) {
            $elems = 0;
            $elems++ for List::Util::pairs @$entities;
        }

        is $elems, $n;

        diag sprintf '    Testing %s iterations took %.3fs',
            $iterations, time - $start;
    }
};

subtest 'Short loops' => sub {
    my $R = Game::Entities->new;
    my $iterations = 10_000;

    diag '';
    diag 'Test: using a view of components';
    for ( 1 .. $sets ) {
        my $n = 10 ** $_ - 1;
        diag "Testing $n entities";

        $R->clear;

        for ( 0 .. ( $n - 1 ) ) {

xt/short-loops.t  view on Meta::CPAN

            [     $n,     qw( A     ) ],
            [ 2 * $n / 3, qw( A B   ) ],
            [     $n / 3, qw(   B C ) ],
            [     $n / 3, qw( A   C ) ],
            [     $n / 3, qw( A B C ) ],
        ) {
            my $start = time;
            my ( $want, @components ) = @$_;

            my $elems;
            for ( 0 .. $iterations ) {
                $elems = 0;
                $elems++ for @{ $R->view(@components) };
            }

            is $elems, $want;

            diag sprintf '    Testing %s iterations with %s took %.3fs',
                $iterations, join( '-', @components ), time - $start;
        }
    }
};

done_testing;



( run in 0.583 second using v1.01-cache-2.11-cpan-71847e10f99 )