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

v3: Use Named Fields Instead of Positional and Align Structures to Reduce Memory Usage #3079

Merged
merged 11 commits into from
Jul 23, 2024
Prev Previous commit
Next Next commit
Update struct alignment in test files
  • Loading branch information
gaby committed Jul 20, 2024
commit f3eb7d21577a5d5fd007bdbb06cb0a80fef209d7
4 changes: 2 additions & 2 deletions addon/retry/exponential_backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
func Test_ExponentialBackoff_Retry(t *testing.T) {
t.Parallel()
tests := []struct {
name string
expErr error
expBackoff *ExponentialBackoff
f func() error
expErr error
name string
}{
{
name: "With default values - successful",
Expand Down
40 changes: 20 additions & 20 deletions bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ func Test_Bind_Query(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Query struct {
ID int
Name string
Hobby []string
ID int
}
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand All @@ -53,14 +53,14 @@ func Test_Bind_Query(t *testing.T) {
require.Empty(t, empty.Hobby)

type Query2 struct {
Bool bool
ID int
Name string
Hobby string
FavouriteDrinks []string
Empty []string
Alloc []string
No []int64
ID int
Bool bool
}

c.Request().URI().SetQueryString("id=1&name=tom&hobby=basketball,football&favouriteDrinks=milo,coke,pepsi&alloc=&no=1")
Expand Down Expand Up @@ -237,8 +237,8 @@ func Test_Bind_Query_Schema(t *testing.T) {
require.Equal(t, "nested.age is empty", c.Bind().Query(q2).Error())

type Node struct {
Value int `query:"val,required"`
Next *Node `query:"next,required"`
Value int `query:"val,required"`
}
c.Request().URI().SetQueryString("val=1&next.val=3")
n := new(Node)
Expand Down Expand Up @@ -292,9 +292,9 @@ func Test_Bind_Header(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Header struct {
ID int
Name string
Hobby []string
ID int
}
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand All @@ -318,14 +318,14 @@ func Test_Bind_Header(t *testing.T) {
require.Empty(t, empty.Hobby)

type Header2 struct {
Bool bool
ID int
Name string
Hobby string
FavouriteDrinks []string
Empty []string
Alloc []string
No []int64
ID int
Bool bool
}

c.Request().Header.Add("id", "2")
Expand Down Expand Up @@ -502,8 +502,8 @@ func Test_Bind_Header_Schema(t *testing.T) {
require.Equal(t, "Nested.age is empty", c.Bind().Header(h2).Error())

type Node struct {
Value int `header:"Val,required"`
Next *Node `header:"Next,required"`
Value int `header:"Val,required"`
}
c.Request().Header.Add("Val", "1")
c.Request().Header.Add("Next.Val", "3")
Expand Down Expand Up @@ -533,9 +533,9 @@ func Test_Bind_RespHeader(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Header struct {
ID int
Name string
Hobby []string
ID int
}
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand All @@ -559,14 +559,14 @@ func Test_Bind_RespHeader(t *testing.T) {
require.Empty(t, empty.Hobby)

type Header2 struct {
Bool bool
ID int
Name string
Hobby string
FavouriteDrinks []string
Empty []string
Alloc []string
No []int64
ID int
Bool bool
}

c.Response().Header.Add("id", "2")
Expand Down Expand Up @@ -635,9 +635,9 @@ func Benchmark_Bind_Query(b *testing.B) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Query struct {
ID int
Name string
Hobby []string
ID int
}
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand Down Expand Up @@ -708,9 +708,9 @@ func Benchmark_Bind_Query_Comma(b *testing.B) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Query struct {
ID int
Name string
Hobby []string
ID int
}
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand All @@ -732,9 +732,9 @@ func Benchmark_Bind_Header(b *testing.B) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type ReqHeader struct {
ID int
Name string
Hobby []string
ID int
}
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand Down Expand Up @@ -782,9 +782,9 @@ func Benchmark_Bind_RespHeader(b *testing.B) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type ReqHeader struct {
ID int
Name string
Hobby []string
ID int
}
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand Down Expand Up @@ -1252,9 +1252,9 @@ func Test_Bind_Cookie(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Cookie struct {
ID int
Name string
Hobby []string
ID int
}
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand All @@ -1278,14 +1278,14 @@ func Test_Bind_Cookie(t *testing.T) {
require.Empty(t, empty.Hobby)

type Cookie2 struct {
Bool bool
ID int
Name string
Hobby string
FavouriteDrinks []string
Empty []string
Alloc []string
No []int64
ID int
Bool bool
}

c.Request().Header.SetCookie("id", "2")
Expand Down Expand Up @@ -1463,8 +1463,8 @@ func Test_Bind_Cookie_Schema(t *testing.T) {
require.Equal(t, "Nested.Age is empty", c.Bind().Cookie(h2).Error())

type Node struct {
Value int `cookie:"Val,required"`
Next *Node `cookie:"Next,required"`
Value int `cookie:"Val,required"`
}
c.Request().Header.SetCookie("Val", "1")
c.Request().Header.SetCookie("Next.Val", "3")
Expand Down Expand Up @@ -1495,9 +1495,9 @@ func Benchmark_Bind_Cookie(b *testing.B) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Cookie struct {
ID int
Name string
Hobby []string
ID int
}
c.Request().SetBody([]byte(``))
c.Request().Header.SetContentType("")
Expand Down
10 changes: 5 additions & 5 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,8 @@ func Test_Client_Cookie(t *testing.T) {
t.Run("set cookies with struct", func(t *testing.T) {
t.Parallel()
type args struct {
CookieInt int `cookie:"int"`
CookieString string `cookie:"string"`
CookieInt int `cookie:"int"`
}

req := New().SetCookiesWithStruct(&args{
Expand Down Expand Up @@ -1087,12 +1087,12 @@ func Test_Client_QueryParam(t *testing.T) {
t.Parallel()

type args struct {
TInt int
TString string
TFloat float64
TBool bool
TSlice []string
TIntSlice []int `param:"int_slice"`
TInt int
TFloat float64
TBool bool
}

p := New()
Expand Down Expand Up @@ -1195,8 +1195,8 @@ func Test_Client_PathParam(t *testing.T) {
t.Run("set path params with struct", func(t *testing.T) {
t.Parallel()
type args struct {
CookieInt int `path:"int"`
CookieString string `path:"string"`
CookieInt int `path:"int"`
}

req := New().SetPathParamsWithStruct(&args{
Expand Down
2 changes: 1 addition & 1 deletion client/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func Test_AddMissing_Port(t *testing.T) {
}
tests := []struct {
name string
args args
want string
args args
}{
{
name: "do anything",
Expand Down
28 changes: 14 additions & 14 deletions client/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ func Test_Request_QueryParam(t *testing.T) {
t.Parallel()

type args struct {
TInt int
TString string
TFloat float64
TBool bool
TSlice []string
TIntSlice []int `param:"int_slice"`
TInt int
TFloat float64
TBool bool
}

p := AcquireRequest()
Expand Down Expand Up @@ -334,8 +334,8 @@ func Test_Request_Cookie(t *testing.T) {
t.Run("set cookies with struct", func(t *testing.T) {
t.Parallel()
type args struct {
CookieInt int `cookie:"int"`
CookieString string `cookie:"string"`
CookieInt int `cookie:"int"`
}

req := AcquireRequest().SetCookiesWithStruct(&args{
Expand Down Expand Up @@ -396,8 +396,8 @@ func Test_Request_PathParam(t *testing.T) {
t.Run("set path params with struct", func(t *testing.T) {
t.Parallel()
type args struct {
CookieInt int `path:"int"`
CookieString string `path:"string"`
CookieInt int `path:"int"`
}

req := AcquireRequest().SetPathParamsWithStruct(&args{
Expand Down Expand Up @@ -510,12 +510,12 @@ func Test_Request_FormData(t *testing.T) {
t.Parallel()

type args struct {
TInt int
TString string
TFloat float64
TBool bool
TSlice []string
TIntSlice []int `form:"int_slice"`
TInt int
TFloat float64
TBool bool
}

p := AcquireRequest()
Expand Down Expand Up @@ -1299,13 +1299,13 @@ func Test_SetValWithStruct(t *testing.T) {

// test SetValWithStruct vai QueryParam struct.
type args struct {
TString string
TSlice []string
TIntSlice []int `param:"int_slice"`
unexport int
TInt int
TString string
TFloat float64
TBool bool
TSlice []string
TIntSlice []int `param:"int_slice"`
}

t.Run("the struct should be applied", func(t *testing.T) {
Expand Down Expand Up @@ -1453,13 +1453,13 @@ func Test_SetValWithStruct(t *testing.T) {
func Benchmark_SetValWithStruct(b *testing.B) {
// test SetValWithStruct vai QueryParam struct.
type args struct {
TString string
TSlice []string
TIntSlice []int `param:"int_slice"`
unexport int
TInt int
TString string
TFloat float64
TBool bool
TSlice []string
TIntSlice []int `param:"int_slice"`
}

b.Run("the struct should be applied", func(b *testing.B) {
Expand Down
14 changes: 7 additions & 7 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,8 @@ func Benchmark_Ctx_Body_With_Compression(b *testing.B) {
}
)
compressionTests := []struct {
contentEncoding string
compressWriter func([]byte) ([]byte, error)
contentEncoding string
}{
{
contentEncoding: "gzip",
Expand Down Expand Up @@ -702,8 +702,8 @@ func Benchmark_Ctx_Body_With_Compression_Immutable(b *testing.B) {
}
)
compressionTests := []struct {
contentEncoding string
compressWriter func([]byte) ([]byte, error)
contentEncoding string
}{
{
contentEncoding: "gzip",
Expand Down Expand Up @@ -1123,9 +1123,9 @@ func Test_Ctx_AutoFormat_Struct(t *testing.T) {
c := app.AcquireCtx(&fasthttp.RequestCtx{})

type Message struct {
Recipients []string
Sender string `xml:"sender,attr"`
Urgency int `xml:"urgency,attr"`
Recipients []string
Urgency int `xml:"urgency,attr"`
}
data := Message{
Recipients: []string{"Alice", "Bob"},
Expand Down Expand Up @@ -1370,11 +1370,11 @@ func Test_Ctx_Binders(t *testing.T) {
}

type TestStruct struct {
Name string
NameWithDefault string `json:"name2" xml:"Name2" form:"name2" cookie:"name2" query:"name2" params:"name2" header:"Name2"`
TestEmbeddedStruct
Name string
Class int
NameWithDefault string `json:"name2" xml:"Name2" form:"name2" cookie:"name2" query:"name2" params:"name2" header:"Name2"`
ClassWithDefault int `json:"class2" xml:"Class2" form:"class2" cookie:"class2" query:"class2" params:"class2" header:"Class2"`
ClassWithDefault int `json:"class2" xml:"Class2" form:"class2" cookie:"class2" query:"class2" params:"class2" header:"Class2"`
}

withValues := func(t *testing.T, actionFn func(c Ctx, testStruct *TestStruct) error) {
Expand Down
Loading