Skip to content

Possible bloat from over-use of default methods on trait objects #12449

Closed
@zkamsler

Description

@zkamsler

I am not sure if this is an issue as such, but I figured that I ought to mention it.

I don't know the current linker behavior, but the best case is that all implementations of default methods for types that are coerced to trait objects must be generated and cannot be stripped because they are referenced by their vtable. As a result, there are a may be a number of method implementations that cannot be removed even if they do not happen to be used.

This is most notable for the io::Writer and io::Reader traits, which are often used behind trait objects and have a number of default methods. Writer has 28 default methods (not including flush and by_ref), which is up to 513 methods over 19 implementations. Reader has 31 default methods (not including by_ref and bytes), which is up to 620 methods over 20 implementations.

I do not mean to suggest that default methods should not be used for traits that are often uses as trait objects, but merely that some care should be used when adding them. In some cases things like such as wrapper structs, or the unpopular trait-just-to-add-methods may be appropriate.

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-compiletimeIssue: Problems and improvements with respect to compile times.I-slowIssue: Problems and improvements with respect to performance of generated code.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions