Skip to content

Commit

Permalink
[api] Replace double-check singlton with lazy initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfliu committed Oct 30, 2023
1 parent 85d9e85 commit 75b8839
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@code LgbmEngineProvider} is the LightGBM implementation of {@link EngineProvider}. */
public class LgbmEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (LgbmEngineProvider.class) {
engine = LgbmEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = LgbmEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@code XgbEngineProvider} is the XGBoost implementation of {@link EngineProvider}. */
public class XgbEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (XgbEngineProvider.class) {
engine = XgbEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = XgbEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@code MxEngineProvider} is the MXNet implementation of {@link EngineProvider}. */
public class MxEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (MxEngineProvider.class) {
engine = MxEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = MxEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@code OrtEngineProvider} is the ONNX Runtime implementation of {@link EngineProvider}. */
public class OrtEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (OrtEngineProvider.class) {
engine = OrtEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = OrtEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@code PpEngineProvider} is the PaddlePaddle implementation of {@link EngineProvider}. */
public class PpEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (PpEngineProvider.class) {
engine = PpEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = PpEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@code PtEngineProvider} is the PyTorch implementation of {@link EngineProvider}. */
public class PtEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (PtEngineProvider.class) {
engine = PtEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = PtEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@code TfEngineProvider} is the TensorFlow implementation of {@link EngineProvider}. */
public class TfEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (TfEngineProvider.class) {
engine = TfEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = TfEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@code TrtEngineProvider} is the TensorRT implementation of {@link EngineProvider}. */
public class TrtEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (TrtEngineProvider.class) {
engine = TrtEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = TrtEngine.newInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
/** {@code TfLiteEngineProvider} is the TFLite implementation of {@link EngineProvider}. */
public class TfLiteEngineProvider implements EngineProvider {

private static volatile Engine engine; // NOPMD

/** {@inheritDoc} */
@Override
public String getEngineName() {
Expand All @@ -35,11 +33,10 @@ public int getEngineRank() {
/** {@inheritDoc} */
@Override
public Engine getEngine() {
if (engine == null) {
synchronized (TfLiteEngineProvider.class) {
engine = TfLiteEngine.newInstance();
}
}
return engine;
return InstanceHolder.INSTANCE;
}

private static class InstanceHolder {
static final Engine INSTANCE = TfLiteEngine.newInstance();
}
}

0 comments on commit 75b8839

Please sign in to comment.