From d91137fd4841b77fa18e61417eaba1a2bf29fbbb Mon Sep 17 00:00:00 2001 From: fhanik Date: Mon, 22 Jun 2009 15:36:35 +0000 Subject: [PATCH] Minimize thread local access when possible, since thread local does a map lookup. Idea by kkolinko git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@787274 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/catalina/valves/AccessLogValve.java | 107 ++++++--------------- 1 file changed, 32 insertions(+), 75 deletions(-) diff --git a/java/org/apache/catalina/valves/AccessLogValve.java b/java/org/apache/catalina/valves/AccessLogValve.java index e72b71721..c4920d7dc 100644 --- a/java/org/apache/catalina/valves/AccessLogValve.java +++ b/java/org/apache/catalina/valves/AccessLogValve.java @@ -248,20 +248,30 @@ public class AccessLogValve * is true. */ protected File currentLogFile = null; + private class AccessDateStruct { + private Date currentDate = new Date(); + private String currentDateString = null; + private SimpleDateFormat dayFormatter = new SimpleDateFormat("dd");; + private SimpleDateFormat monthFormatter = new SimpleDateFormat("MM"); + private SimpleDateFormat yearFormatter = new SimpleDateFormat("yyyy"); + private SimpleDateFormat timeFormatter = new SimpleDateFormat("HH:mm:ss"); + public AccessDateStruct() { + dayFormatter.setTimeZone(timezone); + monthFormatter.setTimeZone(timezone); + yearFormatter.setTimeZone(timezone); + timeFormatter.setTimeZone(timezone); + } + } /** * The system time when we last updated the Date that this valve * uses for log lines. */ - private ThreadLocal currentDate = new ThreadLocal() { - protected Date initialValue() { - return new Date(); + private ThreadLocal currentDateStruct = new ThreadLocal() { + protected AccessDateStruct initialValue() { + return new AccessDateStruct(); } }; - private ThreadLocal currentDateString = - new ThreadLocal(); - - /** * Resolve hosts. */ @@ -736,11 +746,12 @@ public class AccessLogValve private Date getDate() { // Only create a new Date once per second, max. long systime = System.currentTimeMillis(); - if ((systime - currentDate.get().getTime()) > 1000) { - currentDate.get().setTime(systime); - currentDateString.set(null); + AccessDateStruct struct = currentDateStruct.get(); + if ((systime - struct.currentDate.getTime()) > 1000) { + struct.currentDate.setTime(systime); + struct.currentDateString = null; } - return currentDate.get(); + return struct.currentDate; } @@ -836,7 +847,7 @@ public class AccessLogValve fileDateFormat = "yyyy-MM-dd"; fileDateFormatter = new SimpleDateFormat(fileDateFormat); fileDateFormatter.setTimeZone(timezone); - dateStamp = fileDateFormatter.format(currentDate.get()); + dateStamp = fileDateFormatter.format(currentDateStruct.get().currentDate); open(); } @@ -972,82 +983,28 @@ public class AccessLogValve */ protected class DateAndTimeElement implements AccessLogElement { - /** - * A date formatter to format Dates into a day string in the format - * "dd". - */ - private ThreadLocal dayFormatter = - new ThreadLocal() { - protected SimpleDateFormat initialValue() { - SimpleDateFormat init = new SimpleDateFormat("dd"); - init.setTimeZone(timezone); - return init; - - } - }; - - /** - * A date formatter to format a Date into a month string in the format - * "MM". - */ - private ThreadLocal monthFormatter = - new ThreadLocal() { - protected SimpleDateFormat initialValue() { - SimpleDateFormat init = new SimpleDateFormat("MM"); - init.setTimeZone(timezone); - return init; - - } - }; - - - /** - * A date formatter to format a Date into a year string in the format - * "yyyy". - */ - private ThreadLocal yearFormatter = - new ThreadLocal() { - protected SimpleDateFormat initialValue() { - SimpleDateFormat init = new SimpleDateFormat("yyyy"); - init.setTimeZone(timezone); - return init; - - } - }; - - - /** - * A date formatter to format a Date into a time in the format - * "kk:mm:ss" (kk is a 24-hour representation of the hour). - */ - private ThreadLocal timeFormatter = - new ThreadLocal() { - protected SimpleDateFormat initialValue() { - SimpleDateFormat init = new SimpleDateFormat("HH:mm:ss"); - init.setTimeZone(timezone); - return init; - } - }; + public void addElement(StringBuffer buf, Date date, Request request, Response response, long time) { - if (currentDateString.get() == null) { + AccessDateStruct struct = currentDateStruct.get(); + if (struct.currentDateString == null) { StringBuffer current = new StringBuffer(32); current.append('['); - current.append(dayFormatter.get().format(date)); + current.append(struct.dayFormatter.format(date)); current.append('/'); - current.append(lookup(monthFormatter.get().format(date))); + current.append(lookup(struct.monthFormatter.format(date))); current.append('/'); - current.append(yearFormatter.get().format(date)); + current.append(struct.yearFormatter.format(date)); current.append(':'); - current.append(timeFormatter.get().format(date)); + current.append(struct.timeFormatter.format(date)); current.append(' '); current.append(getTimeZone(date)); current.append(']'); - currentDateString.set(current.toString()); + struct.currentDateString = current.toString(); } - buf.append(currentDateString.get()); + buf.append(struct.currentDateString); } } -- 2.11.0