-
Notifications
You must be signed in to change notification settings - Fork 105
D optimization techniques
By default, D initializes elements of allocated array. In a lot of cases, this is not needed. For instance, if the chunk of memory is allocated just to be filled from some Stream
. In order to avoid this extra overhead, use std.array.uninitializedArray
function.
The same goes for variables. In this case, you can just use = void
in variable declaration.
By default, all methods are virtual. You can mark them as final
to avoid extra indirection. Also, the whole class can be marked as final
.
Structs are more lightweight. Of course you lose inheritance and interfaces in this case, but there're mixins and compile-time polymorphism, therefore flexible design is still possible.
If a class instance is needed only in the current scope, it can be allocated on stack to avoid new
overhead. The preferred way to do it is to use std.typecons.scoped
function.
Unfortunately, write/writef/etc. are slow. Use good old printf instead. To output not zero-terminated D string, use "%.*s" format string:
printf("%.*s", str.length, str.ptr);
Useful for making library with C-like interface, when you need to manually allocate/deallocate memory. Allows to eliminate unnecessary GC.addRange/GC.removeRange calls. Avoiding them manually is dangerous due to possible future changes which might break your code, while static if
+ hasIndirections
makes your code safe.