You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Section.copy_section gets slower with every child section already in the destination, so copying n sections into one parent takes O(n²) time. Copying 2,000 small sections takes about 6 minutes.
This problem was uncovered during the refactor of simulation results structure for the BSB dbbs-lab/bsb#299
Reproduction
importos, tempfile, timeimportnixiodefrun(n):
withtempfile.TemporaryDirectory() astmp:
f=nixio.File.open(os.path.join(tmp, "f.nix"), nixio.FileMode.Overwrite)
src=f.create_section("src", "t")
src.create_property("p", 1) # works around #527, see the note theresections= [src.create_section(f"s{i}", "t") foriinrange(n)]
dst=f.create_section("dst", "t")
start=time.perf_counter()
forsecinsections:
dst.copy_section(sec)
elapsed=time.perf_counter() -startf.close()
returnelapsedfornin (250, 500, 1000, 2000):
t=run(n)
print(f"{n:>5} copies: {t:6.2f}s ({t/n*1e3:5.1f} ms per copy)")
250 copies: 6.22s ( 24.9 ms per copy)
500 copies: 22.84s ( 45.7 ms per copy)
1000 copies: 90.29s ( 90.3 ms per copy)
2000 copies: 371.57s (185.8 ms per copy)
Cause
Section.copy_section (nixio/section.py:211) returns the copy by its id:
returnself.sections[sec.attrs["entity_id"]]
A lookup by id goes through H5Group.get_by_id, which goes through every child section and reads its entity_id attribute until it finds a match. That makes each copy O(n) in the number of children of the destination.
Suggested fix
Return by the name the section was copied to, as File.copy_section does:
Activity