I was working on a project to make existing tokio-socks5 production.
One of the improvements is, I want to use
impl Future instead of
The plan was successful but I need a custom enum type to allow returning from different
match arms. Once I did that, I noticed the compile time is significantly increased but don’t know why.
Until yesterday, I started to refactoring a big function body to a set of method chains. (This is usually called “extract method” refactoring). When I was doing so I see an error says the type names is exceeding the limit (to be set to 2,097,152 chars long at the time), and I need a specific attribute in my crate main to increase it. I did that and continue to refactoring, then I received another error says it is now reaching the new limit. I gave up after then.
So, some facts I realized from the excersise:
- The increased compile time is due to the compiler creates huge type names internally.
- The refactoring is actually makes this case worse (i.e. it makes the type names longer rather than shorter)
- The length of type name must being growing exponentially in some scenarios.
- The exponential growth is not only theoretical, but also practical in real world, as I am only refactoring existing code that worked.
Right now, I am planed to raise an issue to the rust compiler, but to continue the work I am planning to stop the exponentially growing type names.
One way to do so I think is to use closure types in the middle. As I can see so far, the compiler generated closure types would not be too long as it does not contain information about the captured variables. So it is ideal if I want to hide some types inside. As those types will not be visible to the outside, it will then not appearing in the type signature. This requires me to create a struct to simply wrap a
Fn trait inside, and implement
Future trait on it. I would try this tonight.
For the long term, I believe the proper solution is to generate a unique type for each
impl returns and hiding all component types inside, so if people still get trouble of this, doing extract method refactoring would help rather than making things worse.
As the previous plan does not work, I have to do something else. Now I may have to go back to use
Box, but can anyone show me alternatives?