Skip to content

Commit 79a2350

Browse files
Avoid nanosecond-resolution time where not needed (#224)
* Avoid nanosecond-resolution time where not needed `Instant#now` returns an Instant with nanonsecond-level resolution. The ns-level timer isn't cheap, and is always discarded in X-Ray's code. We can avoid much of this cost by swapping out `Instant#now` with `System#currentTimeMillis`. * Update aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/internal/TimeUtils.java Co-authored-by: Anuraag Agrawal <anuraaga@gmail.com>
1 parent ec009fd commit 79a2350

File tree

6 files changed

+39
-13
lines changed

6 files changed

+39
-13
lines changed

aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/DummySegment.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.amazonaws.xray.AWSXRayRecorder;
1919
import com.fasterxml.jackson.annotation.JsonInclude;
20-
import java.time.Instant;
2120
import java.util.ArrayList;
2221
import java.util.List;
2322
import java.util.Map;
@@ -62,7 +61,7 @@ public DummySegment(AWSXRayRecorder creator) {
6261
}
6362

6463
public DummySegment(AWSXRayRecorder creator, TraceID traceId) {
65-
this.startTime = Instant.now().toEpochMilli() / 1000.0d;
64+
this.startTime = System.currentTimeMillis() / 1000.0d;
6665
this.creator = creator;
6766
this.traceId = traceId;
6867
}
@@ -316,7 +315,7 @@ public String prettySerialize() {
316315
@Override
317316
public boolean end() {
318317
if (getEndTime() < Double.MIN_NORMAL) {
319-
setEndTime(Instant.now().toEpochMilli() / 1000.0d);
318+
setEndTime(System.currentTimeMillis() / 1000.0d);
320319
}
321320

322321
return false;

aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/EntityImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import com.fasterxml.jackson.databind.node.NullNode;
3434
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
3535
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
36-
import java.time.Instant;
3736
import java.util.ArrayList;
3837
import java.util.List;
3938
import java.util.Map;
@@ -175,7 +174,7 @@ protected EntityImpl(AWSXRayRecorder creator, String name) {
175174
this.sql = new ConcurrentHashMap<>();
176175
this.annotations = new ConcurrentHashMap<>();
177176
this.metadata = new ConcurrentHashMap<>();
178-
this.startTime = Instant.now().toEpochMilli() / 1000.0d;
177+
this.startTime = System.currentTimeMillis() / 1000d;
179178
this.id = creator.getIdGenerator().newEntityId();
180179
this.inProgress = true;
181180
this.referenceCount = new LongAdder();

aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/SegmentImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import com.amazonaws.xray.AWSXRayRecorder;
1919
import com.fasterxml.jackson.annotation.JsonIgnore;
20-
import java.time.Instant;
2120
import java.util.HashMap;
2221
import java.util.Map;
2322
import java.util.concurrent.ConcurrentHashMap;
@@ -63,7 +62,7 @@ public SegmentImpl(AWSXRayRecorder creator, String name, TraceID traceId) {
6362
@Override
6463
public boolean end() {
6564
if (getEndTime() < Double.MIN_NORMAL) {
66-
setEndTime(Instant.now().toEpochMilli() / 1000.0d);
65+
setEndTime(System.currentTimeMillis() / 1000d);
6766
}
6867

6968
setInProgress(false);

aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/SubsegmentImpl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.amazonaws.xray.AWSXRayRecorder;
1919
import com.fasterxml.jackson.core.JsonProcessingException;
2020
import com.fasterxml.jackson.databind.node.ObjectNode;
21-
import java.time.Instant;
2221
import java.util.HashSet;
2322
import java.util.Set;
2423
import org.apache.commons.logging.Log;
@@ -55,7 +54,7 @@ public boolean end() {
5554
}
5655

5756
if (getEndTime() < Double.MIN_NORMAL) {
58-
setEndTime(Instant.now().toEpochMilli() / 1000.0d);
57+
setEndTime(System.currentTimeMillis() / 1000d);
5958
}
6059
setInProgress(false);
6160
boolean shouldEmit = parentSegment.decrementReferenceCount() && parentSegment.isSampled();

aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/entities/TraceID.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import com.amazonaws.xray.AWSXRay;
2222
import com.amazonaws.xray.AWSXRayRecorder;
23+
import com.amazonaws.xray.internal.TimeUtils;
2324
import java.math.BigInteger;
24-
import java.time.Instant;
2525
import org.checkerframework.checker.nullness.qual.Nullable;
2626

2727
public class TraceID {
@@ -39,7 +39,7 @@ public class TraceID {
3939
* @see #create(AWSXRayRecorder)
4040
*/
4141
public static TraceID create() {
42-
return new TraceID(Instant.now().getEpochSecond(), AWSXRay.getGlobalRecorder());
42+
return new TraceID(TimeUtils.currentEpochSecond(), AWSXRay.getGlobalRecorder());
4343
}
4444

4545
/**
@@ -48,7 +48,7 @@ public static TraceID create() {
4848
* that created it.
4949
*/
5050
public static TraceID create(AWSXRayRecorder creator) {
51-
return new TraceID(Instant.now().getEpochSecond(), creator);
51+
return new TraceID(TimeUtils.currentEpochSecond(), creator);
5252
}
5353

5454
/**
@@ -108,7 +108,7 @@ public static TraceID invalid() {
108108
*/
109109
@Deprecated
110110
public TraceID() {
111-
this(Instant.now().getEpochSecond());
111+
this(TimeUtils.currentEpochSecond());
112112
}
113113

114114
/**
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amazonaws.xray.internal;
17+
18+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
19+
20+
public final class TimeUtils {
21+
/**
22+
* @return the current epoch second
23+
*/
24+
public static long currentEpochSecond() {
25+
return MILLISECONDS.toSeconds(System.currentTimeMillis());
26+
}
27+
28+
private TimeUtils() {
29+
}
30+
}

0 commit comments

Comments
 (0)