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

implement subslice sharing annotation #3

Open
kortschak opened this issue Mar 25, 2015 · 3 comments
Open

implement subslice sharing annotation #3

kortschak opened this issue Mar 25, 2015 · 3 comments
Labels

Comments

@kortschak
Copy link
Owner

This is an extension of #2. It is not clear how to address this though.

For example, if we have

a := []int{1, 2, 4, 8}
b := a[1:3]

How should this be rendered?

@dmitshur
Copy link

Yeah, this is not trivial.

FWIW, []int{1, 2, 4, 8}[1:3] is valid Go code and that could be one way.

http://play.golang.org/p/LoPa9zv9w2

@kortschak
Copy link
Owner Author

That is fine, but does not correctly describe the situation I'm worried about. Note that utter only ever takes a single value, so we only need to be consistent within that value.

Say we have:

a := [][]int{{1, 2, 4, 8}}
a = append(a, a[0][1:3])

We would like to have annotation that shows that a change to a[1][0] results in a change to a[0][1]. This is the motivation behind #2 and this issue is just an extension of that.

A possible approach is to use address annotation like so:

[][]int{
    /*0xdeadbeef*/ {1, 2, 4, 8},
    {2, 4} /*[]0xdeadbeef[1:3]*/,
}

The current pre-dump walk I do can get this information with some extension.

@kortschak
Copy link
Owner Author

The issue is further complicated.

Take the following:

b := []int{1, 2, 4, 8}
a := [][]int{b[1:2], b[:3], b[1:]}

The previous approach to rendering a does not work because a[1] indexes and a[2] indexes do not completely overlap in b. In this case a solution borrowing from Dimitri's approach may be OK:

[][]int{
    {2, 4} /*[]0xdeadbeef[1:3]*/,
    []int /*0xdeadbeef*/ {1, 2, 4, 8}[0:3],
    {2, 4, 8} /*[]0xdeadbeef[1:4]*/,
}

The necessary work to prepare for this is building a map of overlapping slices (the map would contain one memory interval for each set of overlapping slices by the end of the walk) for each elem type. The address of the first left-most slice (lowest index in the backing array) needs to be retained - this becomes the reference slice.

During rendering, the reference slice is rendered as the entire slice, marking its address and slicing to the appropriate length. All other slices are rendered as marked subslices of the backing slice.

When the backing data is an array or array pointer, then the prefix to the address in marked subslices is [n] where n is the array len.

An additional complication is how/if to render caps of the reference slice (or at all) - it can not be capped at an index less than the longest referring slice. At this stage, I will ignore that, though consider just using a comment unless there is another uncapped slice to use as a reference slice. Others subslices are easy since the is no problem with actually showing the cap with Go syntax.

If this is implemented, it is disruptive enough to need a config option and not be on by default.

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

No branches or pull requests

2 participants