-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use IntUnaryOperator / DoubleUnaryOperator in VegasLimit
Switch to IntUnaryOperator and DoubleUnaryOperator in VegasLimit to avoid unnecessary boxing. Additionally, reduced repeated reads of volatile fields.
- Loading branch information
Showing
5 changed files
with
167 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...re/src/main/java/com/netflix/concurrency/limits/limit/functions/Log10RootIntFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.netflix.concurrency.limits.limit.functions; | ||
|
||
import java.util.function.IntUnaryOperator; | ||
|
||
/** | ||
* Function used by limiters to calculate thresholds using log10 of the current limit. | ||
* Here we pre-compute the log10 of numbers up to 1000 as an optimization. | ||
*/ | ||
public final class Log10RootIntFunction implements IntUnaryOperator { | ||
private static final int[] lookup = new int[1000]; | ||
|
||
static { | ||
for (int i = 0; i < lookup.length; i++) { | ||
lookup[i] = Math.max(1, (int) Math.log10(i)); | ||
} | ||
} | ||
|
||
private static final Log10RootIntFunction INSTANCE = new Log10RootIntFunction(); | ||
|
||
/** | ||
* Create an instance of a function that returns : baseline + sqrt(limit) | ||
* | ||
* @param baseline | ||
* @return | ||
*/ | ||
public static IntUnaryOperator create(int baseline) { | ||
return baseline == 0 ? INSTANCE : INSTANCE.andThen(t -> t + baseline); | ||
} | ||
|
||
@Override | ||
public int applyAsInt(int t) { | ||
return t < 1000 ? lookup[t] : (int) Math.log10(t); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...rc/test/java/com/netflix/concurrency/limits/limit/functions/Log10RootIntFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.netflix.concurrency.limits.limit.functions; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.function.IntUnaryOperator; | ||
|
||
public class Log10RootIntFunctionTest { | ||
@Test | ||
public void test0Index() { | ||
IntUnaryOperator func = Log10RootIntFunction.create(0); | ||
Assert.assertEquals(1, func.applyAsInt(0)); | ||
} | ||
|
||
@Test | ||
public void testInRange() { | ||
IntUnaryOperator func = Log10RootIntFunction.create(0); | ||
Assert.assertEquals(2, func.applyAsInt(100)); | ||
} | ||
|
||
@Test | ||
public void testOutofLookupRange() { | ||
IntUnaryOperator func = Log10RootIntFunction.create(0); | ||
Assert.assertEquals(4, func.applyAsInt(10000)); | ||
} | ||
} |