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

Hashtbl.resize is not tail recursive #4747

Closed
vicuna opened this issue Mar 16, 2009 · 14 comments
Closed

Hashtbl.resize is not tail recursive #4747

vicuna opened this issue Mar 16, 2009 · 14 comments

Comments

@vicuna
Copy link

vicuna commented Mar 16, 2009

Original bug ID: 4747
Reporter: @mshinwell
Assigned to: @alainfrisch
Status: closed (set by @xavierleroy on 2017-09-24T15:32:01Z)
Resolution: fixed
Priority: high
Severity: minor
Version: 3.11.0
Target version: 4.03.1+dev
Fixed in version: 4.03.1+dev
Category: standard library
Tags: patch
Monitored by: lyongu @jmeber yminsky pzimmer @mmottl

Bug description

The function in Hashtbl.resize that copies the linked list attached to one bucket isn't tail recursive, which means that if you have the (admittedly undesirable) situation of a very long chain of buckets it can cause a stack overflow.

Degenerate example:

let ht = Hashtbl.create 100
let () = for x = 1 to 2500000 do Hashtbl.add ht 0 () done

File attachments

@vicuna
Copy link
Author

vicuna commented Jun 20, 2012

Comment author: varobert

I encountered this bug while using Coq extraction feature, because of a misuse of Hashtbl.add instead of Hashtbl.replace that has been reported upstream.

I attached a proposed patch.

Beware that other functions build results in a non-tail-rec fashion over buckets:
remove_bucket
find_in_bucket
replace_bucket

Therefore, this patch may only delay stack overflows some more, as long as it can shrink buckets enough. A more thorough patch would probably incur both performance and efficiency loss.

This patch will slow down the resizing process too, since it builds the reversed bucket first.

@vicuna
Copy link
Author

vicuna commented Jul 12, 2012

Comment author: @gasche

Valentin Robert has given me a full patch. However, due to the non-critical status¹ and potential for performance regressions, there is no need to do that before the release. I'm changing the "target version" to reflect this.

¹: If the bucket becomes so long that the overflows becomes a problem, it means that there is a problem with the way hashtables are used. If it comes from hashing conflicts, you will get terrible performances due to the linked-list bucket implementation (linear cost for finding a given element), and a difference implementation must be preferred. If it comes from an over-use of the multiple binding semantics ("adding" a lot of different values on the same key), you should probably switch to a different data structure. One might argue that having a Stack Overflow is a good signal for a design defect that could otherwise go unnoticed. I'm still in favor of fixing this bug, but it can wait after the release.

@vicuna
Copy link
Author

vicuna commented Jun 12, 2013

Comment author: @alainfrisch

Gabriel: have you had a chance to review the patch?

@vicuna
Copy link
Author

vicuna commented Jun 13, 2013

Comment author: @gasche

I just updated Valentin's patch; sorry, I should have done that much earlier.

I reviewed this patch and tested it for correctness. That said, we haven't done any serious measurement of the performance impact (which may be non-neglectible on small bucket lists, as the new code does one (additional) reversal when the traversal ends). I'll try to do that later this week, but in the end it will always be a judgment call on whether we favor speed on small buckets, or correctness on very large ones.

N.B.: the patch only touches the non-functorial part of Hashtbl; of course in a final commit the changes should be duplicated in the functorial part as well.

@vicuna
Copy link
Author

vicuna commented Jun 13, 2013

Comment author: @alainfrisch

What about starting with the "direct-style" (non-tail rec) version, decrementing a counter at each step (starting at, say, 50), and when the counter reaches 0, switching to the tail-rec (but slower) version? (Anyway, resizing is not so frequent, so it might not be worth the tiny extra complexity.)

@vicuna
Copy link
Author

vicuna commented Jun 13, 2013

Comment author: @gasche

I did that for List.fold_right in Batteries and the results were okay, but the implementation complexity is a bit annoying. Will try at benchmark time.

@vicuna
Copy link
Author

vicuna commented Jun 22, 2013

Comment author: @gasche

So I did some micro-benchmarks, that were not very conclusive. There is a performance hit (that seems to disappear if you crank up the GC params (specifically "s=10M")), but hybrid methods as suggested above were not successful to bridge the gap for all changed functions.

After a bit of trial and error, I have uploaded a version of the patch that seems to bridge the gap -- at least on my x86_64 machine. I will try to get results on other machines, and re-check it for correctness (it's in a rough state now).

@vicuna
Copy link
Author

vicuna commented Dec 3, 2015

Comment author: @alainfrisch

Gabriel: any news on this one?

Would it make sense to use imperative lists here (perhaps using inline records to allow mutating the tail)?

@vicuna
Copy link
Author

vicuna commented Dec 3, 2015

Comment author: @gasche

I haven't worked on this since then. It's not a very motivating topic to work on, as we are contemplating regressions on the common case for a more robust behavior in a case that rarely happens... My conclusion at the time, I think, was that it is very hard to reliably benchmark those hybrid strategies, and that the added complexity is not pleasing.

Maybe we could wait on that one until we agree on TRMC?

(Another option is that flambda would change the performance game between the tail-rec and the non-tail-rec version. Maybe I'll try to re-run my measures after the flambda merge. But this is not a priority for me.)

@vicuna
Copy link
Author

vicuna commented Dec 3, 2015

Comment author: @alainfrisch

Note that we already have a custom type for lists (bucketlist) and with inline records, it's easy to make the tail mutable.

TRMC would allow to get more tail recursion with code that manipulate lists in a functional way (e.g. copying cells to remove one of them). But here it seems we could directly mutate the "original" list, which would mean fewer allocations. This might compensate the extra costs related to mutation and improve also the general case.

@vicuna
Copy link
Author

vicuna commented Dec 3, 2015

Comment author: @gasche

Indeed, using inline records! I hadn't thought of that.

Would you wish to have a go at it? You've been super-productive fixing stuff lately. If you don't want to work on it, I'm thinking of asking Valentin Robert.

@vicuna
Copy link
Author

vicuna commented Dec 3, 2015

Comment author: @alainfrisch

#328

I've attached to this ticket some micro benchmark test. See above for the results.

@vicuna
Copy link
Author

vicuna commented Dec 9, 2015

Comment author: @alainfrisch

Xavier argues for pushing this after 4.03. Mark: do you agree?

@vicuna
Copy link
Author

vicuna commented Dec 9, 2015

Comment author: @mshinwell

Seems fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants