Skip to content

Commit

Permalink
skip memory alloc on regex parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
sduchesneau committed Dec 2, 2024
1 parent a5f3eb8 commit 0020d33
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
8 changes: 5 additions & 3 deletions storage/execout/filename.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ func parseIndexFileName(filename string) (*FileInfo, error) {
}

func fileNameToRange(filename string, regex *regexp.Regexp) (*block.Range, error) {
res := regex.FindAllStringSubmatch(filename, 1)
regex.FindAllSubmatchIndex([]byte(filename), 1)

res := regex.FindAllSubmatchIndex([]byte(filename), 1)
if len(res) != 1 {
return nil, fmt.Errorf("invalid output cache filename, %q", filename)
}

start := uint64(mustAtoi(res[0][1]))
end := uint64(mustAtoi(res[0][2]))
start := uint64(mustAtoi(filename[res[0][2]:res[0][3]]))
end := uint64(mustAtoi(filename[res[0][4]:res[0][5]]))

return &block.Range{
StartBlock: start,
Expand Down
59 changes: 59 additions & 0 deletions storage/execout/filename_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package execout

import (
"reflect"
"regexp"
"testing"

"github.com/streamingfast/substreams/block"
)

func Test_fileNameToRange(t *testing.T) {
type args struct {
filename string
regex *regexp.Regexp
}
tests := []struct {
name string
args args
want *block.Range
wantErr bool
}{
{
name: "cache filename",
args: args{
filename: "0013368000-0013369000.output",
regex: cacheFilenameRegex,
},
want: &block.Range{
StartBlock: 13368000,
ExclusiveEndBlock: 13369000,
},
wantErr: false,
},
{
name: "index filename",
args: args{
filename: "0000122000-0000123000.index",
regex: indexFilenameRegex,
},
want: &block.Range{
StartBlock: 122000,
ExclusiveEndBlock: 123000,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := fileNameToRange(tt.args.filename, tt.args.regex)
if (err != nil) != tt.wantErr {
t.Errorf("fileNameToRange() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("fileNameToRange() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 0020d33

Please sign in to comment.