~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/xml5.py

  • Committer: John Arbash Meinel
  • Date: 2006-08-16 21:28:52 UTC
  • mto: This revision was merged to the branch mainline in revision 1942.
  • Revision ID: john@arbash-meinel.com-20060816212852-15c8a2d1fe44971d
Cache the entity escaping cuts us down to 450ms

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
from bzrlib.errors import BzrError
28
28
 
29
29
 
30
 
_unicode_to_escaped_map = {}
31
 
 
32
30
_utf8_re = None
33
31
_utf8_escape_map = {
34
32
    "&":'&',
72
70
        return "&#%d;" % ord(match.group())
73
71
 
74
72
 
 
73
_unicode_to_escaped_map = {}
 
74
 
75
75
def _encode_and_escape(unicode_str, _map=_unicode_to_escaped_map):
76
76
    """Encode the string into utf8, and escape invalid XML characters"""
77
 
    try:
78
 
        return _map[unicode_str]
79
 
    except KeyError:
 
77
    text = _map.get(unicode_str)
 
78
    if text is None:
80
79
        # The alternative policy is to do a regular UTF8 encoding
81
80
        # and then escape only XML meta characters. This could take
82
81
        # advantage of cache_utf8 since a lot of the revision ids
83
82
        # and file ids would already be cached.
84
83
        text = str(_utf8_re.sub(_utf8_escape_replace, unicode_str))
85
84
        _map[unicode_str] = text
86
 
        return text
 
85
    return text
 
86
 
 
87
 
 
88
def _clear_cache():
 
89
    """Clean out the unicode => escaped map"""
 
90
    _unicode_to_escaped_map.clear()
87
91
 
88
92
 
89
93
class Serializer_v5(Serializer):
116
120
            self._append_entry(output, ie)
117
121
        output.append('</inventory>\n')
118
122
        f.writelines(output)
 
123
        # Just to keep the cache from growing without bounds
 
124
        # but we may actually not want to do clear the cache
 
125
        _clear_cache()
119
126
 
120
127
    def _append_inventory_root(self, output, inv):
121
128
        """Append the inventory root to output."""
164
171
 
165
172
    def _append_utf8_escaped(self, output, a_string):
166
173
        """Append a_string to output as utf8."""
167
 
        #output.append(_encode_and_escape(a_string))
168
 
        text = str(_utf8_re.sub(_utf8_escape_replace, a_string))
169
 
        output.append(text)
 
174
        output.append(_encode_and_escape(a_string))
170
175
        output.append('"')
171
176
 
172
177
    def _pack_inventory(self, inv):