Recursive get_section() performance

Hi! For a site I’m currently making, I’m attempting to make a sidebar navigation window that will display every page recursively through subsections. There’s roughly 28 subsections to iterate through, and 650~ pages on the site in total. However, using get_section() to get the subsections of sections (and not being able to use metadata_only, as this will prevent the pages field being populated) dramatically slows down site rendering (bringing it up to roughly 28 seconds).

I was wondering if there was a different way of being able to iterate through every page in a site that I’m overlooking, or if there’s a way to eek out some extra performance here.

The lines in question are here - there’s a lot of extraneous code that I’m yet to tidy up, but the performance-eating line in question is L64.

Thank you!