options.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package reedsolomon
  2. import (
  3. "runtime"
  4. "github.com/klauspost/cpuid"
  5. )
  6. // Option allows to override processing parameters.
  7. type Option func(*options)
  8. type options struct {
  9. maxGoroutines int
  10. minSplitSize int
  11. useAVX2, useSSSE3 bool
  12. }
  13. var defaultOptions = options{
  14. maxGoroutines: 50,
  15. minSplitSize: 512,
  16. }
  17. func init() {
  18. if runtime.GOMAXPROCS(0) <= 1 {
  19. defaultOptions.maxGoroutines = 1
  20. }
  21. // Detect CPU capabilities.
  22. defaultOptions.useSSSE3 = cpuid.CPU.SSSE3()
  23. defaultOptions.useAVX2 = cpuid.CPU.AVX2()
  24. }
  25. // WithMaxGoroutines is the maximum number of goroutines number for encoding & decoding.
  26. // Jobs will be split into this many parts, unless each goroutine would have to process
  27. // less than minSplitSize bytes (set with WithMinSplitSize).
  28. // For the best speed, keep this well above the GOMAXPROCS number for more fine grained
  29. // scheduling.
  30. // If n <= 0, it is ignored.
  31. func WithMaxGoroutines(n int) Option {
  32. return func(o *options) {
  33. if n > 0 {
  34. o.maxGoroutines = n
  35. }
  36. }
  37. }
  38. // MinSplitSize Is the minimum encoding size in bytes per goroutine.
  39. // See WithMaxGoroutines on how jobs are split.
  40. // If n <= 0, it is ignored.
  41. func WithMinSplitSize(n int) Option {
  42. return func(o *options) {
  43. if n > 0 {
  44. o.minSplitSize = n
  45. }
  46. }
  47. }
  48. func withSSE3(enabled bool) Option {
  49. return func(o *options) {
  50. o.useSSSE3 = enabled
  51. }
  52. }
  53. func withAVX2(enabled bool) Option {
  54. return func(o *options) {
  55. o.useAVX2 = enabled
  56. }
  57. }