Return-Path: X-Original-To: apmail-tomcat-dev-archive@www.apache.org Delivered-To: apmail-tomcat-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 858A8182D1 for ; Mon, 14 Sep 2015 08:54:59 +0000 (UTC) Received: (qmail 51455 invoked by uid 500); 14 Sep 2015 08:54:24 -0000 Delivered-To: apmail-tomcat-dev-archive@tomcat.apache.org Received: (qmail 51371 invoked by uid 500); 14 Sep 2015 08:54:24 -0000 Mailing-List: contact dev-help@tomcat.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Tomcat Developers List" Delivered-To: mailing list dev@tomcat.apache.org Received: (qmail 51361 invoked by uid 99); 14 Sep 2015 08:54:24 -0000 Received: from eris.apache.org (HELO hades.apache.org) (140.211.11.105) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 14 Sep 2015 08:54:24 +0000 Received: from hades.apache.org (localhost [127.0.0.1]) by hades.apache.org (ASF Mail Server at hades.apache.org) with ESMTP id A418EAC022D for ; Mon, 14 Sep 2015 08:54:24 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1702873 - /tomcat/tc8.0.x/trunk/java/org/apache/tomcat/util/http/HttpMessages.java Date: Mon, 14 Sep 2015 08:54:24 -0000 To: dev@tomcat.apache.org From: markt@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20150914085424.A418EAC022D@hades.apache.org> Author: markt Date: Mon Sep 14 08:54:24 2015 New Revision: 1702873 URL: http://svn.apache.org/r1702873 Log: Simpler, thread-safe code. Better throughput over anything more than 1000 requests. Modified: tomcat/tc8.0.x/trunk/java/org/apache/tomcat/util/http/HttpMessages.java Modified: tomcat/tc8.0.x/trunk/java/org/apache/tomcat/util/http/HttpMessages.java URL: http://svn.apache.org/viewvc/tomcat/tc8.0.x/trunk/java/org/apache/tomcat/util/http/HttpMessages.java?rev=1702873&r1=1702872&r2=1702873&view=diff ============================================================================== --- tomcat/tc8.0.x/trunk/java/org/apache/tomcat/util/http/HttpMessages.java (original) +++ tomcat/tc8.0.x/trunk/java/org/apache/tomcat/util/http/HttpMessages.java Mon Sep 14 08:54:24 2015 @@ -44,13 +44,28 @@ public class HttpMessages { private final StringManager sm; - private String st_200 = null; - private String st_302 = null; - private String st_400 = null; - private String st_404 = null; + private final String st_200; + private final String st_302; + private final String st_400; + private final String st_404; private HttpMessages(StringManager sm) { + // There is a performance tradeoff here. This implementation incurs + // ~160ns (40ns per StringManager) lookup delay on first access but all + // subsequent lookups take ~0.25ns. + // The alternative approach (lazy init of each cached String) delays the + // StringManager lookup until required but increases the time for + // subsequent lookups to ~0.5ns. + // These times will be in the noise for most requests. This + // implementation was chosen because: + // - Over anything more than a few hundred requests it is faster. + // - The code is a lot simpler. Thread safe lazy init needs care to get + // right. See http://markmail.org/thread/wjp3oejdyxcrz7do this.sm = sm; + st_200 = sm.getString("sc.200"); + st_302 = sm.getString("sc.302"); + st_400 = sm.getString("sc.400"); + st_404 = sm.getString("sc.404"); } @@ -64,37 +79,18 @@ public class HttpMessages { * HTTP specification */ public String getMessage(int status) { - switch( status ) { - case 200: { - String s = st_200; - if(s == null ) { - st_200 = s = sm.getString("sc.200"); - } - return s; + switch (status) { + case 200: + return st_200; + case 302: + return st_302; + case 400: + return st_400; + case 404: + return st_404; + default: + return sm.getString("sc."+ status); } - case 302: { - String s = st_302; - if(s == null ) { - st_302 = s = sm.getString("sc.302"); - } - return s; - } - case 400: { - String s = st_400; - if(s == null ) { - st_400 = s = sm.getString("sc.400"); - } - return s; - } - case 404: { - String s = st_404; - if(s == null ) { - st_404 = s = sm.getString("sc.404"); - } - return s; - } - } - return sm.getString("sc."+ status); } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org For additional commands, e-mail: dev-help@tomcat.apache.org