sgd: many optimizations