Skip to content

Commit 27e29ac

Browse files
committed
update maglev
Signed-off-by: dongjiang <[email protected]>
1 parent c6c089a commit 27e29ac

File tree

1 file changed

+11
-16
lines changed
  • cluster/loadbalance/maglevconsistenthashing

1 file changed

+11
-16
lines changed

cluster/loadbalance/maglevconsistenthashing/maglev.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package maglevconsistenthashing
1919

2020
import (
21+
"math"
22+
2123
"github.com/cespare/xxhash/v2"
2224

2325
"dubbo.apache.org/dubbo-go/v3/protocol"
@@ -109,14 +111,15 @@ func isPrime(num int) bool {
109111
if num < 2 {
110112
return false
111113
}
112-
if num <= 3 {
114+
if num == 2 {
113115
return true
114116
}
115-
if num%2 == 0 || num%3 == 0 {
117+
if num%2 == 0 {
116118
return false
117119
}
118-
for i := 5; i*i <= num; i += 6 {
119-
if num%i == 0 || num%(i+2) == 0 {
120+
sqrt := int(math.Sqrt(float64(num)))
121+
for i := 3; i <= sqrt; i += 2 {
122+
if num%i == 0 {
120123
return false
121124
}
122125
}
@@ -125,17 +128,9 @@ func isPrime(num int) bool {
125128

126129
// NextPrime 函数用于找出比 n 大的第一个素数
127130
func NextPrime(n int) int {
128-
if n <= 2 {
129-
return 2
130-
}
131-
num := n
132-
if num%2 == 0 {
133-
num++
134-
} else {
135-
num += 2
136-
}
137-
for !isPrime(num) {
138-
num += 2
131+
for num := n + 1; ; num++ {
132+
if isPrime(num) {
133+
return num
134+
}
139135
}
140-
return num
141136
}

0 commit comments

Comments
 (0)