string sharing (possible bug?)
Adam P. Jenkins
ajenkins at javanet.com
Wed Dec 9 20:04:27 UTC 1998
> The problem is independent of duplication. Say you have a method that says
> | b |
> b := 'abc'.
> Transcript space; show: b.
> b at: 2 put: $d
> What prints the second time you send #foo? Because the literal (copied or
> not) is stored with the compiled method and not the activation, the second
> time aroud b is 'adc' even though it's right after the assignment
> of 'abc'.
Yuck! I assume from what you say below that this problem wouldn't occur
with VisualWorks. Is that true?
> For VisualWorks, we got rid of the sharing but not the mutability.
This seems like the best solution to me. I can't imagine that sharing is
useful very often. How often are there really identical string or array
literals in the same method? And in general a literal is going to be small,
since you have to type it in, so the memory savings by sharing are also
small. The simplest method is to just create a new object every time a
literal is encountered. If a programmer wants them to be shared, they can
a := 'hello'.
b := a.
c := a.
More information about the Squeak-dev