From 294be25164601332ba1c960967117d1d34e7b3c5 Mon Sep 17 00:00:00 2001 From: Sean McLeod Date: Sat, 2 Dec 2023 21:30:15 +0200 Subject: [PATCH 1/5] Use cached PropertyNodes in python API for property reads --- python/jsbsim.pyx.in | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/python/jsbsim.pyx.in b/python/jsbsim.pyx.in index 4a8c2144f7..f35895e408 100644 --- a/python/jsbsim.pyx.in +++ b/python/jsbsim.pyx.in @@ -701,6 +701,9 @@ cdef class FGFDMExec(FGJSBBase): self.set_aircraft_path("aircraft") self.set_systems_path("systems") + # Dictionary cache of property nodes + self.properties = { } + def __dealloc__(self) -> None: del self.thisptr @@ -720,10 +723,17 @@ cdef class FGFDMExec(FGJSBBase): def __getitem__(self, key: str) -> float: _key = key.strip() - pm = self.get_property_manager() - if not pm.hasNode(_key): - raise KeyError("No property named {}".format(_key)) - return self.get_property_value(_key) + try: + property_node = self.properties[_key] + return property_node.get_double_value() + except KeyError: + pm = self.get_property_manager() + property_node = pm.get_node(_key) + if property_node is not None: + self.properties[_key] = property_node + return property_node.get_double_value() + else: + raise KeyError(f'No property named {_key}') def __setitem__(self, key: str, value: float) -> None: self.set_property_value(key.strip(), value) From 1eb21010812ca538057e226e576d83da8dc307be Mon Sep 17 00:00:00 2001 From: Sean McLeod Date: Sat, 2 Dec 2023 22:15:16 +0200 Subject: [PATCH 2/5] Create Python properties dictionary in __init__ not __cinit__ --- python/jsbsim.pyx.in | 1 + 1 file changed, 1 insertion(+) diff --git a/python/jsbsim.pyx.in b/python/jsbsim.pyx.in index f35895e408..83e5f50072 100644 --- a/python/jsbsim.pyx.in +++ b/python/jsbsim.pyx.in @@ -701,6 +701,7 @@ cdef class FGFDMExec(FGJSBBase): self.set_aircraft_path("aircraft") self.set_systems_path("systems") + def __init__(self, *args, **kwargs): # Dictionary cache of property nodes self.properties = { } From 7ba12bb075f83e7a44368580485ba54e5869fd3b Mon Sep 17 00:00:00 2001 From: Sean McLeod Date: Sat, 2 Dec 2023 22:48:07 +0200 Subject: [PATCH 3/5] Declare properties dictionary using cdef --- python/jsbsim.pyx.in | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/python/jsbsim.pyx.in b/python/jsbsim.pyx.in index 83e5f50072..be478687ec 100644 --- a/python/jsbsim.pyx.in +++ b/python/jsbsim.pyx.in @@ -674,6 +674,7 @@ cdef class FGFDMExec(FGJSBBase): """@Dox(JSBSim::FGFDMExec)""" cdef c_FGFDMExec *thisptr # hold a C++ instance which we're wrapping + cdef dict properties # Dictionary cache of property nodes def __cinit__(self, root_dir, FGPropertyManager pm_root=None, *args, **kwargs): @@ -701,10 +702,6 @@ cdef class FGFDMExec(FGJSBBase): self.set_aircraft_path("aircraft") self.set_systems_path("systems") - def __init__(self, *args, **kwargs): - # Dictionary cache of property nodes - self.properties = { } - def __dealloc__(self) -> None: del self.thisptr From 5eee956204da0308929cb54b0ff0fed3d354505b Mon Sep 17 00:00:00 2001 From: Sean McLeod Date: Sat, 2 Dec 2023 23:02:48 +0200 Subject: [PATCH 4/5] Initialise properties dictionary --- python/jsbsim.pyx.in | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/jsbsim.pyx.in b/python/jsbsim.pyx.in index be478687ec..ad1bd2cd34 100644 --- a/python/jsbsim.pyx.in +++ b/python/jsbsim.pyx.in @@ -702,6 +702,8 @@ cdef class FGFDMExec(FGJSBBase): self.set_aircraft_path("aircraft") self.set_systems_path("systems") + self.properties = { } + def __dealloc__(self) -> None: del self.thisptr From ce47e42fd9f36fb4f9a13a4f6a79d1a93ab44408 Mon Sep 17 00:00:00 2001 From: Sean McLeod Date: Sun, 3 Dec 2023 14:54:14 +0200 Subject: [PATCH 5/5] Rename dictionary from properties to properties_cache --- python/jsbsim.pyx.in | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/python/jsbsim.pyx.in b/python/jsbsim.pyx.in index ad1bd2cd34..3344bcc3b2 100644 --- a/python/jsbsim.pyx.in +++ b/python/jsbsim.pyx.in @@ -674,7 +674,7 @@ cdef class FGFDMExec(FGJSBBase): """@Dox(JSBSim::FGFDMExec)""" cdef c_FGFDMExec *thisptr # hold a C++ instance which we're wrapping - cdef dict properties # Dictionary cache of property nodes + cdef dict properties_cache # Dictionary cache of property nodes def __cinit__(self, root_dir, FGPropertyManager pm_root=None, *args, **kwargs): @@ -702,7 +702,7 @@ cdef class FGFDMExec(FGJSBBase): self.set_aircraft_path("aircraft") self.set_systems_path("systems") - self.properties = { } + self.properties_cache = { } def __dealloc__(self) -> None: del self.thisptr @@ -724,13 +724,13 @@ cdef class FGFDMExec(FGJSBBase): def __getitem__(self, key: str) -> float: _key = key.strip() try: - property_node = self.properties[_key] + property_node = self.properties_cache[_key] return property_node.get_double_value() except KeyError: pm = self.get_property_manager() property_node = pm.get_node(_key) if property_node is not None: - self.properties[_key] = property_node + self.properties_cache[_key] = property_node return property_node.get_double_value() else: raise KeyError(f'No property named {_key}')