proc-macro2 icon indicating copy to clipboard operation
proc-macro2 copied to clipboard

O(n) fallback::Span::start/end

Open Axel-Reactor opened this issue 10 months ago • 2 comments

I use syn to parse Rust files outside of Rust macros and I noticed really slow performance in fallback::Span.

The problem seems like that there is a SOURCE_MAP thread local that only keeps growing. I never destroy the threads, it's a thread pool. Now I could try to create new threads every time I call into syn, but this seems silly.

Also, why is SourceMap::files a Vec? It seems like the spans in FileInfo can never overlap, so why not just use a BTreeMap sorted by span.start to look up the files? At least then it would be O(log n) for look ups even if it keeps growing.

Axel-Reactor avatar Jan 27 '25 22:01 Axel-Reactor

You should be invalidating spans using https://docs.rs/proc-macro2/1/proc_macro2/extra/fn.invalidate_current_thread_spans.html.

dtolnay avatar Jan 27 '25 22:01 dtolnay

Didn't know about this. That's kind of bad UX though when syn itself doesn't expose any of this to the outside?

Axel-Reactor avatar Jan 27 '25 22:01 Axel-Reactor