Skip to content

Added cached properties implementation. #292

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 81 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
3842149
Added cached properties implementation.
jordanbriere Nov 28, 2019
d90de7c
Fixed cached generators being exhausted after the first iteration.
jordanbriere Nov 29, 2019
7aefd2a
Fixed CachedGenerator not caching all the generated values if the fir…
jordanbriere Nov 29, 2019
fe034ab
Dynamic entity functions are now cached.
jordanbriere Nov 29, 2019
af24025
Added Entity.instances cached property.
jordanbriere Nov 30, 2019
e06bde9
Entity.inputs and Entity.outputs instances are now cached.
jordanbriere Nov 30, 2019
70717da
Improved performance of Entity.<get/set>_property_<type> methods.
jordanbriere Dec 1, 2019
a4642cc
Fixed some memory leaks.
jordanbriere Dec 1, 2019
afde4a1
Fixed CachedProperty's decorators overriding the main descriptor name.
jordanbriere Dec 2, 2019
388e71e
EntityMemFuncWrapper is no longer caching itself; the Entity class al…
jordanbriere Dec 3, 2019
9c7e4a2
Fixed Player.is_bot and Player.is_hltv now being properties rather th…
jordanbriere Dec 3, 2019
9066933
Improved the caching of dynamic function wrappers.
jordanbriere Dec 3, 2019
b187391
Fixed a circular reference.
jordanbriere Dec 4, 2019
682ee74
Fixed cached properties declared on the c++ side not being managed by…
jordanbriere Dec 4, 2019
0e29616
Removed CachedProperty.owner, which was causing another circular refe…
jordanbriere Dec 4, 2019
258c105
Fixed back reference issues.
jordanbriere Dec 4, 2019
fc618e1
Fixed KeyError when invalidating the cache for field that were not pr…
jordanbriere Dec 4, 2019
de055b8
Fixed EntityDictionary potentially caching soon-to-be removed entities.
jordanbriere Dec 4, 2019
b664ef0
Added a warning to CachedProperty's documentation regarding circular …
jordanbriere Dec 4, 2019
be36626
Fixed dynamic function wrappers from no longer being documented.
jordanbriere Dec 4, 2019
7747961
Fixed Entity.__setattr__ not properly iterating over the entity's ser…
jordanbriere Dec 5, 2019
88bec54
Added "unbound" parameter to CachedProperty as a workaround for circu…
jordanbriere Dec 6, 2019
45a4060
Added back CachedProperty.owner as a weak reference.
jordanbriere Dec 6, 2019
8502e1b
Fixed Player.is_bot to be cached correctly. (#294)
CookStar Dec 11, 2019
debc63b
Fixed a crash caused by Function's convention being freed twice when …
jordanbriere Dec 14, 2019
02ea4e1
Fixed the invalidation of the internal entity cache before all entity…
jordanbriere Dec 25, 2019
a74aa97
Merge branch 'master' into cached_property
jordanbriere Jan 3, 2020
44f89f6
Added caching boolean argument for Player.from_userid.
satoon101 Jan 4, 2020
f90078c
Disabled instance caching for Entity's subclasses that do not explici…
jordanbriere Jan 9, 2020
545ef0d
Improved performance of memory tools (approximately 2.5 times faster).
jordanbriere Jan 9, 2020
31ea73a
Merge branch 'master' into bugfix_player_from_userid
jordanbriere Jan 9, 2020
f87788a
Merge branch 'bugfix_player_from_userid' into cached_property
jordanbriere Jan 9, 2020
ecab5bb
Merge branch 'master' into cached_property
jordanbriere Jan 9, 2020
7c5f43a
Merge branch 'master' into cached_property
jordanbriere Mar 11, 2020
6e8d282
Merge branch 'master' into cached_property
jordanbriere Mar 18, 2020
d8043ba
Merge branch 'master' into cached_property
jordanbriere Apr 9, 2020
5130d5c
Fixed exceptions potentially being silenced when caching generators.
jordanbriere Apr 9, 2020
abba9e7
Added missing args and kwargs parameters to CachedProperty.wrap_descr…
jordanbriere Apr 9, 2020
37c9337
Merge remote-tracking branch 'remotes/origin/master' into cached_prop…
jordanbriere Apr 10, 2020
6a6dd96
Merge remote-tracking branch 'remotes/origin/master' into cached_prop…
jordanbriere Apr 15, 2020
6f55b09
Merge branch 'master' into cached_property
jordanbriere Apr 24, 2020
274faf4
Merge branch 'master' into cached_property
jordanbriere Apr 28, 2020
3785faa
Merge branch 'master' into cached_property
jordanbriere May 1, 2020
b6ed444
Merge branch 'master' into cached_property
jordanbriere May 2, 2020
03609da
Merge branch 'master' into cached_property
jordanbriere May 2, 2020
2c309b5
Moved an extraction to avoid doing it when not necessary.
jordanbriere May 2, 2020
e9c5a82
Optimized Entity.<get/set>_property_edict methods.
jordanbriere May 2, 2020
6e84aac
Merge remote-tracking branch 'remotes/origin/master' into cached_prop…
jordanbriere May 4, 2020
fa1926b
Fixed entity delays/repeats not being cancelled if they were register…
jordanbriere May 4, 2020
3cedd95
Player.language property is now only cached for games it was already …
jordanbriere May 4, 2020
b8afdb6
Optimized non-cached get_client_language by using the internal Player…
jordanbriere May 4, 2020
24380ff
Optimized various python calls from c++.
jordanbriere May 4, 2020
ea77ce4
Optimized pointer extraction.
jordanbriere May 4, 2020
0c4248e
Fixed memory leaks caused by Sound/StreamSound instances never unload…
jordanbriere May 4, 2020
f3e8ce5
Fixed memory leaks into engine_sound.emit_sound caused by the sounds …
jordanbriere May 4, 2020
fe1422f
Merge branch 'master' into cached_property
jordanbriere May 4, 2020
95cf384
Merge branch 'master' into cached_property
jordanbriere May 5, 2020
a179e8c
Improved performance of Entity.is_in_solid's default behaviour (appro…
jordanbriere May 5, 2020
a87f121
Moved Entity.<get/set>_property_<type> methods to BaseEntity (approx.…
jordanbriere May 5, 2020
ff4ec27
Removed Entity.<get/set>_property_<type> methods.
jordanbriere May 5, 2020
8d1ed6e
Merge branch 'master' into cached_property
jordanbriere May 6, 2020
4269a14
Improved performance of TraceFilterSimple.should_hit_entity (approx. …
jordanbriere May 6, 2020
6fcf7b1
Oops, fixed a condition.
jordanbriere May 6, 2020
c613ac8
Added example to CachedProperty's docstring.
jordanbriere May 7, 2020
705a088
Improved performance of Entity.create, find and find_or_create by mov…
jordanbriere May 7, 2020
6fe0038
Fixed non-networked entities creation/lookup.
jordanbriere May 7, 2020
ec6ac90
Added an overload to memory.make_object to avoid redundant extraction.
jordanbriere May 7, 2020
bb86856
Added BaseEntity.is_marked_for_deletion (approx. 13 times faster than…
jordanbriere May 7, 2020
83e23cb
Merge branch 'master' into cached_property
jordanbriere May 8, 2020
1b67129
Fixed BaseEntity.is_marked_for_deletion from checking the wrong flag.
jordanbriere May 8, 2020
3e7b711
Moved InputFunction to c++ (~21x faster).
jordanbriere May 10, 2020
067d6ff
Added sanity check to InputFunction's constructor.
jordanbriere May 10, 2020
3809139
Removed no longer used imports into entities._base.
jordanbriere May 10, 2020
350825f
Merge branch 'master' into cached_property
jordanbriere May 14, 2020
1265508
Added cached_result decorator.
jordanbriere May 16, 2020
4cb773c
Merge branch 'master' into cached_property
jordanbriere May 16, 2020
2ce7227
Removed a redundant layer to get Entity.index.
jordanbriere May 16, 2020
79e62a7
Added CachedProperty.<get/set>_cached_value methods.
jordanbriere May 17, 2020
b9e8826
Fixed CachedProperty.<get/set>_cached_value methods from possibly cac…
jordanbriere May 19, 2020
f730a62
Improved some Entity's methods by avoiding repeated attribute retriev…
jordanbriere May 19, 2020
fe1f31c
Merge branch 'master' into cached_property
jordanbriere May 29, 2020
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
Prev Previous commit
Next Next commit
Improved performance of Entity.create, find and find_or_create by mov…
…ing them to BaseEntity directly.
  • Loading branch information
jordanbriere committed May 7, 2020
commit 705a0885646edb7f5396c6f620c2beb5f0bc8936
60 changes: 4 additions & 56 deletions addons/source-python/packages/source-python/entities/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,58 +282,6 @@ def __dir__(self):
# Return a sorted list of attributes
return sorted(attributes)

@classmethod
def create(cls, classname):
"""Create a new networked entity with the given classname.

:param str classname:
Classname of the entity to create.
:raise ValueError:
Raised if the given classname is not a networked entity.
"""
entity = BaseEntity.create(classname)
if entity.is_networked():
return cls(entity.index)

entity.remove()
raise ValueError('"{}" is not a networked entity.'.format(classname))

@classmethod
def find(cls, classname):
"""Try to find an entity with the given classname.

If not entity has been found, None will be returned.

:param str classname:
The classname of the entity.
:return:
Return the found entity.
:rtype: Entity
"""
entity = BaseEntity.find(classname)
if entity is not None and entity.is_networked():
return cls(entity.index)

return None

@classmethod
def find_or_create(cls, classname):
"""Try to find an entity with the given classname.

If no entity has been found, it will be created.

:param str classname:
The classname of the entity.
:return:
Return the found or created entity.
:rtype: Entity
"""
entity = cls.find(classname)
if entity is None:
entity = cls.create(classname)

return entity

@classmethod
def from_inthandle(cls, inthandle, caching=None):
"""Create an entity instance from an inthandle.
Expand All @@ -346,10 +294,10 @@ def from_inthandle(cls, inthandle, caching=None):
"""
return cls(index_from_inthandle(inthandle), caching=caching)

@classmethod
def _obj(cls, ptr):
"""Return an entity instance of the given pointer."""
return cls(index_from_pointer(ptr))
# @classmethod
# def _obj(cls, ptr):
# """Return an entity instance of the given pointer."""
# return cls(index_from_pointer(ptr))

def is_networked(self):
"""Return True if the entity is networked.
Expand Down
44 changes: 44 additions & 0 deletions src/core/modules/entities/entities_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ CBaseEntity* CBaseEntityWrapper::create(const char* name)
return pEntity->GetBaseEntity();
}

object CBaseEntityWrapper::create(object cls, const char *name)
{
object entity = object();
CBaseEntityWrapper *pEntity = (CBaseEntityWrapper *)create(name);
try
{
entity = cls(pEntity->GetIndex());
}
catch (...)
{
pEntity->remove();

const char *classname = extract<const char *>(cls.attr("__qualname__"));
BOOST_RAISE_EXCEPTION(
PyExc_ValueError,
"Unable to make a '%s' instance out of this '%s' entity.",
classname, name
)
}
return entity;
}

CBaseEntity* CBaseEntityWrapper::find(const char* name)
{
CBaseEntity* pEntity = (CBaseEntity *) servertools->FirstEntity();
Expand All @@ -86,6 +108,19 @@ CBaseEntity* CBaseEntityWrapper::find(const char* name)
return NULL;
}

object CBaseEntityWrapper::find(object cls, const char *name)
{
try
{
return cls(((CBaseEntityWrapper *)find(name))->GetIndex());
}
catch (...)
{
PyErr_Clear();
}
return object();
}

CBaseEntity* CBaseEntityWrapper::find_or_create(const char* name)
{
CBaseEntity* entity = find(name);
Expand All @@ -95,6 +130,15 @@ CBaseEntity* CBaseEntityWrapper::find_or_create(const char* name)
return entity;
}

object CBaseEntityWrapper::find_or_create(object cls, const char *name)
{
object entity = find(cls, name);
if (entity.is_none())
return create(cls, name);

return entity;
}

CBaseEntityOutputWrapper* CBaseEntityWrapper::get_output(const char* name)
{
// TODO: Caching?
Expand Down
3 changes: 3 additions & 0 deletions src/core/modules/entities/entities_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ class CBaseEntityWrapper: public IServerEntity
static boost::shared_ptr<CBaseEntityWrapper> __init__(unsigned int uiEntityIndex);
static boost::shared_ptr<CBaseEntityWrapper> wrap(CBaseEntity* pEntity);
static CBaseEntity* create(const char* name);
static object create(object cls, const char* name);
static CBaseEntity* find(const char* name);
static object find(object cls, const char* name);
static CBaseEntity* find_or_create(const char* name);
static object find_or_create(object cls, const char* name);

CBaseEntityOutputWrapper* get_output(const char* name);
static IEntityFactory* get_factory(const char* name);
Expand Down
27 changes: 15 additions & 12 deletions src/core/modules/entities/entities_entity_wrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,29 @@ void export_base_entity(scope _entity)
"Return True if both entities are the same."
);

BaseEntity.def("create",
&CBaseEntityWrapper::create,
return_by_value_policy(),
CLASSMETHOD(
BaseEntity,
"create",
GET_FUNCTION(object, CBaseEntityWrapper::create, object, const char *),
"Create an entity by its class name.\n\n"
":rtype: BaseEntity"
).staticmethod("create");
);

BaseEntity.def("find",
&CBaseEntityWrapper::find,
return_by_value_policy(),
CLASSMETHOD(
BaseEntity,
"find",
GET_FUNCTION(object, CBaseEntityWrapper::find, object, const char *),
"Return the first entity that has a matching class name.\n\n"
":rtype: BaseEntity"
).staticmethod("find");
);

BaseEntity.def("find_or_create",
&CBaseEntityWrapper::find_or_create,
return_by_value_policy(),
CLASSMETHOD(
BaseEntity,
"find_or_create",
GET_FUNCTION(object, CBaseEntityWrapper::find_or_create, object, const char *),
"Try to find an entity that has a matching class name. If no entity has been found, it will be created.\n\n"
":rtype: BaseEntity"
).staticmethod("find_or_create");
);

// Others
BaseEntity.def("is_player",
Expand Down