New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Efficient creation of float arrays #6180
Comments
Comment author: @mmottl Correction: I meant to say "double array tag", not "double tag". |
Comment author: @alainfrisch Wish granted, commit 14156 on trunk. The new function is Array.make_float (don't hesitate to suggest a better name!). Speed is almost doubled for code such as: let x = 10000000 let () = Of course, this will be diluted for code performing something useful. (It might be interesting to see where we would be performance-wise with a version of Array.make_float which initializes the array to 0.) |
Comment author: @gasche Aren't there security concerns with letting users access uninitialized memory? If the initialized-to-0 version isn't much slower, I would say it is a more reasonable choice. |
Comment author: @alainfrisch There is already String.create. An argument for initializing to 0 is rather one of predictability to me (and it is also a useful behavior in many cases). |
Comment author: @mmottl @gasche: I don't think that float arrays are in any way risky in that respect. That said, I do usually initialize float arrays during testing, but with NANs, not with zeros. The reason is that an incorrect use of the array (reading uninitialized parts) would then likely propagate the NANs everywhere, making the bug more obvious. Thanks for the patch, Alain! |
Comment author: @mmottl I've just seen that the new C-function "caml_make_float_vec" contains the code: if (wsize == 0) This seems redundant, because the subsequent call to "caml_alloc" will perform the exact same test. I think it can be safely dropped, slightly increasing performance for most use cases. Another optimization would be to just manually inline the contents of "caml_alloc" and specialize its body for float tags (i.e. dropping the redundant tests for field initialization). |
Comment author: @alainfrisch Thanks. I've followed your suggestion of inlining caml_alloc in caml_make_float_vect (commit 14338 on trunk). |
Original bug ID: 6180
Reporter: @mmottl
Assigned to: @alainfrisch
Status: closed (set by @xavierleroy on 2015-12-11T18:25:23Z)
Resolution: fixed
Priority: low
Severity: feature
Version: 4.01.0
Fixed in version: 4.02.0+dev
Category: back end (clambda to assembly)
Monitored by: @gasche @hcarty @mmottl
Bug description
Would it be possible to provide a primitive that can efficiently allocate a block of a given length with a double tag without initializing its contents (which won't be scanned anyway)? This could then be exposed e.g. in the Array-module as follows:
val alloc_floats : int -> float array = "%alloc_floats"
Allocating float arrays this way could speed up some numeric code. Users who need to optimize allocation performance of pure, mutable float records would also benefit.
The text was updated successfully, but these errors were encountered: