Skip to content

Commit 225db43

Browse files
authored
fixes for performance (#1669)
1 parent ba49766 commit 225db43

File tree

4 files changed

+185
-167
lines changed

4 files changed

+185
-167
lines changed

src/ansys/dpf/core/any.py

Lines changed: 78 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _new_from_string_as_bytes_on_client(self, client, str):
8989
else:
9090
return self._api.any_new_from_string_on_client(client, str)
9191

92-
def _type_to_new_from_get_as_method(self):
92+
def _type_to_new_from_get_as_method(self, obj):
9393
from ansys.dpf.core import (
9494
field,
9595
property_field,
@@ -100,74 +100,72 @@ def _type_to_new_from_get_as_method(self):
100100
custom_type_field,
101101
collection,
102102
)
103-
104-
return [
105-
(
106-
int,
103+
if issubclass(obj, int):
104+
return (
107105
self._api.any_new_from_int,
108106
self._api.any_get_as_int,
109107
self._api.any_new_from_int_on_client,
110-
),
111-
(
112-
str,
108+
)
109+
elif issubclass(obj, str):
110+
return (
113111
self._new_from_string,
114112
self._get_as_string,
115113
self._new_from_string_on_client,
116-
),
117-
(
118-
float,
114+
)
115+
elif issubclass(obj, float):
116+
return (
119117
self._api.any_new_from_double,
120118
self._api.any_get_as_double,
121119
self._api.any_new_from_double_on_client,
122-
),
123-
(
124-
bytes,
120+
)
121+
elif issubclass(obj, bytes):
122+
return (
125123
self._new_from_string_as_bytes,
126124
self._get_as_string_as_bytes,
127125
self._new_from_string_as_bytes_on_client,
128-
),
129-
(field.Field, self._api.any_new_from_field, self._api.any_get_as_field),
130-
(
131-
property_field.PropertyField,
126+
)
127+
elif issubclass(obj, field.Field):
128+
return self._api.any_new_from_field, self._api.any_get_as_field
129+
elif issubclass(obj, property_field.PropertyField):
130+
return (
132131
self._api.any_new_from_property_field,
133132
self._api.any_get_as_property_field,
134-
),
135-
(
136-
string_field.StringField,
133+
)
134+
elif issubclass(obj, string_field.StringField):
135+
return (
137136
self._api.any_new_from_string_field,
138137
self._api.any_get_as_string_field,
139-
),
140-
(
141-
generic_data_container.GenericDataContainer,
138+
)
139+
elif issubclass(obj, generic_data_container.GenericDataContainer):
140+
return (
142141
self._api.any_new_from_generic_data_container,
143142
self._api.any_get_as_generic_data_container,
144-
),
145-
(
146-
scoping.Scoping,
143+
)
144+
elif issubclass(obj, scoping.Scoping):
145+
return (
147146
self._api.any_new_from_scoping,
148147
self._api.any_get_as_scoping,
149-
),
150-
(
151-
data_tree.DataTree,
148+
)
149+
elif issubclass(obj, data_tree.DataTree):
150+
return (
152151
self._api.any_new_from_data_tree,
153152
self._api.any_get_as_data_tree,
154-
),
155-
(
156-
custom_type_field.CustomTypeField,
153+
)
154+
elif issubclass(obj, custom_type_field.CustomTypeField):
155+
return (
157156
self._api.any_new_from_custom_type_field,
158157
self._api.any_get_as_custom_type_field,
159-
),
160-
(
161-
collection.Collection,
158+
)
159+
elif issubclass(obj, collection.Collection):
160+
return (
162161
self._api.any_new_from_any_collection,
163162
self._api.any_get_as_any_collection,
164-
),
165-
(
166-
dpf_vector.DPFVectorInt,
163+
)
164+
elif issubclass(obj, dpf_vector.DPFVectorInt):
165+
return (
167166
self._api.any_new_from_int_collection,
168167
self._api.any_get_as_int_collection,
169-
),
170-
]
168+
)
171169

172170
@staticmethod
173171
def new_from(obj, server=None):
@@ -190,30 +188,31 @@ def new_from(obj, server=None):
190188

191189
any_dpf = Any(server=inner_server)
192190

193-
for type_tuple in any_dpf._type_to_new_from_get_as_method():
194-
if isinstance(obj, type_tuple[0]):
195-
# call respective new_from function
196-
if isinstance(server, ansys.dpf.core.server_types.InProcessServer) or not (
197-
isinstance(obj, (int, str, float, bytes))
198-
):
199-
any_dpf._internal_obj = type_tuple[1](obj)
200-
else:
201-
any_dpf._internal_obj = type_tuple[3](inner_server.client, obj)
202-
# store get_as & type for casting back to original type
203-
any_dpf._internal_type = type_tuple[0]
204-
any_dpf._get_as_method = type_tuple[2]
205-
191+
type_tuple = any_dpf._type_to_new_from_get_as_method(type(obj))
192+
if type_tuple is not None:
193+
# call respective new_from function
194+
if isinstance(server, ansys.dpf.core.server_types.InProcessServer) or not (
195+
isinstance(obj, (int, str, float, bytes))
196+
):
197+
any_dpf._internal_obj = type_tuple[0](obj)
198+
else:
199+
any_dpf._internal_obj = type_tuple[2](inner_server.client, obj)
200+
# store get_as & type for casting back to original type
201+
any_dpf._internal_type = type(obj)
202+
any_dpf._get_as_method = type_tuple[1]
203+
204+
return any_dpf
205+
elif isinstance(obj, (list, np.ndarray)):
206+
type_tuple = any_dpf._type_to_new_from_get_as_method(dpf_vector.DPFVectorInt)
207+
from ansys.dpf.core import collection
208+
if server_meet_version_and_raise("9.0", inner_server, "Creating an Any from a list is only supported "
209+
"with"
210+
"server versions starting at 9.0"):
211+
inpt = collection.CollectionBase.integral_collection(obj, inner_server)
212+
any_dpf._internal_obj = type_tuple[0](inpt)
213+
any_dpf._internal_type = dpf_vector.DPFVectorInt
214+
any_dpf._get_as_method = type_tuple[1]
206215
return any_dpf
207-
if isinstance(obj, (list, np.ndarray)) and type_tuple[0]==dpf_vector.DPFVectorInt:
208-
from ansys.dpf.core import collection
209-
if server_meet_version_and_raise("9.0", inner_server, "Creating an Any from a list is only supported "
210-
"with"
211-
"server versions starting at 9.0"):
212-
inpt = collection.CollectionBase.integral_collection(obj, inner_server)
213-
any_dpf._internal_obj = type_tuple[1](inpt)
214-
any_dpf._internal_type = type_tuple[0]
215-
any_dpf._get_as_method = type_tuple[2]
216-
return any_dpf
217216

218217
raise TypeError(f"{obj.__class__} is not currently supported by the Any class.")
219218

@@ -258,22 +257,21 @@ def cast(self, output_type=None):
258257

259258
self._internal_type = output_type if output_type is not None else self._internal_type
260259

261-
for type_tuple in Any._type_to_new_from_get_as_method(self):
262-
if issubclass(self._internal_type, type_tuple[0]):
263-
# call the get_as function for the appropriate type
264-
internal_obj = type_tuple[2](self)
265-
if (
266-
self._internal_type is int
267-
or self._internal_type is str
268-
or self._internal_type is float
269-
or self._internal_type is bytes
270-
271-
):
272-
obj = internal_obj
273-
else:
274-
return create_dpf_instance(self._internal_type, internal_obj, self._server)
275-
276-
return obj
260+
type_tuple = self._type_to_new_from_get_as_method(self._internal_type)
261+
if type_tuple is not None:
262+
internal_obj = type_tuple[1](self)
263+
if (
264+
self._internal_type is int
265+
or self._internal_type is str
266+
or self._internal_type is float
267+
or self._internal_type is bytes
268+
269+
):
270+
obj = internal_obj
271+
else:
272+
return create_dpf_instance(self._internal_type, internal_obj, self._server)
273+
274+
return obj
277275

278276
raise TypeError(f"{output_type} is not currently supported by the Any class.")
279277

src/ansys/dpf/core/common.py

Lines changed: 76 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -319,74 +319,87 @@ def _common_percentage_progress_bar(text):
319319
return TqdmProgressBar(text, "%", 100)
320320

321321

322+
class SubClassSmartDict(dict):
323+
def __getitem__(self, item):
324+
"""If found returns the item of key == ìtem`, else returns item with key matching `issubclass(item,
325+
key)`."""
326+
if item in self:
327+
return super().__getitem__(item)
328+
else:
329+
for key, value in self.items():
330+
if issubclass(item, key):
331+
return value
332+
raise KeyError
333+
334+
335+
_type_to_internal_object_keyword = None
336+
337+
322338
def type_to_internal_object_keyword():
323-
from ansys.dpf.core import (
324-
cyclic_support,
325-
data_sources,
326-
field,
327-
fields_container,
328-
meshed_region,
329-
meshes_container,
330-
property_field,
331-
string_field,
332-
custom_type_field,
333-
result_info,
334-
scoping,
335-
scopings_container,
336-
time_freq_support,
337-
dpf_operator,
338-
data_tree,
339-
workflow,
340-
streams_container,
341-
generic_data_container,
342-
any,
343-
collection,
344-
)
339+
global _type_to_internal_object_keyword
340+
if _type_to_internal_object_keyword is None:
341+
from ansys.dpf.core import (
342+
cyclic_support,
343+
data_sources,
344+
field,
345+
fields_container,
346+
meshed_region,
347+
meshes_container,
348+
property_field,
349+
string_field,
350+
custom_type_field,
351+
result_info,
352+
scoping,
353+
scopings_container,
354+
time_freq_support,
355+
dpf_operator,
356+
data_tree,
357+
workflow,
358+
streams_container,
359+
generic_data_container,
360+
any,
361+
collection,
362+
)
345363

346-
class _smart_dict_types(dict):
347-
def __getitem__(self, item):
348-
"""If found returns the item of key == ìtem`, else returns item with key matching `issubclass(item,
349-
key)`."""
350-
if item in self:
351-
return super().__getitem__(item)
352-
else:
353-
for key, value in self.items():
354-
if issubclass(item, key):
355-
return value
356-
raise KeyError
357-
358-
return _smart_dict_types({
359-
field.Field: "field",
360-
property_field.PropertyField: "property_field",
361-
string_field.StringField: "string_field",
362-
custom_type_field.CustomTypeField: "field",
363-
scoping.Scoping: "scoping",
364-
fields_container.FieldsContainer: "fields_container",
365-
scopings_container.ScopingsContainer: "scopings_container",
366-
meshes_container.MeshesContainer: "meshes_container",
367-
streams_container.StreamsContainer: "streams_container",
368-
data_sources.DataSources: "data_sources",
369-
cyclic_support.CyclicSupport: "cyclic_support",
370-
meshed_region.MeshedRegion: "mesh",
371-
result_info.ResultInfo: "result_info",
372-
time_freq_support.TimeFreqSupport: "time_freq_support",
373-
workflow.Workflow: "workflow",
374-
data_tree.DataTree: "data_tree",
375-
dpf_operator.Operator: "operator",
376-
generic_data_container.GenericDataContainer: "generic_data_container",
377-
any.Any: "any_dpf",
378-
collection.Collection: "collection",
379-
})
364+
_type_to_internal_object_keyword = SubClassSmartDict({
365+
field.Field: "field",
366+
property_field.PropertyField: "property_field",
367+
string_field.StringField: "string_field",
368+
custom_type_field.CustomTypeField: "field",
369+
scoping.Scoping: "scoping",
370+
fields_container.FieldsContainer: "fields_container",
371+
scopings_container.ScopingsContainer: "scopings_container",
372+
meshes_container.MeshesContainer: "meshes_container",
373+
streams_container.StreamsContainer: "streams_container",
374+
data_sources.DataSources: "data_sources",
375+
cyclic_support.CyclicSupport: "cyclic_support",
376+
meshed_region.MeshedRegion: "mesh",
377+
result_info.ResultInfo: "result_info",
378+
time_freq_support.TimeFreqSupport: "time_freq_support",
379+
workflow.Workflow: "workflow",
380+
data_tree.DataTree: "data_tree",
381+
dpf_operator.Operator: "operator",
382+
generic_data_container.GenericDataContainer: "generic_data_container",
383+
any.Any: "any_dpf",
384+
collection.Collection: "collection",
385+
})
386+
return _type_to_internal_object_keyword
387+
388+
389+
_type_to_special_dpf_constructors = None
380390

381391

382392
def type_to_special_dpf_constructors():
383-
from ansys.dpf.gate.dpf_vector import DPFVectorInt
384-
from ansys.dpf.core import collection_base
385-
return {DPFVectorInt:
386-
lambda obj, server: collection_base.IntCollection(
387-
server=server, collection=obj
388-
).get_integral_entries()
389-
}
393+
global _type_to_special_dpf_constructors
394+
if _type_to_special_dpf_constructors is None:
395+
from ansys.dpf.gate.dpf_vector import DPFVectorInt
396+
from ansys.dpf.core import collection_base
397+
_type_to_special_dpf_constructors = {DPFVectorInt:
398+
lambda obj, server: collection_base.IntCollection(
399+
server=server, collection=obj
400+
).get_integral_entries()
401+
}
402+
return _type_to_special_dpf_constructors
390403

391404

392405
def create_dpf_instance(type, internal_obj, server):

0 commit comments

Comments
 (0)