fieldalignment of all structs to save memory

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2021-04-27 08:32:47 +03:00
parent ee11f39a2f
commit 86626c5922
78 changed files with 343 additions and 410 deletions

View File

@@ -5,9 +5,7 @@ import (
"net"
)
var (
privateBlocks []*net.IPNet
)
var privateBlocks []*net.IPNet
func init() {
for _, b := range []string{"10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "100.64.0.0/10", "fd00::/8"} {

View File

@@ -54,7 +54,6 @@ func TestExtractor(t *testing.T) {
t.Errorf("Expected %s got %s", d.expect, addr)
}
}
}
func TestAppendPrivateBlocks(t *testing.T) {

View File

@@ -4,20 +4,20 @@ import (
"bytes"
)
type buffer struct {
type Buffer struct {
*bytes.Buffer
}
// Close reset buffer contents
func (b *buffer) Close() error {
func (b *Buffer) Close() error {
b.Buffer.Reset()
return nil
}
// New creates new buffer that satisfies Closer interface
func New(b *bytes.Buffer) *buffer {
func New(b *bytes.Buffer) *Buffer {
if b == nil {
b = bytes.NewBuffer(nil)
}
return &buffer{b}
return &Buffer{b}
}

View File

@@ -27,7 +27,6 @@ func HostPort(addr string, port interface{}) string {
// Listen takes addr:portmin-portmax and binds to the first available port
// Example: Listen("localhost:5000-6000", fn)
func Listen(addr string, fn func(string) (net.Listener, error)) (net.Listener, error) {
if strings.Count(addr, ":") == 1 && strings.Count(addr, "-") == 0 {
return fn(addr)
}

View File

@@ -22,5 +22,4 @@ func TestListen(t *testing.T) {
// TODO nats case test
// natsAddr := "_INBOX.bID2CMRvlNp0vt4tgNBHWf"
// Expect addr DO NOT has extra ":" at the end!
}

View File

@@ -8,9 +8,8 @@ import (
"crypto/rand"
"crypto/x509"
"encoding/pem"
"fmt"
"errors"
"fmt"
)
// GenerateKey returns an ed25519 key
@@ -46,14 +45,14 @@ func CA(opts ...CertOption) ([]byte, []byte, error) {
return nil, nil, err
}
cert, key := &bytes.Buffer{}, &bytes.Buffer{}
if err := pem.Encode(cert, &pem.Block{Type: "CERTIFICATE", Bytes: x509Cert}); err != nil {
if err = pem.Encode(cert, &pem.Block{Type: "CERTIFICATE", Bytes: x509Cert}); err != nil {
return nil, nil, err
}
x509Key, err := x509.MarshalPKCS8PrivateKey(options.Priv)
if err != nil {
return nil, nil, err
}
if err := pem.Encode(key, &pem.Block{Type: "PRIVATE KEY", Bytes: x509Key}); err != nil {
if err = pem.Encode(key, &pem.Block{Type: "PRIVATE KEY", Bytes: x509Key}); err != nil {
return nil, nil, err
}

View File

@@ -63,9 +63,7 @@ func Unmarshal(dst interface{}, query string) error {
// possible. Eg the example above would output:
// {"bar":{"one":{"two":2,"red":112}}}
func ToJSON(query string) ([]byte, error) {
var (
builder interface{} = make(map[string]interface{})
)
var builder interface{} = make(map[string]interface{})
params := strings.Split(query, "&")
for _, part := range params {
tempMap, err := queryToMap(part)

View File

@@ -32,9 +32,10 @@ type unmarshalT struct {
A string `json:"a"`
B unmarshalB `json:"b"`
}
type unmarshalB struct {
C int `json:"c"`
D string `json:"D"`
C int `json:"c"`
}
func TestUnmarshal(t *testing.T) {

View File

@@ -11,7 +11,7 @@ type Rand struct {
}
func (r *Rand) Int31() int32 {
rand.Read(r.buf[:4])
_, _ = rand.Read(r.buf[:4])
return int32(binary.BigEndian.Uint32(r.buf[:4]) & ^uint32(1<<31))
}
@@ -54,7 +54,7 @@ func (r *Rand) Intn(n int) int {
}
func (r *Rand) Int63() int64 {
rand.Read(r.buf[:])
_, _ = rand.Read(r.buf[:])
return int64(binary.BigEndian.Uint64(r.buf[:]) & ^uint64(1<<63))
}

View File

@@ -5,7 +5,6 @@ import (
)
func TestPath(t *testing.T) {
type Nested2 struct {
Name string
}

View File

@@ -9,14 +9,10 @@ import (
"strings"
)
var (
// ErrInvalidParam specifies invalid url query params
ErrInvalidParam = errors.New("invalid url query param provided")
)
// ErrInvalidParam specifies invalid url query params
var ErrInvalidParam = errors.New("invalid url query param provided")
var (
bracketSplitter = regexp.MustCompile(`\[|\]`)
)
var bracketSplitter = regexp.MustCompile(`\[|\]`)
// StructFields returns slice of struct fields
func StructFields(src interface{}) ([]reflect.StructField, error) {
@@ -157,7 +153,7 @@ func StructURLValues(src interface{}, pref string, tags []string) (url.Values, e
case reflect.Slice:
for i := 0; i < val.Len(); i++ {
va := val.Index(i)
//if va.Type().Elem().Kind() != reflect.Ptr {
// if va.Type().Elem().Kind() != reflect.Ptr {
if va.Kind() != reflect.Ptr {
data.Set(t.name, fmt.Sprintf("%v", va.Interface()))
continue
@@ -193,9 +189,7 @@ func StructURLValues(src interface{}, pref string, tags []string) (url.Values, e
// URLMap returns map of url query params
func URLMap(query string) (map[string]interface{}, error) {
var (
mp interface{} = make(map[string]interface{})
)
var mp interface{} = make(map[string]interface{})
params := strings.Split(query, "&")

View File

@@ -7,9 +7,9 @@ import (
func TestStructURLValues(t *testing.T) {
type Str struct {
Str *Str `json:"str"`
Name string `json:"name"`
Args []int `json:"args"`
Str *Str `json:"str"`
}
val := &Str{Name: "test_name", Args: []int{1, 2, 3}, Str: &Str{Name: "nested_name"}}
@@ -90,8 +90,8 @@ func TestIsZero(t *testing.T) {
Nested string
}
type testStr2 struct {
Name string
Nested *testStr3
Name string
}
vtest := &testStr2{
Name: "test_name",
@@ -106,5 +106,5 @@ func TestIsZero(t *testing.T) {
t.Fatalf("non zero ret on zero struct: %#+v", vtest)
}
//t.Logf("XX %#+v\n", ok)
// t.Logf("XX %#+v\n", ok)
}

View File

@@ -5,11 +5,11 @@ import (
)
func addNodes(old, neu []*register.Node) []*register.Node {
nodes := make([]*register.Node, len(neu))
nodes := make([]*register.Node, 0, len(neu))
// add all new nodes
for i, n := range neu {
for _, n := range neu {
node := *n
nodes[i] = &node
nodes = append(nodes, &node)
}
// look at old nodes
@@ -19,7 +19,7 @@ func addNodes(old, neu []*register.Node) []*register.Node {
// check against new nodes
for _, n := range nodes {
// ids match then skip
if o.Id == n.Id {
if o.ID == n.ID {
exists = true
break
}
@@ -40,7 +40,7 @@ func delNodes(old, del []*register.Node) []*register.Node {
for _, o := range old {
var rem bool
for _, n := range del {
if o.Id == n.Id {
if o.ID == n.ID {
rem = true
break
}

View File

@@ -14,7 +14,7 @@ func TestRemove(t *testing.T) {
Version: "1.0.0",
Nodes: []*register.Node{
{
Id: "foo-123",
ID: "foo-123",
Address: "localhost:9999",
},
},
@@ -24,7 +24,7 @@ func TestRemove(t *testing.T) {
Version: "1.0.0",
Nodes: []*register.Node{
{
Id: "foo-123",
ID: "foo-123",
Address: "localhost:6666",
},
},
@@ -47,11 +47,11 @@ func TestRemoveNodes(t *testing.T) {
Version: "1.0.0",
Nodes: []*register.Node{
{
Id: "foo-123",
ID: "foo-123",
Address: "localhost:9999",
},
{
Id: "foo-321",
ID: "foo-321",
Address: "localhost:6666",
},
},
@@ -61,7 +61,7 @@ func TestRemoveNodes(t *testing.T) {
Version: "1.0.0",
Nodes: []*register.Node{
{
Id: "foo-123",
ID: "foo-123",
Address: "localhost:6666",
},
},

View File

@@ -77,7 +77,7 @@ func (t template) Compile() Template {
rawOps = append(rawOps, s.compile()...)
}
//ops := make([]int, 0, len(rawOps))
// ops := make([]int, 0, len(rawOps))
var (
ops []int
pool []string

View File

@@ -50,9 +50,7 @@ func tokenize(path string) (tokens []string, verb string) {
field
nested
)
var (
st = init
)
st := init
for path != "" {
var idx int
switch st {

View File

@@ -209,7 +209,8 @@ func TestParseSegments(t *testing.T) {
"a", "/", "b", "/", "*", "/", "c",
"}", "/",
"**",
eof},
eof,
},
want: []segment{
literal("v1"),
variable{

View File

@@ -40,7 +40,7 @@ func (c *syncStore) processQueue(index int) {
}
var opts []store.WriteOption
if !ir.expiresAt.IsZero() {
opts = append(opts, store.WriteTTL(ir.expiresAt.Sub(time.Now())))
opts = append(opts, store.WriteTTL(time.Until(ir.expiresAt)))
}
// Todo = internal queue also has to hold the corresponding store.WriteOptions
if err := c.syncOpts.Stores[index+1].Write(c.storeOpts.Context, ir.key, ir.value, opts...); err != nil {

View File

@@ -17,10 +17,8 @@ type Basic struct {
store store.Store
}
var (
// StorePrefix to isolate tokens
StorePrefix = "tokens/"
)
// StorePrefix to isolate tokens
var StorePrefix = "tokens/"
// NewTokenProvider returns an initialized basic provider
func NewTokenProvider(opts ...token.Option) token.Provider {

View File

@@ -83,5 +83,4 @@ func TestInspect(t *testing.T) {
t.Fatalf("Inspect returned %v error, expected %v", err, token.ErrInvalidToken)
}
})
}

View File

@@ -46,7 +46,7 @@ func NewOptions(opts ...Option) Options {
for _, o := range opts {
o(&options)
}
//set default store
// set default store
if options.Store == nil {
options.Store = store.DefaultStore
}
@@ -75,7 +75,7 @@ func NewGenerateOptions(opts ...GenerateOption) GenerateOptions {
for _, o := range opts {
o(&options)
}
//set default Expiry of token
// set default Expiry of token
if options.Expiry == 0 {
options.Expiry = time.Minute * 15
}