Skip to content

Commit

Permalink
perf: optimize LocalGuid#getTimeMsFromId
Browse files Browse the repository at this point in the history
chore: 1.1.3 pre-release
  • Loading branch information
stun4j committed Jul 22, 2021
1 parent 28e2311 commit c599159
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
2 changes: 0 additions & 2 deletions misc/log4j2.component.properties_bak

This file was deleted.

8 changes: 5 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<version>2.5.2</version>
<relativePath />
</parent>
<groupId>com.stun4j</groupId>
<artifactId>stun4j-guid</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>

<name>Stun4J Guid</name>
<description>Global unique id generator,distributed,ultra fast,easy to use</description>
Expand Down Expand Up @@ -75,7 +76,8 @@
<log4j2.version>2.14.1</log4j2.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<guava.version>30.1.1-jre</guava.version>
<disruptor.version>3.4.3</disruptor.version>
<disruptor.version>3.4.4</disruptor.version>
<dropwizard-metrics.version>4.2.3</dropwizard-metrics.version>
</properties>

<dependencies>
Expand Down
10 changes: 2 additions & 8 deletions src/main/java/com/stun4j/guid/LocalGuid.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*-
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
Expand All @@ -19,7 +19,6 @@
import static com.stun4j.guid.utils.Asserts.argument;
import static com.stun4j.guid.utils.Asserts.notNull;
import static com.stun4j.guid.utils.Asserts.state;
import static com.stun4j.guid.utils.Strings.leftPad;
import static com.stun4j.guid.utils.Strings.lenientFormat;

import java.security.SecureRandom;
Expand Down Expand Up @@ -176,12 +175,7 @@ public long from(long timeMs) {
}

public long getTimeMsFromId(long idExpectingSameEpoch) {
// assume the id is an unsigned num
String binStr = Long.toUnsignedString(idExpectingSameEpoch, 2);
binStr = leftPad(binStr, 64, "0");
String timeDeltaStr = binStr.substring(1, 42);
long back = Long.valueOf(timeDeltaStr, 2);
return back + this.epoch;
return (idExpectingSameEpoch >> timestampLeftShift & ~(-1L << 41L)) + this.epoch;
}
// <-

Expand Down

0 comments on commit c599159

Please sign in to comment.