summaryrefslogtreecommitdiffstats
path: root/Lib/profiling/sampling/string_table.py
blob: 25c347f7ff12ade2ca2d78fe773e617381c8174f (plain)
1
2
3
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""String table implementation for memory-efficient string storage in profiling data."""

class StringTable:
    """A string table for interning strings and reducing memory usage."""

    def __init__(self):
        self._strings = []
        self._string_to_index = {}

    def intern(self, string):
        """Intern a string and return its index.

        Args:
            string: The string to intern

        Returns:
            int: The index of the string in the table
        """
        if not isinstance(string, str):
            string = str(string)

        if string in self._string_to_index:
            return self._string_to_index[string]

        index = len(self._strings)
        self._strings.append(string)
        self._string_to_index[string] = index
        return index

    def get_string(self, index):
        """Get a string by its index.

        Args:
            index: The index of the string

        Returns:
            str: The string at the given index, or empty string if invalid
        """
        if 0 <= index < len(self._strings):
            return self._strings[index]
        return ""

    def get_strings(self):
        """Get the list of all strings in the table.

        Returns:
            list: A copy of the strings list
        """
        return self._strings.copy()

    def __len__(self):
        """Return the number of strings in the table."""
        return len(self._strings)