3737from ..stream import READ_BACKWARD
3838
3939
40- def tc_map (key : bytes , _memo_map : dict = {}):
40+ _tc_map = {}
41+
42+
43+ def tc_map (key : bytes ):
4144 """
4245 Returns a default parser/generator class for the given type code.
4346
@@ -50,7 +53,8 @@ def tc_map(key: bytes, _memo_map: dict = {}):
5053 of the “type code-type class” mapping,
5154 :return: parser/generator class for the type code.
5255 """
53- if not _memo_map :
56+ global _tc_map
57+ if not _tc_map :
5458 from pygridgain .datatypes import (
5559 Null , ByteObject , ShortObject , IntObject , LongObject , FloatObject ,
5660 DoubleObject , CharObject , BoolObject , UUIDObject , DateObject ,
@@ -65,7 +69,7 @@ def tc_map(key: bytes, _memo_map: dict = {}):
6569 MapObject , BinaryObject , WrappedDataObject ,
6670 )
6771
68- _memo_map = {
72+ _tc_map = {
6973 TC_NULL : Null ,
7074
7175 TC_BYTE : ByteObject ,
@@ -111,7 +115,7 @@ def tc_map(key: bytes, _memo_map: dict = {}):
111115 TC_COMPLEX_OBJECT : BinaryObject ,
112116 TC_ARRAY_WRAPPED_OBJECTS : WrappedDataObject ,
113117 }
114- return _memo_map [key ]
118+ return _tc_map [key ]
115119
116120
117121class Conditional :
@@ -184,7 +188,7 @@ async def parse_async(self, stream):
184188 def __parse_length (self , stream ):
185189 counter_type_len = ctypes .sizeof (self .counter_type )
186190 length = int .from_bytes (
187- stream .mem_view (offset = counter_type_len ),
191+ stream .slice (offset = counter_type_len ),
188192 byteorder = PROTOCOL_BYTE_ORDER
189193 )
190194 stream .seek (counter_type_len , SEEK_CUR )
@@ -349,6 +353,9 @@ class AnyDataObject:
349353 """
350354 _python_map = None
351355 _python_array_map = None
356+ _map_obj_type = None
357+ _collection_obj_type = None
358+ _binary_obj_type = None
352359
353360 @staticmethod
354361 def get_subtype (iterable , allow_none = False ):
@@ -392,7 +399,7 @@ async def parse_async(cls, stream):
392399
393400 @classmethod
394401 def __data_class_parse (cls , stream ):
395- type_code = bytes ( stream .mem_view (offset = ctypes .sizeof (ctypes .c_byte ) ))
402+ type_code = stream .slice (offset = ctypes .sizeof (ctypes .c_byte ))
396403 try :
397404 return tc_map (type_code )
398405 except KeyError :
@@ -417,15 +424,17 @@ def __data_class_from_ctype(cls, ctype_object):
417424 return tc_map (type_code )
418425
419426 @classmethod
420- def _init_python_map (cls ):
427+ def _init_python_mapping (cls ):
421428 """
422429 Optimizes Python types→GridGain types map creation for speed.
423430
424431 Local imports seem inevitable here.
425432 """
426433 from pygridgain .datatypes import (
427- LongObject , DoubleObject , String , BoolObject , Null , UUIDObject ,
428- DateObject , TimeObject , DecimalObject , ByteArrayObject ,
434+ LongObject , DoubleObject , String , BoolObject , Null , UUIDObject , DateObject , TimeObject ,
435+ DecimalObject , ByteArrayObject , LongArrayObject , DoubleArrayObject , StringArrayObject ,
436+ BoolArrayObject , UUIDArrayObject , DateArrayObject , TimeArrayObject , DecimalArrayObject ,
437+ MapObject , CollectionObject , BinaryObject
429438 )
430439
431440 cls ._python_map = {
@@ -443,17 +452,6 @@ def _init_python_map(cls):
443452 decimal .Decimal : DecimalObject ,
444453 }
445454
446- @classmethod
447- def _init_python_array_map (cls ):
448- """
449- Optimizes Python types→GridGain array types map creation for speed.
450- """
451- from pygridgain .datatypes import (
452- LongArrayObject , DoubleArrayObject , StringArrayObject ,
453- BoolArrayObject , UUIDArrayObject , DateArrayObject , TimeArrayObject ,
454- DecimalArrayObject ,
455- )
456-
457455 cls ._python_array_map = {
458456 int : LongArrayObject ,
459457 float : DoubleArrayObject ,
@@ -467,18 +465,20 @@ def _init_python_array_map(cls):
467465 decimal .Decimal : DecimalArrayObject ,
468466 }
469467
468+ cls ._map_obj_type = MapObject
469+ cls ._collection_obj_type = CollectionObject
470+ cls ._binary_obj_type = BinaryObject
471+
470472 @classmethod
471473 def map_python_type (cls , value ):
472- from pygridgain .datatypes import (
473- MapObject , CollectionObject , BinaryObject ,
474- )
475-
476- if cls ._python_map is None :
477- cls ._init_python_map ()
478- if cls ._python_array_map is None :
479- cls ._init_python_array_map ()
474+ if cls ._python_map is None or cls ._python_array_map is None :
475+ cls ._init_python_mapping ()
480476
481477 value_type = type (value )
478+
479+ if value_type in cls ._python_map :
480+ return cls ._python_map [value_type ]
481+
482482 if is_iterable (value ) and value_type not in (str , bytearray , bytes ):
483483 value_subtype = cls .get_subtype (value )
484484 if value_subtype in cls ._python_array_map :
@@ -491,15 +491,15 @@ def map_python_type(cls, value):
491491 isinstance (value [0 ], int ),
492492 isinstance (value [1 ], dict ),
493493 ]):
494- return MapObject
494+ return cls . _map_obj_type
495495
496496 if all ([
497497 value_subtype is None ,
498498 len (value ) == 2 ,
499499 isinstance (value [0 ], int ),
500500 is_iterable (value [1 ]),
501501 ]):
502- return CollectionObject
502+ return cls . _collection_obj_type
503503
504504 # no default for ObjectArrayObject, sorry
505505
@@ -508,10 +508,8 @@ def map_python_type(cls, value):
508508 )
509509
510510 if is_binary (value ):
511- return BinaryObject
511+ return cls . _binary_obj_type
512512
513- if value_type in cls ._python_map :
514- return cls ._python_map [value_type ]
515513 raise TypeError (
516514 'Type `{}` is invalid.' .format (value_type )
517515 )
0 commit comments