Skip to content

filter/transform/asMap overloads for Sorted/Navigable types hinder Proguard #2079

Open
@cpovirk

Description

@cpovirk

I recently knocked ~20Kb, 200 methods, and 20 classes off a Proguarded Android apps by replacing Maps.filterValues(map, predicate).clear() with Iterables.removeIf(map.values(), predicate). The app was using our JDK5 backport. If it had been using the mainline version, those savings would be 30Kb, 300 methods, and 30 classes.

But let's not pat ourselves on the back too much at these savings: A better way to look at it is that it was space Guava was wasting.

Maybe there's a way to enhance Proguard to detect that no Map passed to filterValues can ever be a SortedMap, at which it can prune the SortedMap and NavigableMap implementations that produced the bloat. But this is likely to be hard, and in fact it's likely to be impossible in some cases: If the Android system is returning a Map to you, there's no way for Proguard to know that neither this version of Android nor any future version will ever return a SortedMap.

For this reason, we should consider removing the Sorted/Navigable special case (basically, the instanceof checks) entirely. But... this will run afoul of the cardinal rule of overloading, which says here that calling filterKeys((Map) m) should do the same thing as filterKeys((SortedMap) m). Does that rule really matter? I'd say probably not, but I'd feel bad if I turned out to be wrong. We could always split the overloads out, java.util.Collections-style. But the naming here is uglier -- filterSortedKeys, etc. That's not too bad, I guess. (Originally we had the other methods in a separate NavigableMaps class... but we "fixed" that three years ago. But I don't think we'd want to go back to that, anyway.)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions