Skip to content

Improve TypeManager.function/TypeManager.virtual_function fetch speed by using cache. #479

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
66 changes: 46 additions & 20 deletions addons/source-python/packages/source-python/memory/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,19 +619,37 @@ def virtual_function(
if return_type not in DataType.values:
return_type = self.create_converter(return_type)

# Store the function cache
funcs = {}

def fget(ptr):
"""Return the virtual function."""
# Create the virtual function
func = ptr.make_virtual_function(
index,
convention,
args,
return_type
)
# Get the vtable address
address = ptr.get_pointer().address
# Search function cache by vtable address
func = funcs.get(address, None)

if func is None:
# Create the virtual function cache it
func = ptr.make_virtual_function(
index,
convention,
args,
return_type
)
for func_cache in funcs.values():
if func_cache == func:
func = func_cache
break

funcs[address] = func

# Wrap it using MemberFunction, so we don't have to pass the this
# pointer anymore
return MemberFunction(self, return_type, func, ptr)
m_func = MemberFunction(self, return_type, func, ptr)
m_func.__doc__ = doc

return m_func

return property(fget, None, None, doc)

Expand All @@ -646,34 +664,42 @@ def function(
if return_type not in DataType.values:
return_type = self.create_converter(return_type)

# Store the function cache
func = None

class fget(object):
def __get__(fget_self, obj, cls=None):
nonlocal func
if cls is None:
if obj is None:
return fget_self
else:
cls = obj.__class__

if cls._binary is None:
raise ValueError('_binary was not specified.')
if func is None:
if cls._binary is None:
raise ValueError('_binary was not specified.')

# Create a binary object
binary = find_binary(cls._binary, cls._srv_check)
# Create a binary object
binary = find_binary(cls._binary, cls._srv_check)

# Create the function object
func = binary[identifier].make_function(
convention,
args,
return_type
)
# Create the function object and cache it
func = binary[identifier].make_function(
convention,
args,
return_type
)
func.__doc__ = doc

# Called with a this pointer?
if obj is not None:
# Wrap the function using MemberFunction, so we don't have
# to pass the this pointer anymore
func = MemberFunction(self, return_type, func, obj)
m_func = MemberFunction(self, return_type, func, obj)
m_func.__doc__ = doc

return m_func

func.__doc__ = doc
return func

return fget()
Expand Down