www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Pointers vs functional or array semantics

reply data pulverizer <data.pulverizer gmail.com> writes:
I have noticed that some numerical packages written in D use 
pointer semantics heavily (not referring to packages that link to 
C libraries). I am in the process of writing code for a numerical 
computing library and would like to know whether there times when 
addressing an array using pointers conveys performance benefits 
over using D's array or functional semantics?
Feb 25 2017
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
data pulverizer wrote:

 I have noticed that some numerical packages written in D use pointer 
 semantics heavily (not referring to packages that link to C 
 libraries). I am in the process of writing code for a numerical 
 computing library and would like to know whether there times when 
 addressing an array using pointers conveys performance benefits over 
 using D's array or functional semantics?
using `arr.ptr[n]` instead of `arr[n]` bypasses bounds checking. this may be diserable in tight loops (while disabling bounds checking globally is not). but note that `foreach (int n; arr)` doesn't do bounds checking in loop too (afair), so you prolly better use `foreach` instead of pointers. this way your code will be fast, but still safe.
Feb 25 2017
parent data pulverizer <data.pulverizer gmail.com> writes:
On Saturday, 25 February 2017 at 11:15:53 UTC, ketmar wrote:
 data pulverizer wrote:

 I have noticed that some numerical packages written in D use 
 pointer semantics heavily (not referring to packages that link 
 to C libraries). I am in the process of writing code for a 
 numerical computing library and would like to know whether 
 there times when addressing an array using pointers conveys 
 performance benefits over using D's array or functional 
 semantics?
using `arr.ptr[n]` instead of `arr[n]` bypasses bounds checking. this may be diserable in tight loops (while disabling bounds checking globally is not). but note that `foreach (int n; arr)` doesn't do bounds checking in loop too (afair), so you prolly better use `foreach` instead of pointers. this way your code will be fast, but still safe.
Thanks ketmar and thanks in advance to anyone else that comments.
Feb 25 2017
prev sibling parent Ilya Yaroshenko <ilyayaroshenko gmail.com> writes:
On Saturday, 25 February 2017 at 11:06:28 UTC, data pulverizer 
wrote:
 I have noticed that some numerical packages written in D use 
 pointer semantics heavily (not referring to packages that link 
 to C libraries). I am in the process of writing code for a 
 numerical computing library and would like to know whether 
 there times when addressing an array using pointers conveys 
 performance benefits over using D's array or functional 
 semantics?
Pointers can behave like iterators, while to have an iterator on top of array you need to store array and index. This is slower and may disable vectorisation. Iterators semantic is required to build multidimensional random access ranges (ndslice). ndslice uses iterators heavily in its internals. Iterators semantic helped to create many multidimensional Phobos analogs in few dozens LOC, while Phobos has few hundreds LOC for the same functionality. Phobos functional semantics disable vectorisation and other optimisations. mir.ndslice.topology [1], mir.ndslice.algorithm[1] and mir.functional[1] in combination with other Mir modules and packages can be used instead of Phobos functional utilities from std.algorithm, std.range, std.functional. You may want to use the new ndslice [1]. Slice!(Contiguous, [1], T*) can replace T[]. [1] https://github.com/libmir/mir-algorithm
Feb 25 2017