3 ms·
Please don't slavishly copy Java/JS idioms. Go is not a language with a JIT and does not have a GC designed to handle lots of garbage; it assumes you won't allo
by morelisp 5y ago
Please don't slavishly copy Java/JS idioms. Go is not a language with a JIT and does not have a GC designed to handle lots of garbage; it assumes you won't allocate unless you need to.
For example instead of
func Drop[T any](array []T, n int) []T {
if n > len(array) {
n = len(array)
}
return array[n:]
}
Consider
func Drop[T any](array []T, n int) []T {
if n < len(array) {
return array[n:]
}
return nil
}
Which can collect the underlying array sooner.
Instead of your Chunk implementation (which, I want to offer, is better than most), consider an iterative zero-alloc version:
func Chunk[T any](array []T, n int) (chunk, remaining []T) {
return Take(array, n), Drop(array, n)
}
(And not performance-related, but instead of indices in `FindIndex` etc., consider returning pointers; they can then generalize more easily to non-slice sequences, or anonymous slices.)
(And please don't call slices arrays.)
- sudhirj 5y agoThanks, Will keep this in mind when I do the perf edits and benchmarks. And yeah, didn’t want to confuse slices arrays, but it’s weirder to call them slices.