Share the strings representing scopes (alternative approach)#9896
Share the strings representing scopes (alternative approach)#9896xavierleroy merged 2 commits intoocaml:trunkfrom
Conversation
|
If I understand correctly, this implementation has the downside that we recompute the whole output each time we grow the scope, which gets us in the base quadratic-complexity case of string concatenation: we share the outputs, but we compute them less efficiently than with the previous |
|
Again, I know little about those scope strings, but it seems that all intermediate strings will actually be needed at some point (i.e. if we build "X.Y.x", we will also need to build "X" and "X.Y"). At least this is what I observe empirically on examples I tried. |
Ah! That would be nice. @stedolan could you maybe confirm that this sounds likely? |
|
Some results, compiling typing/typecore.ml on trunk and the two PRs (allocated words returned by OCAMLRUNPARAM=v=1024):
So this actually allocates a bit less than #9748 (on this example), but it does not fully remove the duplication of strings (to be investigated). |
|
The lack of sharing in this PR is likely caused by the anonymous function case. |
|
The last commit shares the computation of the
|
|
Remaining lack of sharing is related to multiple values with the same name. Keeping the sharing there would require memoization/hash-consing, but this might not be worth the trouble. |
|
Adding hash-consing (through a weak table):
|
|
Mr. Caution speaking here: ephemerons and weak tables are not very stable yet, with known issues (#9424) and alternate APIs being considered, so I'd rather not use them in the compiler, at least in the bootstrap cycle. I know there is already one use in typing/btypes.ml, but I'm hoping it's just for error recovery. Besides, the gain from using a weak table appears tiny. |
|
Thanks @xavierleroy . Do you have a preference between this PR (minus the commit that introduces hash-consing) and #9748 (+ something to free the memoization table between compilation units?)? |
|
I have a preference for the overall approach here, but it contains larger API changes as well that I have not reviewed yet. |
gasche
left a comment
There was a problem hiding this comment.
I reviewed the API changes, they make the type of scopes opaque which is fine.
I think that the implementation is too complex and slightly ugly at places, Alain got a bit enthusiastic about beating a benchmark target and we don't need all the complexity. I would be in favor of simplifying a bit (accepting to lose small amounts of sharing in the process). I think the general approach is good and I would be in favor of merging eventually.
lambda/debuginfo.ml
Outdated
|
|
||
| module WeakString = Weak.Make(struct include String let hash = Hashtbl.hash end) | ||
|
|
||
| let memo_str = WeakString.merge (WeakString.create 128) |
There was a problem hiding this comment.
I would be in favor of avoiding weak pointers to simplify the runtime surface fo the code, especially given that the sharing gains reported are rather small.
There was a problem hiding this comment.
As discussed during today's meeting, I've reverted this hash-consing.
Note that if the concern is just the use of Weak tables, one could just use a regular table, and clear it after each compilation unit / toplevel phrase. But it was agreed that the tiny lack of sharing is not worth the trouble.
|
I think all comments have been addressed. |
|
The history is kind of a mess right now. We could squash in a single commit, but I think it would be nicer if you could keep the " Make Scoped_location.scopes abstract" commit, and then aggregate all other changes in a single commit. |
2d4d6d3 to
0d25f78
Compare
|
Done. I think parts of the second commit should be part of the first one, but honestly, I think it's a bit pointless to spent time on that. (I'd be in favor of squash-merging anyway.) |
|
Ah, indeed, the separation does not really work. Let's squash-merge then. This is good to merge when the CI is green, and we want to include it in 4.12 as it fixes a cmo-size regression that only appears there. |
|
CI is positive: the AppVeyor failure is unrelated (the wretched "weaklifetime" test). Cannot resist the "merge-me" label... |
|
Cherry-picked to 4.12 (8bc0461). |
An alternative to #9748 (see that PR for the discussion).