Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-3407

Need a mechanism to avoid calling entrySet() on java.util.Map properties

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.0.4
    • Fix Version/s: 2.0.5
    • Component/s: Core
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      TypeConverterDelegate.convertIfNecessary() method always calls convertToTypedMap() if the property implements the java.util.Map interface. The convertToTypedMap() method always calls java.util.Map.entrySet() on the property. When that property is something like a distributed cache containing a very large number of objects, iteration over the map must be avoided. There should be some way to prevent or override the call to convertToTypedMap().

        Activity

        Hide
        juergen.hoeller Juergen Hoeller added a comment -

        Indeed, this code assumes that Maps are always statically specified value Maps of reasonably small size.

        I've optimized this analogously to our array conversion optimizations in 2.0.1: We check in advance whether there is any point in trying to convert elements, checking for generic key/value types as well as for custom editors for this particular collection. This means that we don't even try to create a typed collection, avoiding to iterate over the collection's original elements just to find out that no element had to be converted in the first place.

        So in your scenario, as long as the target property is not declared as typed Map but rather just as plain Map, no conversion will be attempted, hence no iteration of the original entries will happen. Note, however, that a Map<String, Object> declaration (or the like) would still trigger a check of all keys... Consider declaring your receiving property/argument as plain Map in this case, internally accessing it as typed Map through a cast.

        This refinement will be available in the next 2.0.5 snapshot (http://www.springframework.org/snapshots). Please give it a try and let us know whether it works for you!

        Juergen

        Show
        juergen.hoeller Juergen Hoeller added a comment - Indeed, this code assumes that Maps are always statically specified value Maps of reasonably small size. I've optimized this analogously to our array conversion optimizations in 2.0.1: We check in advance whether there is any point in trying to convert elements, checking for generic key/value types as well as for custom editors for this particular collection. This means that we don't even try to create a typed collection, avoiding to iterate over the collection's original elements just to find out that no element had to be converted in the first place. So in your scenario, as long as the target property is not declared as typed Map but rather just as plain Map, no conversion will be attempted, hence no iteration of the original entries will happen. Note, however, that a Map<String, Object> declaration (or the like) would still trigger a check of all keys... Consider declaring your receiving property/argument as plain Map in this case, internally accessing it as typed Map through a cast. This refinement will be available in the next 2.0.5 snapshot ( http://www.springframework.org/snapshots ). Please give it a try and let us know whether it works for you! Juergen

          People

          • Assignee:
            juergen.hoeller Juergen Hoeller
            Reporter:
            scottb Scott Bartram
            Last updater:
            Trevor Marshall
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              10 years, 27 weeks, 1 day ago