Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006180OCamlOCaml backend (code generation)public2013-09-16 18:502013-12-04 10:38
Reportermottl 
Assigned Tofrisch 
PrioritylowSeverityfeatureReproducibilityalways
StatusresolvedResolutionfixed 
PlatformOSOS Version
Product Version4.01.0 
Target VersionFixed in Version4.02.0+dev 
Summary0006180: Efficient creation of float arrays
DescriptionWould 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.
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
(0010360)
mottl (reporter)
2013-09-16 18:51

Correction: I meant to say "double array tag", not "double tag".
(0010364)
frisch (developer)
2013-09-17 14:02

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.)
(0010365)
gasche (developer)
2013-09-17 15:11

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.
(0010366)
frisch (developer)
2013-09-17 15:34

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).
(0010368)
mottl (reporter)
2013-09-17 16:02

@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!
(0010679)
mottl (reporter)
2013-11-29 23:51

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).
(0010690)
frisch (developer)
2013-12-04 10:38

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

- Issue History
Date Modified Username Field Change
2013-09-16 18:50 mottl New Issue
2013-09-16 18:51 mottl Note Added: 0010360
2013-09-17 13:39 frisch Assigned To => frisch
2013-09-17 13:39 frisch Status new => assigned
2013-09-17 14:02 frisch Note Added: 0010364
2013-09-17 14:02 frisch Status assigned => resolved
2013-09-17 14:02 frisch Fixed in Version => 4.02.0+dev
2013-09-17 14:02 frisch Resolution open => fixed
2013-09-17 15:11 gasche Note Added: 0010365
2013-09-17 15:34 frisch Note Added: 0010366
2013-09-17 16:02 mottl Note Added: 0010368
2013-11-29 23:51 mottl Note Added: 0010679
2013-12-04 10:38 frisch Note Added: 0010690


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker