def executeInLoop(self, f, duration=.25):
        memUsage = currentMemUsageMb()

        t0 = time.time()

        while time.time() - t0 < duration:
            f()

        gc.collect()
        self.assertLess(currentMemUsageMb() - memUsage, 1.0)
    def test_serialize_dict_doesnt_leak(self):
        T = Dict(int, int)
        d = T({i: i+1 for i in range(100)})
        x = SerializationContext({})

        usage = currentMemUsageMb()
        for _ in range(20000):
            x.deserialize(x.serialize(d))

        self.assertLess(currentMemUsageMb(), usage+1)
Beispiel #3
0
    def serializeInLoop(self, objectMaker, context=None):
        context = context or SerializationContext({})
        memUsage = currentMemUsageMb()

        t0 = time.time()

        while time.time() - t0 < .25:
            data = context.serialize(objectMaker())
            context.deserialize(data)

        gc.collect()
        self.assertLess(currentMemUsageMb() - memUsage, 1.0)
    def test_string_split(self):
        @Compiled
        def c_split(s: str, sep: str, max: int) -> ListOf(str):
            r = ListOf(str)()
            s.split(r, s, sep, max)
            return r

        @Compiled
        def c_split_2(s: str) -> ListOf(str):
            r = ListOf(str)()
            s.split(r, s)
            return r

        @Compiled
        def c_split_3(s: str, sep: str) -> ListOf(str):
            r = ListOf(str)()
            s.split(r, s, sep)
            return r

        @Compiled
        def c_split_3max(s: str, max: int) -> ListOf(str):
            r = ListOf(str)()
            s.split(r, s, max)
            return r

        @Compiled
        def c_split_initialized(s: str, lst: ListOf(str)) -> ListOf(str):
            r = ListOf(str)(lst)
            s.split(r, s)
            return r

        # unexpected standard behavior:
        #   "   abc   ".split(maxsplit=0) = "abc   "  *** not "abc" nor "   abc   " ***
        split_strings = [
            "  abc  ", "ahjdfashjkdfsj ksdjkhsfhjdkf",
            "ahjdfashjkdfsj ksdjkhsfhjdkf" * 100, "", "a",
            " one two  three   \tfour    \n\nfive\r\rsix\n",
            " one two  three   \tfour    \n\nfive\r\rsix\n" * 100
        ]
        for s in split_strings:
            result = callOrExceptNoType(c_split_2, s)
            if result[0] == "Normal":
                self.assertEqual(_types.refcount(result[1]), 1)
            baseline = callOrExceptNoType(lambda: s.split())
            self.assertEqual(result, baseline, f"{s} -> {result}")
            result = callOrExceptNoType(c_split_initialized, s,
                                        ["a", "b", "c"])
            if result[0] == "Normal":
                self.assertEqual(_types.refcount(result[1]), 1)
            self.assertEqual(result, baseline, "{} -> {}".format(s, result))
            for m in range(-2, 10):
                result = callOrExceptNoType(c_split_3max, s, m)
                if result[0] == "Normal":
                    self.assertEqual(_types.refcount(result[1]), 1)
                baseline = callOrExceptNoType(lambda: s.split(maxsplit=m))
                self.assertEqual(result, baseline, f"{s},{m}-> {result}")

            for sep in ["", "j", "s", "d", "t", " ", "as", "jks"]:
                result = callOrExceptNoType(c_split_3, s, sep)
                if result[0] == "Normal":
                    self.assertEqual(_types.refcount(result[1]), 1)
                baseline = callOrExceptNoType(lambda: s.split(sep))
                self.assertEqual(result, baseline, f"{s},{sep}-> {result}")
                for m in range(-2, 10):
                    result = callOrExceptNoType(c_split, s, sep, m)
                    if result[0] == "Normal":
                        self.assertEqual(_types.refcount(result[1]), 1)
                    baseline = callOrExceptNoType(lambda: s.split(sep, m))
                    self.assertEqual(result, baseline,
                                     f"{s},{sep},{m}-> {result}")

        startusage = currentMemUsageMb()
        for i in range(100000):
            for s in split_strings:
                result = c_split_2(s)
                result = c_split(s, " ", 9)
        endusage = currentMemUsageMb()
        self.assertLess(endusage, startusage + 1)
        """