Skip to content

Commit 73fb82b

Browse files
committed
Optimise session reset to reduce Calendar creation
1 parent d8c729c commit 73fb82b

File tree

12 files changed

+199
-11
lines changed

12 files changed

+199
-11
lines changed

quickfixj-core/src/main/java/quickfix/CachedFileStore.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ public Date getCreationTime() throws IOException {
163163
return cache.getCreationTime();
164164
}
165165

166+
/*
167+
* (non-Javadoc)
168+
* @see quickfix.MessageStore#getCreationTimeCalendar()
169+
*/
170+
public Calendar getCreationTimeCalendar() throws IOException {
171+
return cache.getCreationTimeCalendar();
172+
}
173+
166174
private void initializeSequenceNumbers() throws IOException {
167175
sequenceNumberFile.seek(0);
168176
if (sequenceNumberFile.length() > 0) {

quickfixj-core/src/main/java/quickfix/DefaultSessionSchedule.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,17 @@ public class DefaultSessionSchedule implements SessionSchedule {
4141
private final int[] weekdayOffsets;
4242
protected static final Logger LOG = LoggerFactory.getLogger(DefaultSessionSchedule.class);
4343

44+
//Cache recent time data to reduce creation of calendar objects
45+
private ThreadLocal<Calendar> threadLocalCalendar;
46+
private ThreadLocal<TimeInterval> threadLocalRecentTimeInterval;
47+
4448
public DefaultSessionSchedule(SessionSettings settings, SessionID sessionID) throws ConfigError,
4549
FieldConvertError {
50+
threadLocalCalendar = ThreadLocal.withInitial(SystemTime::getUtcCalendar);
51+
threadLocalRecentTimeInterval = new ThreadLocal<>();
52+
isNonStopSession = settings.isSetting(sessionID, Session.SETTING_NON_STOP_SESSION)
53+
&& settings.getBool(sessionID, Session.SETTING_NON_STOP_SESSION);
4654

47-
isNonStopSession = settings.isSetting(sessionID, Session.SETTING_NON_STOP_SESSION) && settings.getBool(sessionID, Session.SETTING_NON_STOP_SESSION);
4855
TimeZone defaultTimeZone = getDefaultTimeZone(settings, sessionID);
4956
if (isNonStopSession) {
5057
isWeekdaySession = false;
@@ -104,7 +111,7 @@ private TimeEndPoint getTimeEndPoint(SessionSettings settings, SessionID session
104111
}
105112

106113
private TimeZone getDefaultTimeZone(SessionSettings settings, SessionID sessionID)
107-
throws ConfigError, FieldConvertError {
114+
throws ConfigError {
108115
TimeZone sessionTimeZone;
109116
if (settings.isSetting(sessionID, Session.SETTING_TIMEZONE)) {
110117
String sessionTimeZoneID = settings.getString(sessionID, Session.SETTING_TIMEZONE);
@@ -300,9 +307,16 @@ public boolean isSessionTime() {
300307
if(isNonStopSession()) {
301308
return true;
302309
}
303-
Calendar now = SystemTime.getUtcCalendar();
304-
TimeInterval interval = theMostRecentIntervalBefore(now);
305-
return interval.isContainingTime(now);
310+
Calendar now = threadLocalCalendar.get();
311+
now.setTimeInMillis(SystemTime.currentTimeMillis());
312+
TimeInterval mostRecentInterval = threadLocalRecentTimeInterval.get();
313+
if (mostRecentInterval != null && mostRecentInterval.isContainingTime(now)) {
314+
return true;
315+
}
316+
mostRecentInterval = theMostRecentIntervalBefore(now);
317+
boolean result = mostRecentInterval.isContainingTime(now);
318+
threadLocalRecentTimeInterval.set(mostRecentInterval);
319+
return result;
306320
}
307321

308322
public String toString() {

quickfixj-core/src/main/java/quickfix/FileStore.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ public Date getCreationTime() throws IOException {
153153
return cache.getCreationTime();
154154
}
155155

156+
/* (non-Javadoc)
157+
* @see quickfix.MessageStore#getCreationTimeCalendar()
158+
*/
159+
@Override
160+
public Calendar getCreationTimeCalendar() throws IOException {
161+
return cache.getCreationTimeCalendar();
162+
}
163+
156164
private void initializeSequenceNumbers() throws IOException {
157165
senderSequenceNumberFile.seek(0);
158166
if (senderSequenceNumberFile.length() > 0) {

quickfixj-core/src/main/java/quickfix/JdbcStore.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ public Date getCreationTime() throws IOException {
155155
return cache.getCreationTime();
156156
}
157157

158+
public Calendar getCreationTimeCalendar() throws IOException {
159+
return cache.getCreationTimeCalendar();
160+
}
161+
158162
public int getNextSenderMsgSeqNum() throws IOException {
159163
return cache.getNextSenderMsgSeqNum();
160164
}

quickfixj-core/src/main/java/quickfix/MemoryStore.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public Date getCreationTime() throws IOException {
7070
return creationTime.getTime();
7171
}
7272

73+
public Calendar getCreationTimeCalendar() throws IOException {
74+
return creationTime;
75+
}
76+
7377
/* package */void setCreationTime(Calendar creationTime) {
7478
this.creationTime = creationTime;
7579
}

quickfixj-core/src/main/java/quickfix/MessageStore.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package quickfix;
2121

22+
import java.util.Calendar;
2223
import java.util.Collection;
2324
import java.util.Date;
2425
import java.io.IOException;
@@ -73,6 +74,14 @@ public interface MessageStore {
7374
*/
7475
Date getCreationTime() throws IOException;
7576

77+
/**
78+
* Get the session creation time as a calendar object.
79+
*
80+
* @return the session creation time.
81+
* @throws IOException IO error
82+
*/
83+
Calendar getCreationTimeCalendar() throws IOException;
84+
7685
/**
7786
* Reset the message store. Sequence numbers are set back to 1 and stored
7887
* messages are erased. The session creation time is also set to the time of

quickfixj-core/src/main/java/quickfix/NoopStore.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
package quickfix;
2222

23+
import java.util.Calendar;
2324
import java.util.Collection;
2425
import java.util.Date;
2526

@@ -31,6 +32,7 @@
3132
public class NoopStore implements MessageStore {
3233

3334
private Date creationTime = new Date();
35+
private Calendar creationTimeCalendar = SystemTime.getUtcCalendar(creationTime);
3436
private int nextSenderMsgSeqNum = 1;
3537
private int nextTargetMsgSeqNum = 1;
3638

@@ -41,6 +43,10 @@ public Date getCreationTime() {
4143
return creationTime;
4244
}
4345

46+
public Calendar getCreationTimeCalendar() {
47+
return creationTimeCalendar;
48+
}
49+
4450
public int getNextSenderMsgSeqNum() {
4551
return nextSenderMsgSeqNum;
4652
}

quickfixj-core/src/main/java/quickfix/Session.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ public String getRemoteAddress() {
584584
private boolean isCurrentSession(final long time)
585585
throws IOException {
586586
return sessionSchedule == null || sessionSchedule.isSameSession(
587-
SystemTime.getUtcCalendar(time), SystemTime.getUtcCalendar(state.getCreationTime()));
587+
SystemTime.getUtcCalendar(time), state.getCreationTimeCalendar());
588588
}
589589

590590
/**

quickfixj-core/src/main/java/quickfix/SessionState.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
package quickfix;
2121

2222
import java.io.IOException;
23-
import java.util.Collection;
24-
import java.util.Date;
25-
import java.util.LinkedHashMap;
26-
import java.util.Map;
23+
import java.util.*;
2724
import java.util.concurrent.atomic.AtomicInteger;
2825
import java.util.concurrent.locks.Lock;
2926
import java.util.concurrent.locks.ReentrantLock;
@@ -502,6 +499,10 @@ public Object getLock() {
502499
return lock;
503500
}
504501

502+
public Calendar getCreationTimeCalendar() throws IOException {
503+
return messageStore.getCreationTimeCalendar();
504+
}
505+
505506
private final static class NullLog implements Log {
506507
public void onOutgoing(String message) {
507508
}

quickfixj-core/src/main/java/quickfix/SleepycatStore.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,23 @@ private void convertToIOExceptionAndRethrow(Exception e) throws IOException {
256256
throw ioe;
257257
}
258258

259+
260+
/*
261+
* (non-Javadoc)
262+
* @see quickfix.MessageStore#getCreationTime()
263+
*/
259264
public Date getCreationTime() throws IOException {
260265
return info.getCreationTime().getTime();
261266
}
262267

268+
/*
269+
* (non-Javadoc)
270+
* @see quickfix.MessageStore#getCreationTimeCalendar()
271+
*/
272+
public Calendar getCreationTimeCalendar() throws IOException {
273+
return info.getCreationTime();
274+
}
275+
263276
public int getNextSenderMsgSeqNum() throws IOException {
264277
return info.getNextSenderMsgSeqNum();
265278
}

0 commit comments

Comments
 (0)