Skip to content

Commit

Permalink
ftyp: exit asap to prevent mem allocs (#517)
Browse files Browse the repository at this point in the history
go test -bench=SliceRand -benchmem
before:
BenchmarkSliceRand-8   	  688160	      1690 ns/op	     728 B/op	      75 allocs/op
after:
BenchmarkSliceRand-8   	 1232066	      1173 ns/op	     160 B/op	       4 allocs/op
  • Loading branch information
gabriel-vasile authored May 5, 2024
1 parent 889166d commit fd7639e
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion internal/magic/magic.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,11 @@ func ftyp(sigs ...[]byte) Detector {
if len(raw) < 12 {
return false
}
if !bytes.Equal(raw[4:8], []byte("ftyp")) {
return false
}
for _, s := range sigs {
if bytes.Equal(raw[4:12], append([]byte("ftyp"), s...)) {
if bytes.Equal(raw[8:12], s) {
return true
}
}
Expand Down

0 comments on commit fd7639e

Please sign in to comment.