-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from kilink/vegas-limit-int-unary-operator
Use IntUnaryOperator / DoubleUnaryOperator in VegasLimit
- Loading branch information
Showing
5 changed files
with
215 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
37 changes: 37 additions & 0 deletions
37
...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,37 @@ | ||
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 Log10RootIntFunction() {} | ||
|
||
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
Oops, something went wrong.