Project

General

Profile

« Previous | Next » 

Revision 1294

xpath.py: Removed unnecessary copy.deepcopy()'s and instead changed set_value() and set_id() to make copies of any elements they change. This should result in up to a 17% speed increase in the import, because deepcopy() was taking a lot of time. Added documentation to set_value() and set_id() that caller must make a shallow copy of the path to prevent modifications from propagating to other copies of the path. (Previously, a deep copy was needed, but there was no comment specifying this.)

View differences:

lib/xpath.py
53 53

  
54 54
def value(path): return path[-1].value
55 55

  
56
def set_value(path, value): path[-1].value = value
56
def set_value(path, value):
57
    '''Caller must make a shallow copy of the path to prevent modifications from
58
    propagating to other copies of the path (a deep copy is not needed)'''
59
    path[-1] = copy.copy(path[-1]) # don't modify other copies of the path
60
    path[-1].value = value
57 61

  
58 62
def backward_id(elem):
59 63
    if len(elem.keys) >= 1 and value(elem.keys[0]) == None: return elem.keys[0]
......
67 71
    return obj_path
68 72

  
69 73
def set_id(path, id_, has_types=True):
74
    '''Caller must make a shallow copy of the path to prevent modifications from
75
    propagating to other copies of the path (a deep copy is not needed)'''
70 76
    if has_types: id_level = instance_level
71 77
    else: id_level = 0 # root's children
72 78
    if is_self(path[0]): id_level += 1 # explicit root element
73
    path[id_level].keys.append([XpathElem('id', id_, True)])
79
    
80
    id_elem = path[id_level] = copy.copy(path[id_level])
81
        # don't modify other copies of the path
82
    id_elem.keys = id_elem.keys[:] # don't modify other copies of the elem
83
    id_elem.keys.append([XpathElem('id', id_, True)])
74 84

  
75 85
def is_id(path): return path[0].is_attr and path[0].name == 'id'
76 86

  
......
281 291
        next += get(root, xpath, create, last_only, limit, allow_rooted=False)
282 292
        
283 293
        for branch in elem.other_branches:
284
            branch = copy.deepcopy(branch)
294
            branch = branch[:] # don't modify input!
285 295
            set_value(branch, path_value)
286 296
            next += get(node, branch, create, last_only, limit,
287 297
                allow_rooted=False)
......
291 301
def put_obj(root, xpath, id_, has_types, value=None):
292 302
    if util.is_str(xpath): xpath = parse(xpath)
293 303
    
294
    xpath = copy.deepcopy(xpath) # don't modify input!
304
    xpath = xpath[:] # don't modify input!
295 305
    set_id(xpath, id_, has_types)
296 306
    if value != None: set_value(xpath, value)
297 307
    get(root, xpath, True)

Also available in: Unified diff