It's true, there's no realloc()
-style primitive for OCaml memory.
The efficiency of realloc()
isn't a given; it depends on the pattern of calls. Generally speaking it only saves time if there is free space after the reallocated object.
In OCaml this is very unlikely; in the younger GC generation no objects have free space following them; blocks are allocated in decreasing memory order. In the older generation, you can have the odd hole here and there. If compaction has been done recently, only the one old object at the end can have free space after it.
For these reasons, I suspect that realloc()
would have almost no benefit in the OCaml GC environment.
I would actually tend to doubt that realloc()
is the reason you're getting faster times for PHP than for OCaml. I don't think it's a real timesaver except in unusual circumstances. But maybe you have some measurements that support this theory.
Update
You might want to experiment with the OCaml Buffer
module, which will let you concatenate strings imperatively. A buffer is likely to be faster than using the ^
operator for many cases (but has the usual disadvantages of a mutable value).