Uploaded image for project: 'Sling'
  1. Sling
  2. SLING-9312

LazyBindings.entrySet: hashCode of binding values is calculated

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • API 2.21.0
    • API 2.23.0
    • API
    • None

    Description

      The method entrySet in org.apache.sling.api.scripting.LazyBindings currently has a bug that forces the hashCode of the entry values in Bindings are calculated in each HTL request, when it should only be the hashCode of the keys - if at all. This can be a performance problem if the value is e.g. a large map, in our (complicated) case this even got us a StackOverflowError.

      The problematic code is:

          public Set<Entry<String, Object>> entrySet() {
              HashSet<Entry<String, Object>> entrySet = new HashSet<>(super.entrySet());
              for (Map.Entry supplierEntry : suppliers.entrySet()) {
                  entrySet.add(supplierEntry);
              }
              return Collections.unmodifiableSet(entrySet);
          } 

      Since the entries are put together in a HashSet, each add will calculate the hashCode of the entry - which sums the hashCode of the key and the value of each binding entry. I suggest to replace this with a HashMap and return it's entrySet. Another problem is that this could return several entries for the same key, which is probably not intended.

      In case you are wondering, this is called in every HTL request like this:

               at java.base/java.util.HashSet.add(HashSet.java:220)
                at java.base/java.util.AbstractCollection.addAll(AbstractCollection.java:336)
                at java.base/java.util.HashSet.<init>(HashSet.java:120)
                at org.apache.sling.api.scripting.LazyBindings.entrySet(LazyBindings.java:117) [org.apache.sling.api:2.21.0]
                at org.apache.sling.api.scripting.LazyBindings.putAll(LazyBindings.java:87) [org.apache.sling.api:2.21.0]
                at org.apache.sling.scripting.sightly.impl.engine.SightlyCompiledScript.eval(SightlyCompiledScript.java:50) [org.apache.sling.scripting.sightly:1.2.0.0]
                at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:386) [org.apache.sling.scripting.core:2.1.0]
                at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:184) [org.apache.sling.scripting.core:2.1.0]
                at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:491) [org.apache.sling.scripting.core:2.1.0]
                at org.apache.sling.engine.impl.request.RequestData.service(RequestData.java:552) [org.apache.sling.engine:2.6.20]
                at org.apache.sling.engine.impl.filter.SlingComponentFilterChain.render(SlingComponentFilterChain.java:44) [org.apache.sling.engine:2.6.20] 

       

       

      Attachments

        Issue Links

          Activity

            People

              radu Radu Cotescu
              hanspeterstoerr Hans-Peter Stoerr
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: