Skip to content
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

Closed
vicuna opened this issue Sep 16, 2013 · 7 comments

Comments

Projects
None yet
2 participants
@vicuna
Copy link

commented Sep 16, 2013

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.

@vicuna

This comment has been minimized.

Copy link
Author

commented Sep 16, 2013

Comment author: @mmottl

Correction: I meant to say "double array tag", not "double tag".

@vicuna

This comment has been minimized.

Copy link
Author

commented Sep 17, 2013

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 y = 16

let () =
for i = 1 to x do
let a = Array.create y 0. in
for j = 0 to y - 1 do a.(j) <- float j done
done

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.)

@vicuna

This comment has been minimized.

Copy link
Author

commented Sep 17, 2013

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.

@vicuna

This comment has been minimized.

Copy link
Author

commented Sep 17, 2013

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).

@vicuna

This comment has been minimized.

Copy link
Author

commented Sep 17, 2013

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!

@vicuna

This comment has been minimized.

Copy link
Author

commented Nov 29, 2013

Comment author: @mmottl

I've just seen that the new C-function "caml_make_float_vec" contains the code:

if (wsize == 0)
return Atom(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).

@vicuna

This comment has been minimized.

Copy link
Author

commented Dec 4, 2013

Comment author: @alainfrisch

Thanks. I've followed your suggestion of inlining caml_alloc in caml_make_float_vect (commit 14338 on trunk).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.