rust-sourcemap icon indicating copy to clipboard operation
rust-sourcemap copied to clipboard

perf: Intern strings to reduce memory usage

Open kdy1 opened this issue 7 months ago • 4 comments

This interning logic is not highly efficient, but it still reduces RSS noticeably.

kdy1 avatar May 20 '25 14:05 kdy1

I verified that there were too many duplicates in the case of bundler (not in same sourcemap, rather in multiple source maps). The problem is that a bundler intentionally duplicates some modules into multiple chunks. It's for faster loading of JS bundles, so I think all bundlers share this problem.

kdy1 avatar May 20 '25 14:05 kdy1

That makes sense. Its quite interesting that some of our methods take a T: Into<Arc<str>>, which means you can pass it a Arc<str> and it will just Into it, which is a trivial operation. Not so for the source_content though.

I guess my point with that is that if the bundler internally is using Arc<str>, you could pass those around instead without cloning the actual file contents. Does that make sense?

If that would help, we could just make the source_content take a T: Into<Arc<str>> as well.

Swatinem avatar May 20 '25 14:05 Swatinem

I think it makes sense.

For turbopack, we store source texts in another form, which cannot be converted to Arc<str> in a cheap way but it could be useful for other users.

kdy1 avatar May 20 '25 15:05 kdy1

I think the interning should be improved to make entries live a bit longer, but not sure about a way to do it.

kdy1 avatar May 20 '25 16:05 kdy1

I found a better solution for this problem, so I'm closing this.

kdy1 avatar Jun 04 '25 18:06 kdy1