Skip to content

Commit f172799

Browse files
committed
opt: improve the internal packages of math and bytes
1 parent 179cec8 commit f172799

File tree

9 files changed

+94
-72
lines changed

9 files changed

+94
-72
lines changed

client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import (
2727

2828
"golang.org/x/sys/unix"
2929

30+
"github.com/panjf2000/gnet/v2/internal/math"
3031
"github.com/panjf2000/gnet/v2/internal/netpoll"
3132
"github.com/panjf2000/gnet/v2/internal/socket"
32-
"github.com/panjf2000/gnet/v2/internal/toolkit"
3333
"github.com/panjf2000/gnet/v2/pkg/buffer/ring"
3434
gerrors "github.com/panjf2000/gnet/v2/pkg/errors"
3535
"github.com/panjf2000/gnet/v2/pkg/logging"
@@ -82,7 +82,7 @@ func NewClient(eventHandler EventHandler, opts ...Option) (cli *Client, err erro
8282
case rbc <= ring.DefaultBufferSize:
8383
options.ReadBufferCap = ring.DefaultBufferSize
8484
default:
85-
options.ReadBufferCap = toolkit.CeilToPowerOfTwo(rbc)
85+
options.ReadBufferCap = math.CeilToPowerOfTwo(rbc)
8686
}
8787
wbc := options.WriteBufferCap
8888
switch {
@@ -91,7 +91,7 @@ func NewClient(eventHandler EventHandler, opts ...Option) (cli *Client, err erro
9191
case wbc <= ring.DefaultBufferSize:
9292
options.WriteBufferCap = ring.DefaultBufferSize
9393
default:
94-
options.WriteBufferCap = toolkit.CeilToPowerOfTwo(wbc)
94+
options.WriteBufferCap = math.CeilToPowerOfTwo(wbc)
9595
}
9696

9797
el.buffer = make([]byte, options.ReadBufferCap)

connection.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ import (
2626

2727
"golang.org/x/sys/unix"
2828

29+
"github.com/panjf2000/gnet/v2/internal/bs"
2930
gio "github.com/panjf2000/gnet/v2/internal/io"
3031
"github.com/panjf2000/gnet/v2/internal/netpoll"
3132
"github.com/panjf2000/gnet/v2/internal/socket"
32-
"github.com/panjf2000/gnet/v2/internal/toolkit"
3333
"github.com/panjf2000/gnet/v2/pkg/buffer/elastic"
3434
gerrors "github.com/panjf2000/gnet/v2/pkg/errors"
3535
bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
@@ -72,13 +72,13 @@ func (c *conn) releaseTCP() {
7272
if addr, ok := c.localAddr.(*net.TCPAddr); ok && c.localAddr != c.loop.ln.addr {
7373
bsPool.Put(addr.IP)
7474
if len(addr.Zone) > 0 {
75-
bsPool.Put(toolkit.StringToBytes(addr.Zone))
75+
bsPool.Put(bs.StringToBytes(addr.Zone))
7676
}
7777
}
7878
if addr, ok := c.remoteAddr.(*net.TCPAddr); ok {
7979
bsPool.Put(addr.IP)
8080
if len(addr.Zone) > 0 {
81-
bsPool.Put(toolkit.StringToBytes(addr.Zone))
81+
bsPool.Put(bs.StringToBytes(addr.Zone))
8282
}
8383
}
8484
c.localAddr = nil
@@ -109,13 +109,13 @@ func (c *conn) releaseUDP() {
109109
if addr, ok := c.localAddr.(*net.UDPAddr); ok && c.localAddr != c.loop.ln.addr {
110110
bsPool.Put(addr.IP)
111111
if len(addr.Zone) > 0 {
112-
bsPool.Put(toolkit.StringToBytes(addr.Zone))
112+
bsPool.Put(bs.StringToBytes(addr.Zone))
113113
}
114114
}
115115
if addr, ok := c.remoteAddr.(*net.UDPAddr); ok {
116116
bsPool.Put(addr.IP)
117117
if len(addr.Zone) > 0 {
118-
bsPool.Put(toolkit.StringToBytes(addr.Zone))
118+
bsPool.Put(bs.StringToBytes(addr.Zone))
119119
}
120120
}
121121
c.localAddr = nil

gnet.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"sync"
2424
"time"
2525

26-
"github.com/panjf2000/gnet/v2/internal/toolkit"
26+
"github.com/panjf2000/gnet/v2/internal/math"
2727
"github.com/panjf2000/gnet/v2/pkg/buffer/ring"
2828
"github.com/panjf2000/gnet/v2/pkg/errors"
2929
"github.com/panjf2000/gnet/v2/pkg/logging"
@@ -403,7 +403,7 @@ func Run(eventHandler EventHandler, protoAddr string, opts ...Option) (err error
403403
case rbc <= ring.DefaultBufferSize:
404404
options.ReadBufferCap = ring.DefaultBufferSize
405405
default:
406-
options.ReadBufferCap = toolkit.CeilToPowerOfTwo(rbc)
406+
options.ReadBufferCap = math.CeilToPowerOfTwo(rbc)
407407
}
408408
wbc := options.WriteBufferCap
409409
switch {
@@ -412,7 +412,7 @@ func Run(eventHandler EventHandler, protoAddr string, opts ...Option) (err error
412412
case wbc <= ring.DefaultBufferSize:
413413
options.WriteBufferCap = ring.DefaultBufferSize
414414
default:
415-
options.WriteBufferCap = toolkit.CeilToPowerOfTwo(wbc)
415+
options.WriteBufferCap = math.CeilToPowerOfTwo(wbc)
416416
}
417417

418418
network, addr := parseProtoAddr(protoAddr)

internal/toolkit/byteconv.go renamed to internal/bs/bs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package toolkit
15+
package bs
1616

1717
import (
1818
"reflect"

internal/math/math.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2022 Andy Pan.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package math
19+
20+
const (
21+
bitSize = 32 << (^uint(0) >> 63)
22+
maxintHeadBit = 1 << (bitSize - 2)
23+
)
24+
25+
// IsPowerOfTwo reports whether given integer is a power of two.
26+
func IsPowerOfTwo(n int) bool {
27+
return n&(n-1) == 0
28+
}
29+
30+
// CeilToPowerOfTwo returns n if it is a power-of-two, otherwise the next-highest power-of-two.
31+
func CeilToPowerOfTwo(n int) int {
32+
if n&maxintHeadBit != 0 && n > maxintHeadBit {
33+
panic("argument is too large")
34+
}
35+
36+
if n <= 2 {
37+
return 2
38+
}
39+
40+
n--
41+
n |= n >> 1
42+
n |= n >> 2
43+
n |= n >> 4
44+
n |= n >> 8
45+
n |= n >> 16
46+
n++
47+
48+
return n
49+
}
50+
51+
// FloorToPowerOfTwo returns n if it is a power-of-two, otherwise the next-highest power-of-two.
52+
func FloorToPowerOfTwo(n int) int {
53+
if n <= 2 {
54+
return n
55+
}
56+
57+
n |= n >> 1
58+
n |= n >> 2
59+
n |= n >> 4
60+
n |= n >> 8
61+
n |= n >> 16
62+
63+
return n - (n >> 1)
64+
}
65+
66+
// ClosestPowerOfTwo returns n if it is a power-of-two, otherwise the closest power-of-two.
67+
func ClosestPowerOfTwo(n int) int {
68+
next := CeilToPowerOfTwo(n)
69+
if prev := next / 2; (n - prev) < (next - n) {
70+
next = prev
71+
}
72+
return next
73+
}

internal/socket/socktoaddr.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
"golang.org/x/sys/unix"
2424

25-
"github.com/panjf2000/gnet/v2/internal/toolkit"
25+
"github.com/panjf2000/gnet/v2/internal/bs"
2626
bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
2727
)
2828

@@ -101,5 +101,5 @@ func int2decimal(i uint) string {
101101
bp--
102102
b[bp] = byte(i%10) + '0'
103103
}
104-
return toolkit.BytesToString(b[bp:])
104+
return bs.BytesToString(b[bp:])
105105
}

internal/toolkit/math.go

-52
This file was deleted.

load_balancer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"hash/crc32"
1919
"net"
2020

21-
"github.com/panjf2000/gnet/v2/internal/toolkit"
21+
"github.com/panjf2000/gnet/v2/internal/bs"
2222
)
2323

2424
// LoadBalancing represents the type of load-balancing algorithm.
@@ -141,7 +141,7 @@ func (lb *sourceAddrHashLoadBalancer) register(el *eventloop) {
141141

142142
// hash converts a string to a unique hash code.
143143
func (lb *sourceAddrHashLoadBalancer) hash(s string) int {
144-
v := int(crc32.ChecksumIEEE(toolkit.StringToBytes(s)))
144+
v := int(crc32.ChecksumIEEE(bs.StringToBytes(s)))
145145
if v >= 0 {
146146
return v
147147
}

pkg/buffer/ring/ring_buffer.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import (
2323
"errors"
2424
"io"
2525

26-
"github.com/panjf2000/gnet/v2/internal/toolkit"
26+
"github.com/panjf2000/gnet/v2/internal/bs"
27+
"github.com/panjf2000/gnet/v2/internal/math"
2728
bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
2829
)
2930

@@ -56,7 +57,7 @@ func New(size int) *Buffer {
5657
if size == 0 {
5758
return &Buffer{bs: make([][]byte, 2), isEmpty: true}
5859
}
59-
size = toolkit.CeilToPowerOfTwo(size)
60+
size = math.CeilToPowerOfTwo(size)
6061
return &Buffer{
6162
bs: make([][]byte, 2),
6263
buf: make([]byte, size),
@@ -309,7 +310,7 @@ func (rb *Buffer) Available() int {
309310

310311
// WriteString writes the contents of the string s to buffer, which accepts a slice of bytes.
311312
func (rb *Buffer) WriteString(s string) (int, error) {
312-
return rb.Write(toolkit.StringToBytes(s))
313+
return rb.Write(bs.StringToBytes(s))
313314
}
314315

315316
// Bytes returns all available read bytes. It does not move the read pointer and only copy the available data.
@@ -488,7 +489,7 @@ func (rb *Buffer) grow(newCap int) {
488489
if newCap <= DefaultBufferSize {
489490
newCap = DefaultBufferSize
490491
} else {
491-
newCap = toolkit.CeilToPowerOfTwo(newCap)
492+
newCap = math.CeilToPowerOfTwo(newCap)
492493
}
493494
} else {
494495
doubleCap := n + n

0 commit comments

Comments
 (0)