1414# limitations under the License.
1515##############################################################################
1616
17+ import binascii
1718import json
1819import logging
1920import struct
@@ -219,6 +220,14 @@ def _getAnnotation(self, annotation, params):
219220 :param params: paging and region parameters for the annotation.
220221 :returns: a function that will return a generator.
221222 """
223+ import gc
224+
225+ # We increase the threshold2 value for python's gc, because we may
226+ # return a massive number of annotations and the garbage collection
227+ # full scan is _slow_ but probably not useful.
228+ threshold = gc .get_threshold ()
229+ if threshold [2 ] < 100 :
230+ gc .set_threshold (threshold [0 ], threshold [1 ], 100 )
222231 # Ensure that we have read access to the parent item. We could fail
223232 # faster when there are permissions issues if we didn't load the
224233 # annotation elements before checking the item access permissions.
@@ -245,6 +254,7 @@ def generateResult():
245254 if centroids :
246255 # Add a null byte to indicate the start of the binary data
247256 yield b'\x00 '
257+ packing = struct .Struct ('<fffl' )
248258 for element in Annotationelement ().yieldElements (annotation , params , info ):
249259 # The json conversion is fastest if we use defaults as much as
250260 # possible. The only value in an annotation element that needs
@@ -261,9 +271,9 @@ def generateResult():
261271 [int (x ) if isinstance (x , float ) and x .is_integer () else x for x in sub ]
262272 for sub in hole ] for hole in element ['holes' ]]
263273 else :
264- element = struct . pack (
265- '>QL' , int (element [0 ][: 16 ], 16 ), int (element [0 ][ 16 : 24 ], 16 ),
266- ) + struct .pack ('<fffl' , * element [1 :])
274+ element = (
275+ binascii . unhexlify (element [0 ]) if isinstance (element [0 ], str ) else
276+ element [ 0 ]. binary ) + packing .pack (* element [1 :])
267277 # Use orjson; it is much faster. The standard json library
268278 # could be used in its most default mode instead like so:
269279 # result = json.dumps(element, separators=(',', ':'))
0 commit comments