Skip to content

Added entity instances caching. #291

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

Merged
merged 8 commits into from
Nov 26, 2019
Merged

Added entity instances caching. #291

merged 8 commits into from
Nov 26, 2019

Conversation

jordanbriere
Copy link
Contributor

@jordanbriere jordanbriere commented Nov 25, 2019

I was curious about the feasibility of the caching @Ayuto suggested into #284 so I played a bit with the idea and ended with this implementation. Entity instances are cached by default, unless caching=False is passed to the constructor. So, if users want their private instances for whatever reason, they can do so. Ran the following code to time the difference and the results are considerable:

from time import time
from players.entity import Player

class Cached(Player):
    pass

t = time()
for i in range(1000000):
    Cached(1).name
print('Cached:', time() - t)

class NotCached(Player):
    pass

t = time()
for i in range(1000000):
    NotCached(1, caching=False).name
print('NotCached:', time() - t)

Results:

Cached: 0.9825160503387451
NotCached: 5.010047912597656

And here is the code I used to ensure instances were not broken by this implementation:

from entities.entity import Entity
from players.entity import Player
from weapons.entity import Weapon

ent = Entity(0)
assert ent is Entity(0)
ent.index
ent.origin
ent.properties

player = Player(1)
assert player is Player(1)
player.playerinfo
player.name
player.speed

weapon = Weapon._obj(player.give_named_item('weapon_m4a1'))
assert weapon is Weapon(weapon.index)
weapon.clip
weapon.ammo
weapon.index
weapon.next_attack
weapon.remove()

class MyPlayer(Player):
    @property
    def test(self):
        pass

my_player = MyPlayer(player.index)
assert my_player is not player
assert my_player is not MyPlayer(player.index, caching=False)
my_player.name
my_player.test

Copy link
Member

@Ayuto Ayuto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Looks good :)

…e accessed directly from the class, instead of an instance. Added a note about it into Entity's docstring.

Fixed the entity deletion listeners never being unregistered for subclass from plugins that are unloaded (the callbacks themselves were holding a reference of their class, preventing their garbage collection. Could use a proxy, but well; re-using our existing listener is much better anyway.).
@jordanbriere jordanbriere merged commit 872b61a into master Nov 26, 2019
@jordanbriere jordanbriere deleted the entity_caching branch November 26, 2019 00:32
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.

2 participants