-
Notifications
You must be signed in to change notification settings - Fork 52
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
Bytewise crc32 #41
Bytewise crc32 #41
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice PR!
I'd rather see it organized differently, I've made comments.
crc32.go
Outdated
*/ | ||
// https://github.com/videolan/vlc/blob/master/modules/mux/mpeg/ps.c | ||
|
||
var tableCRC32 = [256]uint32{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you create a command to generate this table in a separate go file?
- Add an
internal/cmd/crc32_table
folder - Add a
main.go
in it that would create acrc32_table.go
with only thetableCRC32
variable in it. - Also make sure to add
// Code generated by astits. DO NOT EDIT.
a the top of this file before the package name
That way we have a command to generate the file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All done! This is my first attempt with code generation, so it may be kind of ugly
crc32.go
Outdated
|
||
// computeCRC32 computes a CRC32 | ||
// https://stackoverflow.com/questions/35034042/how-to-calculate-crc32-in-psi-si-packet | ||
func computeCRC32old(bs []byte) uint32 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the old implementation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
crc32_test.go
Outdated
}, | ||
} | ||
|
||
func Benchmark_updateCRC32(b *testing.B) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to keep the benchmark here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
crc32_test.go
Outdated
|
||
func Test_updateCRC32(t *testing.T) { | ||
|
||
for _, test := range tests { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
crc32_test.go
Outdated
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name+" precomputed", func(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can remove the " precomputed" here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
…lation func and corresponding tests/benchmarks.
crc32.go
Outdated
@@ -4,22 +4,17 @@ const ( | |||
crc32Polynomial = uint32(0xffffffff) | |||
) | |||
|
|||
// computeCRC32 computes a CRC32 | |||
// https://stackoverflow.com/questions/35034042/how-to-calculate-crc32-in-psi-si-packet | |||
// this solution based on vlc implementation + static crc table (1kb additional memory on start, without reallocations) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you put this comment above updateCRC32
instead ?
And could you replace it with :
// Based on VLC implementation using a static CRC table (1kb additional memory on start, without
// reallocations): https://github.com/videolan/vlc/blob/master/modules/mux/mpeg/ps.c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
internal/cmd/crc32_table/main.go
Outdated
) | ||
|
||
const ( | ||
disclaimer = `// Code generated by astits. DO NOT EDIT` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you update the disclaimer to // Code generated by astits using internal/cmd/crc32_table. DO NOT EDIT
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
internal/cmd/crc32_table/main.go
Outdated
) | ||
|
||
func main() { | ||
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_SYNC, 0644) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use os.Create()
instead ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, this approach is much neater, thanks!
internal/cmd/crc32_table/main.go
Outdated
func main() { | ||
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC|os.O_SYNC, 0644) | ||
if err != nil { | ||
panic(err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use log.Fatal(err)
instead ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
internal/cmd/crc32_table/main.go
Outdated
doOrPanic(fmt.Fprintf(w, "\n%s", `}`)) | ||
} | ||
|
||
func doOrPanic(_ int, err error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rename this to write()
and instead of panic(err)
do log.Fatal(err)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On my opinion it is more general purpose function then just writer (and it's not even write, just crash if something go wrong). Maybe 'try()' will be more appropriate name? Anyway, i done as you ask
fyi I've created a thanks for the PR! ❤️ |
Good day!
This pull request introduce bytewise crc32 calculation instead of current bitwise. Code based on VLC implementation, but instead per-object table generation, it use static crc32 table (+1kb additional lifetime memory use but no recalculations and reallocations) .
Current:
Introduced in this patch:
Test's and benchmarks based on real TS headers. Old implementation renamed and kept in file for comparison sake, you can remove it safely.