[squeak-dev] Instance variable access in superclasses.

Igor Stasenko siguctua at gmail.com
Tue Nov 25 22:13:14 UTC 2008


2008/11/25 Michael van der Gulik <mikevdg at gmail.com>:
>
>
> On Tue, Nov 25, 2008 at 11:40 PM, Igor Stasenko <siguctua at gmail.com> wrote:
>>
>> 2008/11/25 Michael van der Gulik <mikevdg at gmail.com>:
>> > Hi all.
>> >
>> > What would people's reaction be if a class was prevented from being able
>> > to
>> > directly access its superclass's instance variables? A subclass should
>> > use
>> > accessor methods to access a superclass's instance variables.
>> >
>> > Is there any particular reason subclasses get access to superclass
>> > instance
>> > variables? I think it breaks encapsulation.
>> >
>> > If this was implemented, it might be possible to avoid needing to
>> > recompile
>> > every subclass when you modify the instance variables of a class.
>> >
>> No, you can't avoid recompiling.
>> Suppose base class having vars:
>> 0 - a
>> 1 - b
>>
>> and subclass
>>
>> 2 - d
>> 3 - c
>>
>> now, if you add or remove vars in base class, indexes of 'd' and 'c'
>> variables will be shifted correspondingly. And therefore it would
>> require to recompile all methods where you using 'd' and 'c' ivars. It
>> also may require recompiling methods in base class, when you inserting
>> a var before a or b.. or removing var 'a'. - for same reason - indexes
>> will be shifted.
>
> I was thinking, briefly, about modifying the VM and object memory format so
> that instance variables are always indexed beginning from 0 for every
> subclass. Somehow.
>

Well, if you put ivars into dictionary of some sort, then you don't
need to recompile anything. But speed tradeoff will be huge :)
One possible format change could be to keep a class ivars starting
index in its instanceFormat field.
Then, when you changing the number of ivars, you simply go through
subclasses and update their instanceFormat w/o recompiling  methods.
You still need to recompile methods but only of class which changes
its ivars:

ClassA (ivarsIndex = 0)
ivars: a, b, c

ClassB (ivarsIndex = 3)
ivars: d,e,f

Now if you change ClassA ivars to = 'b,c'
you setting ClassB ivarsIndex = 2
and recompiling ClassA methods.

An ivar accessing instruction now requires 2 arguments: index and class.

ivarAddr = oop at: (class ivarsIndex) + index

not much overhead, but you have to keep a class oop in literal frame of method :

ClassB >> methodFoo
   ^ a + e

will require to keep 2 class oops (ClassA and ClassB) , because you
accessing ivars from both of them..

IMO, it is better to recompile things all the way down, rather than
bother with such complexity which gives no real benefits except
compiling time.

> It isn't necessarily a good idea. I haven't thought through the details yet.
>
> Gulik.
>
> --
> http://people.squeakfoundation.org/person/mikevdg
> http://gulik.pbwiki.com/
>


-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Squeak-dev mailing list