Skip to content

bytecode

xsdata.formats.dataclass.bytecode

MemoryBytecodeCache

Bases: BytecodeCache

In-memory jinja2 bytecode cache.

Every generator instance creates its own environment, because the filters depend on the generator config. Sharing the compiled templates across environments skips parsing and compiling them on every code generation run in the same process.

Buckets are keyed by the template name and filename, and jinja2 discards cached bytecode when the template source checksum changes.

Source code in xsdata/formats/dataclass/bytecode.py
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class MemoryBytecodeCache(BytecodeCache):
    """In-memory jinja2 bytecode cache.

    Every generator instance creates its own environment, because the
    filters depend on the generator config. Sharing the compiled
    templates across environments skips parsing and compiling them
    on every code generation run in the same process.

    Buckets are keyed by the template name and filename, and jinja2
    discards cached bytecode when the template source checksum changes.
    """

    __slots__ = ("store",)

    def __init__(self) -> None:
        self.store: dict[str, bytes] = {}

    def load_bytecode(self, bucket: Bucket) -> None:
        """Load the cached bytecode into the bucket, if any."""
        code = self.store.get(bucket.key)
        if code is not None:
            bucket.bytecode_from_string(code)

    def dump_bytecode(self, bucket: Bucket) -> None:
        """Store the bucket bytecode."""
        self.store[bucket.key] = bucket.bytecode_to_string()

    def clear(self) -> None:
        """Clear the cache."""
        self.store.clear()

load_bytecode(bucket)

Load the cached bytecode into the bucket, if any.

Source code in xsdata/formats/dataclass/bytecode.py
21
22
23
24
25
def load_bytecode(self, bucket: Bucket) -> None:
    """Load the cached bytecode into the bucket, if any."""
    code = self.store.get(bucket.key)
    if code is not None:
        bucket.bytecode_from_string(code)

dump_bytecode(bucket)

Store the bucket bytecode.

Source code in xsdata/formats/dataclass/bytecode.py
27
28
29
def dump_bytecode(self, bucket: Bucket) -> None:
    """Store the bucket bytecode."""
    self.store[bucket.key] = bucket.bytecode_to_string()

clear()

Clear the cache.

Source code in xsdata/formats/dataclass/bytecode.py
31
32
33
def clear(self) -> None:
    """Clear the cache."""
    self.store.clear()