Skip to content

Commit

Permalink
opt: improve the internal packages of math and bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Nov 27, 2022
1 parent 179cec8 commit f172799
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 72 deletions.
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (

"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/math"
"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/internal/socket"
"github.com/panjf2000/gnet/v2/internal/toolkit"
"github.com/panjf2000/gnet/v2/pkg/buffer/ring"
gerrors "github.com/panjf2000/gnet/v2/pkg/errors"
"github.com/panjf2000/gnet/v2/pkg/logging"
Expand Down Expand Up @@ -82,7 +82,7 @@ func NewClient(eventHandler EventHandler, opts ...Option) (cli *Client, err erro
case rbc <= ring.DefaultBufferSize:
options.ReadBufferCap = ring.DefaultBufferSize
default:
options.ReadBufferCap = toolkit.CeilToPowerOfTwo(rbc)
options.ReadBufferCap = math.CeilToPowerOfTwo(rbc)
}
wbc := options.WriteBufferCap
switch {
Expand All @@ -91,7 +91,7 @@ func NewClient(eventHandler EventHandler, opts ...Option) (cli *Client, err erro
case wbc <= ring.DefaultBufferSize:
options.WriteBufferCap = ring.DefaultBufferSize
default:
options.WriteBufferCap = toolkit.CeilToPowerOfTwo(wbc)
options.WriteBufferCap = math.CeilToPowerOfTwo(wbc)
}

el.buffer = make([]byte, options.ReadBufferCap)
Expand Down
10 changes: 5 additions & 5 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (

"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/bs"
gio "github.com/panjf2000/gnet/v2/internal/io"
"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/internal/socket"
"github.com/panjf2000/gnet/v2/internal/toolkit"
"github.com/panjf2000/gnet/v2/pkg/buffer/elastic"
gerrors "github.com/panjf2000/gnet/v2/pkg/errors"
bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
Expand Down Expand Up @@ -72,13 +72,13 @@ func (c *conn) releaseTCP() {
if addr, ok := c.localAddr.(*net.TCPAddr); ok && c.localAddr != c.loop.ln.addr {
bsPool.Put(addr.IP)
if len(addr.Zone) > 0 {
bsPool.Put(toolkit.StringToBytes(addr.Zone))
bsPool.Put(bs.StringToBytes(addr.Zone))
}
}
if addr, ok := c.remoteAddr.(*net.TCPAddr); ok {
bsPool.Put(addr.IP)
if len(addr.Zone) > 0 {
bsPool.Put(toolkit.StringToBytes(addr.Zone))
bsPool.Put(bs.StringToBytes(addr.Zone))
}
}
c.localAddr = nil
Expand Down Expand Up @@ -109,13 +109,13 @@ func (c *conn) releaseUDP() {
if addr, ok := c.localAddr.(*net.UDPAddr); ok && c.localAddr != c.loop.ln.addr {
bsPool.Put(addr.IP)
if len(addr.Zone) > 0 {
bsPool.Put(toolkit.StringToBytes(addr.Zone))
bsPool.Put(bs.StringToBytes(addr.Zone))
}
}
if addr, ok := c.remoteAddr.(*net.UDPAddr); ok {
bsPool.Put(addr.IP)
if len(addr.Zone) > 0 {
bsPool.Put(toolkit.StringToBytes(addr.Zone))
bsPool.Put(bs.StringToBytes(addr.Zone))
}
}
c.localAddr = nil
Expand Down
6 changes: 3 additions & 3 deletions gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"sync"
"time"

"github.com/panjf2000/gnet/v2/internal/toolkit"
"github.com/panjf2000/gnet/v2/internal/math"
"github.com/panjf2000/gnet/v2/pkg/buffer/ring"
"github.com/panjf2000/gnet/v2/pkg/errors"
"github.com/panjf2000/gnet/v2/pkg/logging"
Expand Down Expand Up @@ -403,7 +403,7 @@ func Run(eventHandler EventHandler, protoAddr string, opts ...Option) (err error
case rbc <= ring.DefaultBufferSize:
options.ReadBufferCap = ring.DefaultBufferSize
default:
options.ReadBufferCap = toolkit.CeilToPowerOfTwo(rbc)
options.ReadBufferCap = math.CeilToPowerOfTwo(rbc)
}
wbc := options.WriteBufferCap
switch {
Expand All @@ -412,7 +412,7 @@ func Run(eventHandler EventHandler, protoAddr string, opts ...Option) (err error
case wbc <= ring.DefaultBufferSize:
options.WriteBufferCap = ring.DefaultBufferSize
default:
options.WriteBufferCap = toolkit.CeilToPowerOfTwo(wbc)
options.WriteBufferCap = math.CeilToPowerOfTwo(wbc)
}

network, addr := parseProtoAddr(protoAddr)
Expand Down
2 changes: 1 addition & 1 deletion internal/toolkit/byteconv.go → internal/bs/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package toolkit
package bs

import (
"reflect"
Expand Down
73 changes: 73 additions & 0 deletions internal/math/math.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2022 Andy Pan.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package math

const (
bitSize = 32 << (^uint(0) >> 63)
maxintHeadBit = 1 << (bitSize - 2)
)

// IsPowerOfTwo reports whether given integer is a power of two.
func IsPowerOfTwo(n int) bool {
return n&(n-1) == 0
}

// CeilToPowerOfTwo returns n if it is a power-of-two, otherwise the next-highest power-of-two.
func CeilToPowerOfTwo(n int) int {
if n&maxintHeadBit != 0 && n > maxintHeadBit {
panic("argument is too large")
}

if n <= 2 {
return 2
}

n--
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
n++

return n
}

// FloorToPowerOfTwo returns n if it is a power-of-two, otherwise the next-highest power-of-two.
func FloorToPowerOfTwo(n int) int {
if n <= 2 {
return n
}

n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16

return n - (n >> 1)
}

// ClosestPowerOfTwo returns n if it is a power-of-two, otherwise the closest power-of-two.
func ClosestPowerOfTwo(n int) int {
next := CeilToPowerOfTwo(n)
if prev := next / 2; (n - prev) < (next - n) {
next = prev
}
return next
}
4 changes: 2 additions & 2 deletions internal/socket/socktoaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/toolkit"
"github.com/panjf2000/gnet/v2/internal/bs"
bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
)

Expand Down Expand Up @@ -101,5 +101,5 @@ func int2decimal(i uint) string {
bp--
b[bp] = byte(i%10) + '0'
}
return toolkit.BytesToString(b[bp:])
return bs.BytesToString(b[bp:])
}
52 changes: 0 additions & 52 deletions internal/toolkit/math.go

This file was deleted.

4 changes: 2 additions & 2 deletions load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"hash/crc32"
"net"

"github.com/panjf2000/gnet/v2/internal/toolkit"
"github.com/panjf2000/gnet/v2/internal/bs"
)

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

// hash converts a string to a unique hash code.
func (lb *sourceAddrHashLoadBalancer) hash(s string) int {
v := int(crc32.ChecksumIEEE(toolkit.StringToBytes(s)))
v := int(crc32.ChecksumIEEE(bs.StringToBytes(s)))
if v >= 0 {
return v
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/buffer/ring/ring_buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
"errors"
"io"

"github.com/panjf2000/gnet/v2/internal/toolkit"
"github.com/panjf2000/gnet/v2/internal/bs"
"github.com/panjf2000/gnet/v2/internal/math"
bsPool "github.com/panjf2000/gnet/v2/pkg/pool/byteslice"
)

Expand Down Expand Up @@ -56,7 +57,7 @@ func New(size int) *Buffer {
if size == 0 {
return &Buffer{bs: make([][]byte, 2), isEmpty: true}
}
size = toolkit.CeilToPowerOfTwo(size)
size = math.CeilToPowerOfTwo(size)
return &Buffer{
bs: make([][]byte, 2),
buf: make([]byte, size),
Expand Down Expand Up @@ -309,7 +310,7 @@ func (rb *Buffer) Available() int {

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

// Bytes returns all available read bytes. It does not move the read pointer and only copy the available data.
Expand Down Expand Up @@ -488,7 +489,7 @@ func (rb *Buffer) grow(newCap int) {
if newCap <= DefaultBufferSize {
newCap = DefaultBufferSize
} else {
newCap = toolkit.CeilToPowerOfTwo(newCap)
newCap = math.CeilToPowerOfTwo(newCap)
}
} else {
doubleCap := n + n
Expand Down

0 comments on commit f172799

Please sign in to comment.