Builder.fill performance
For a square lattice using Builder.fill
takes 4x the time of using lattice.shape
and HoppingKind
on a square lattice. See slow_fill.ipynb from @r-j-skolasinski.
After a brief exploration, it appears that the reason is multiple extra calls to Builder.symmetry
methods, that are hard to avoid without resorting to code duplication and using low level functions.
The following diff allows to cut 4x down to 3x (not dramatic, but a start nevertheless).
@@ -1253,6 +1253,7 @@ class Builder:
new_sites.add(candidate)
all_added.add(candidate)
self[candidate] = other[candidate]
+ return True
# Initialize the flood-fill
new_sites = set()
@@ -1264,15 +1265,14 @@ class Builder:
while new_sites:
site = new_sites.pop()
domain = sym.which(site)
+ site_orig = sym.to_fd(site)
# other.neighbors(site) gives neighbors of the *image* of
# site in FD of other.symmetry, so we must map it correctly
- hoppings = [(sym.to_fd(site), n) for n in other.neighbors(site)]
+ hoppings = [(site_orig, n) for n in other.neighbors(site_orig)]
for hopping in to_domain(domain, hoppings):
- add_site(hopping[1])
- try:
+ if (add_site(hopping[1]) or (hopping[1] in self
+ and hopping not in self)):
self[hopping] = other[hopping]
- except KeyError:
- pass
return list(all_added)