Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use less memory by structs #428

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions dtls/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ var DefaultConfig = func() Config {
}()

type Config struct {
CreateInactivityMonitor func() udpClient.InactivityMonitor
GetMID GetMIDFunc
Handler HandlerFunc
OnNewConn OnNewConnFunc
config.Common[*udpClient.Conn]
CreateInactivityMonitor func() udpClient.InactivityMonitor
GetMID GetMIDFunc
Handler HandlerFunc
OnNewConn OnNewConnFunc
TransmissionNStart uint32
TransmissionAcknowledgeTimeout time.Duration
TransmissionNStart uint32
TransmissionMaxRetransmit uint32
MTU uint16
}
4 changes: 2 additions & 2 deletions dtls/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ type Listener interface {
}

type Server struct {
cfg *Config
ctx context.Context
cancel context.CancelFunc
cfg *Config

listenMutex sync.Mutex
listen Listener
listenMutex sync.Mutex
}

// A Option sets options such as credentials, codec and keepalive parameters, etc.
Expand Down
23 changes: 8 additions & 15 deletions dtls/server/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,15 @@ import (
type EventFunc = func()

type Session struct {
onClose []EventFunc

ctx atomic.Value // TODO: change to atomic.Pointer[context.Context] for go1.19

cancel context.CancelFunc
connection *coapNet.Conn

done chan struct{}

mutex sync.Mutex

ctx atomic.Value // TODO: change to atomic.Pointer[context.Context] for go1.19
cancel context.CancelFunc
connection *coapNet.Conn
done chan struct{}
onClose []EventFunc
mutex sync.Mutex
maxMessageSize uint32

mtu uint16

closeSocket bool
mtu uint16
closeSocket bool
}

func NewSession(
Expand Down
2 changes: 1 addition & 1 deletion message/encodeDecodeUint32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ func TestEncodeUint32(t *testing.T) {
}
tests := []struct {
name string
args args
want int
args args
wantErr bool
}{
{
Expand Down
14 changes: 6 additions & 8 deletions message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import (
const MaxTokenSize = 8

type Message struct {
Token Token
Options Options
Code codes.Code
Payload []byte

// For DTLS and UDP messages
MessageID int32 // uint16 is valid, all other values are invalid, -1 is used for unset
Type Type // uint8 is valid, all other values are invalid, -1 is used for unset
Token Token
Options Options
Payload []byte
MessageID int32
Code codes.Code
Type Type
}

func (r *Message) String() string {
Expand Down
2 changes: 1 addition & 1 deletion message/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ func TestGetPathBufferSize(t *testing.T) {
tests := []struct {
name string
args args
wantErr bool
want string
wantErr bool
}{
{
name: "Empty",
Expand Down
17 changes: 9 additions & 8 deletions message/pool/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,16 @@ type Decoder interface {
}

type Message struct {
// Context context of request.
ctx context.Context
msg message.Message
hijacked atomic.Bool
isModified bool
body io.ReadSeeker
valueBuffer []byte
origValueBuffer []byte
body io.ReadSeeker
sequence uint64

// local vars
bufferUnmarshal []byte
bufferMarshal []byte
msg message.Message
hijacked atomic.Bool
sequence uint64
isModified bool
}

const valueBufferSize = 256
Expand Down Expand Up @@ -575,6 +572,10 @@ func (r *Message) Clone(msg *Message) error {
if err != nil {
return err
}
_, err = r.body.Seek(0, io.SeekStart)
if err != nil {
return err
}
_, err = io.Copy(buf, r.Body())
if err != nil {
var errs *multierror.Error
Expand Down
2 changes: 1 addition & 1 deletion message/pool/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func TestMessageSetPath(t *testing.T) {
tests := []struct {
name string
args args
wantErr bool
want string
wantErr bool
}{
{
name: "Empty",
Expand Down
2 changes: 0 additions & 2 deletions message/pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
)

type Pool struct {
// This field needs to be the first in the struct to ensure proper word alignment on 32-bit platforms.
// See: https://golang.org/pkg/sync/atomic/#pkg-note-BUG
currentMessagesInPool atomic.Int64
messagePool sync.Pool
maxNumMessages uint32
Expand Down
7 changes: 3 additions & 4 deletions mux/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ func (f HandlerFunc) ServeCOAP(w ResponseWriter, r *Message) {
// with same name.
// Router is also safe for concurrent access from multiple goroutines.
type Router struct {
middlewares []MiddlewareFunc
errors ErrorFunc

m *sync.RWMutex
errors ErrorFunc
defaultHandler Handler // guarded by m
z map[string]Route // guarded by m
middlewares []MiddlewareFunc
}

type Route struct {
h Handler
pattern string
regexMatcher *routeRegexp
pattern string
}

func (route *Route) GetRouteRegexp() (string, error) {
Expand Down
Loading