Replace Mutex with RwLock in string interning code.#162
Replace Mutex with RwLock in string interning code.#162mwillsey merged 1 commit intoegraphs-good:mainfrom
Conversation
When calling into egg from multiple threads, the Mutex used for STRINGS can become a bottleneck. For the application where I noticed this, replacing the Mutex with a reader-writer lock cuts time spent in egg::util::intern by half.
|
Great PR, I will accept after CI goes. Out of curiosity, what were you doing that caused the interning to get called so frequently? My initial intention was that interning should only be called while setting things up. |
|
To make a long story short, most of my code is C++ and so I'm going through the FFI to call into egg. Currently, that involves converting my expressions into strings, passing them over, simplifying them with egg, and then going back to strings (that I parse back into my own expression representation). That is admittedly not the best approach, but it was an easy way to experiment with egg before going further... (Related, I'm seeing a decent amount of time spent in So, I can certainly imagine that the way I'm using egg is part of the problem... |
|
Ahh, that totally makes sense. If you are crossing FFI using strings, then you don't have much of a choice to parse! |
When calling into egg from multiple threads, the
Mutexused forSTRINGScan become a bottleneck. (In my use case, I have a few hundred expressions that I'd like egg to simplify, so I do that using multiple threads.) In that context, I observed thategg::util::internwas a significant fraction of total runtime and that more time was spent in it than in any of the rest of egg's code(!). Here is whatperfon Linux reported:(Note that is in the context of a larger application; just over half of total runtime is in egg but that 43% is with respect to total runtime.) Total runtime for my test is 4.04s user, 14.22s sys.
This PR replaces the
Mutexis replaced with a reader-writer lock.perfreports roughly a 2x speedup:which is also reflected in the total runtime for my system going to 9.26s user, 0.21s sys.