-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make slab sizes and capacity for fixed size memory pool configurable
Signed-off-by: Christian Haudum <[email protected]>
- Loading branch information
Showing
9 changed files
with
281 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,51 @@ | ||
package mempool | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/c2h5oh/datasize" | ||
) | ||
|
||
type Bucket struct { | ||
Size, Capacity int | ||
Size int | ||
Capacity uint64 | ||
} | ||
|
||
func (b Bucket) Parse(s string) (any, error) { | ||
parts := strings.Split(s, "x") | ||
if len(parts) != 2 { | ||
return nil, errors.New("bucket must be in format {count}x{bytes}") | ||
} | ||
|
||
size, err := strconv.Atoi(parts[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
capacity, err := datasize.ParseString(parts[1]) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
return Bucket{ | ||
Size: size, | ||
Capacity: uint64(capacity), | ||
}, nil | ||
} | ||
|
||
func (b Bucket) String() string { | ||
return fmt.Sprintf("%dx%s", b.Size, datasize.ByteSize(b.Capacity).String()) | ||
} | ||
|
||
type Buckets []Bucket | ||
|
||
func (b Buckets) String() string { | ||
s := make([]string, 0, len(b)) | ||
for i := range b { | ||
s = append(s, b[i].String()) | ||
} | ||
return strings.Join(s, ",") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package flagext | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
type ListValue interface { | ||
String() string | ||
Parse(s string) (any, error) | ||
} | ||
|
||
// StringSliceCSV is a slice of strings that is parsed from a comma-separated string | ||
// It implements flag.Value and yaml Marshalers | ||
type CSV[T ListValue] []T | ||
|
||
// String implements flag.Value | ||
func (v CSV[T]) String() string { | ||
s := make([]string, 0, len(v)) | ||
for i := range v { | ||
s = append(s, v[i].String()) | ||
} | ||
return strings.Join(s, ",") | ||
} | ||
|
||
// Set implements flag.Value | ||
func (v *CSV[T]) Set(s string) error { | ||
if len(s) == 0 { | ||
*v = nil | ||
return nil | ||
} | ||
var zero T | ||
values := strings.Split(s, ",") | ||
for _, val := range values { | ||
el, err := zero.Parse(val) | ||
if err != nil { | ||
return err | ||
} | ||
*v = append(*v, el.(T)) | ||
} | ||
return nil | ||
} | ||
|
||
// String implements flag.Getter | ||
func (v CSV[T]) Get() []T { | ||
return v | ||
} | ||
|
||
// UnmarshalYAML implements yaml.Unmarshaler. | ||
func (v *CSV[T]) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var s string | ||
if err := unmarshal(&s); err != nil { | ||
return err | ||
} | ||
|
||
return v.Set(s) | ||
} | ||
|
||
// MarshalYAML implements yaml.Marshaler. | ||
func (v CSV[T]) MarshalYAML() (interface{}, error) { | ||
return v.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package flagext | ||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type customType int | ||
|
||
// Parse implements ListValue. | ||
func (l customType) Parse(s string) (any, error) { | ||
v, err := strconv.Atoi(s) | ||
if err != nil { | ||
return customType(0), err | ||
} | ||
return customType(v), nil | ||
} | ||
|
||
// String implements ListValue. | ||
func (l customType) String() string { | ||
return strconv.Itoa(int(l)) | ||
} | ||
|
||
var _ ListValue = customType(0) | ||
|
||
func Test_CSV(t *testing.T) { | ||
for _, tc := range []struct { | ||
in string | ||
err bool | ||
out []customType | ||
}{ | ||
{ | ||
in: "", | ||
err: false, | ||
out: nil, | ||
}, | ||
{ | ||
in: ",", | ||
err: true, | ||
out: []customType{}, | ||
}, | ||
{ | ||
in: "1", | ||
err: false, | ||
out: []customType{1}, | ||
}, | ||
{ | ||
in: "1,2", | ||
err: false, | ||
out: []customType{1, 2}, | ||
}, | ||
{ | ||
in: "1,", | ||
err: true, | ||
out: []customType{}, | ||
}, | ||
{ | ||
in: ",1", | ||
err: true, | ||
out: []customType{}, | ||
}, | ||
} { | ||
t.Run(tc.in, func(t *testing.T) { | ||
var v CSV[customType] | ||
|
||
err := v.Set(tc.in) | ||
if tc.err { | ||
require.NotNil(t, err) | ||
} else { | ||
require.Nil(t, err) | ||
require.Equal(t, tc.out, v.Get()) | ||
} | ||
|
||
}) | ||
} | ||
|
||
} |