You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Joseph Walton (JIRA)" <ji...@apache.org> on 2014/09/14 15:14:34 UTC

[jira] [Commented] (HTTPCLIENT-1471) ResponseCachingPolicy - eliminate boxing and HashSet

    [ https://issues.apache.org/jira/browse/HTTPCLIENT-1471?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14133201#comment-14133201 ] 

Joseph Walton commented on HTTPCLIENT-1471:
-------------------------------------------

I tried a simple microbenchmark comparing {{Set.contains}}, iterating over an {{int[]}} and {{Arrays.binarySearch}} and didn't notice much difference. The only significant improvement was moving the test into code:

{code}
return (status == SC_OK || status == SC_NON_AUTHORITATIVE_INFORMATION || status == SC_MULTIPLE_CHOICES || ...);
{code}

That doesn't seem very maintainable.

One change to consider would be making the two possible values of {{uncacheableStatuses}} into constants, so they could be shared across instances of {{ResponseCachingPolicy}}:

{noformat}
--- a/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseCachingPolicy.java
+++ b/httpclient-cache/src/main/java/org/apache/http/impl/client/cache/ResponseCachingPolicy.java
@@ -56,6 +56,10 @@ class ResponseCachingPolicy {
     private static final String[] AUTH_CACHEABLE_PARAMS = {
             "s-maxage", HeaderConstants.CACHE_CONTROL_MUST_REVALIDATE, HeaderConstants.PUBLIC
     };
+    private static final Set<Integer>
+        STATUSES_PARTIAL_CONTENT = new HashSet<Integer>(Arrays.asList(HttpStatus.SC_PARTIAL_CONTENT)),
+        STATUSES_PARTIAL_CONTENT_OR_SEE_OTHER = new HashSet<Integer>(Arrays.asList(HttpStatus.SC_PARTIAL_CONTENT, HttpStatus.SC_SEE_OTHER));
+
     private final long maxObjectSizeBytes;
     private final boolean sharedCache;
     private final boolean neverCache1_0ResponsesWithQueryString;
@@ -86,11 +90,9 @@ class ResponseCachingPolicy {
         this.sharedCache = sharedCache;
         this.neverCache1_0ResponsesWithQueryString = neverCache1_0ResponsesWithQueryString;
         if (allow303Caching) {
-            uncacheableStatuses = new HashSet<Integer>(
-                    Arrays.asList(HttpStatus.SC_PARTIAL_CONTENT));
+            uncacheableStatuses = STATUSES_PARTIAL_CONTENT;
         } else {
-            uncacheableStatuses = new HashSet<Integer>(Arrays.asList(
-                    HttpStatus.SC_PARTIAL_CONTENT, HttpStatus.SC_SEE_OTHER));
+            uncacheableStatuses = STATUSES_PARTIAL_CONTENT_OR_SEE_OTHER;
         }
     }
 
{noformat}


> ResponseCachingPolicy - eliminate boxing and HashSet
> ----------------------------------------------------
>
>                 Key: HTTPCLIENT-1471
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1471
>             Project: HttpComponents HttpClient
>          Issue Type: Improvement
>          Components: HttpCache
>            Reporter: Sebb
>            Priority: Minor
>             Fix For: 4.4 Final
>
>
> ResponseCachingPolicy uses 2 HashSets containing a few Integers each for matching int values.
> This involves lots of boxing plus the overhead of the hashSet.
> Since the numbers of integers involved is very small - at most 6 - it seems unnecessary to use hashing. A linear search would likely be quicker.
> If the number of entries were to be rather larger, then Arrays#binarySearch might be better.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org