Skip to content

Commit b18003e

Browse files
authored
Merge pull request #283 from philipwhiuk/optimiseSessionReset
Optimise session reset to reduce `Calendar` creation
2 parents 9f8fb4b + 126a123 commit b18003e

File tree

12 files changed

+199
-7
lines changed

12 files changed

+199
-7
lines changed

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

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

165+
/*
166+
* (non-Javadoc)
167+
* @see quickfix.MessageStore#getCreationTimeCalendar()
168+
*/
169+
public Calendar getCreationTimeCalendar() throws IOException {
170+
return cache.getCreationTimeCalendar();
171+
}
172+
165173
private void initializeSequenceNumbers() throws IOException {
166174
sequenceNumberFile.seek(0);
167175
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 final ThreadLocal<Calendar> threadLocalCalendar;
46+
private final 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
@@ -152,6 +152,14 @@ public Date getCreationTime() throws IOException {
152152
return cache.getCreationTime();
153153
}
154154

155+
/* (non-Javadoc)
156+
* @see quickfix.MessageStore#getCreationTimeCalendar()
157+
*/
158+
@Override
159+
public Calendar getCreationTimeCalendar() throws IOException {
160+
return cache.getCreationTimeCalendar();
161+
}
162+
155163
private void initializeSequenceNumbers() throws IOException {
156164
senderSequenceNumberFile.seek(0);
157165
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
@@ -159,6 +159,10 @@ public Date getCreationTime() throws IOException {
159159
return cache.getCreationTime();
160160
}
161161

162+
public Calendar getCreationTimeCalendar() throws IOException {
163+
return cache.getCreationTimeCalendar();
164+
}
165+
162166
public int getNextSenderMsgSeqNum() throws IOException {
163167
return cache.getNextSenderMsgSeqNum();
164168
}

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
@@ -646,7 +646,7 @@ public String getRemoteAddress() {
646646
private boolean isCurrentSession(final long time)
647647
throws IOException {
648648
return sessionSchedule == null || sessionSchedule.isSameSession(
649-
SystemTime.getUtcCalendar(time), SystemTime.getUtcCalendar(state.getCreationTime()));
649+
SystemTime.getUtcCalendar(time), state.getCreationTimeCalendar());
650650
}
651651

652652
/**

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

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

2222
import java.io.IOException;
23+
import java.util.Calendar;
2324
import java.util.Collection;
2425
import java.util.Date;
2526
import java.util.concurrent.TimeUnit;
@@ -490,6 +491,10 @@ public Object getLock() {
490491
return lock;
491492
}
492493

494+
public Calendar getCreationTimeCalendar() throws IOException {
495+
return messageStore.getCreationTimeCalendar();
496+
}
497+
493498
private final static class NullLog implements Log {
494499
public void onOutgoing(String message) {
495500
}

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)