Domaining using classes.
Michael van der Gulik
squeakml at gulik.co.nz
Sat Aug 12 01:41:17 UTC 2006
Klaus D. Witzel wrote:
> Hi Michael,
>
> on Tue, 08 Aug 2006 11:19:37 +0200, you wrote:
>
> Klaus D. Witzel wrote:
> That's the easy part: clones of classes share their method dictionary
> (in form of a pointer, not as a copy). Only the class pointer of per
> domain created instances is changed, nothing more and nothing else. A
> very good ROI, that is :)
....wow. That is thinking outside the square. I had never considered
stripping a thin layer off Class/Metaclass like that.
So, a concrete example is needed. Say I have a class "Person". Then,
normally:
alice := Person new.
bob := Person new.
In a domained image, say I have two domains, alicesDomain ("ad") and
bobsDomain ("bd"). Now:
adPerson = Person class copy new. " adPerson is a Class "
"<<Do class initialisation and stuff here, copying stuff from Person>>"
adPerson methodDict: Person methodDict.
adPerson domain: alicesDomain. " Sets an instance variable in the Class"
bdPerson = Person class copy new.
"<<Do class initialisation here>>"
bdPerson methodDict: Person methodDict.
bdPerson domain: bobsDomain.
Now we can do:
alice := adPerson new.
bob := bdPerson new.
In other words, each domain has its own set of classes, but each class
shares its instance variables (methodDict, instanceVariables,
superclass, format etc) with other classes of the same... er... class.
I could probably cludge up some Namespacing or other magic to get
"Person" to resolve to the domain's local version of the Person class,
so you don't need to use "adPerson" and "bdPerson" but just "Person".
So now, finding the domain of any given object would be easy:
Object>>domain
^ self class domain.
Trivial!
The code below hasn't been properly thought out wrt security etc, but
looks good in theory :-). I'll probably break a few images trying to get
it working as well.
Object>>new
self domain waitUntilCanMakeMoreObjects.
^ self basicNew initialize.
Domain>>waitUntilCanMakeMoreObjects
objectCounter := objectCounter + 1.
(objectCounter > maxObjects) ifTrue:
[ tooManyObjectsSemaphore wait ].
Object>>finalize
" This may or may not work... I haven't used >>finalize before"
self domain freeOneObject.
Domain>>freeOneObject
objectCounter := objectCounter - 1.
( do bug-free magic ) ifTrue: [ tooManyObjectsSemaphore signal ].
And ditto for Processes :-).
Advantages:
- should hopefully be more efficient than adding an instance variable
("domain") to Object.
- Can also do Class>>allInstances to count up object memory usage in a
domain.
- Can also do this with special objects like SmallInteger, Array,
CompiledMethod, ...
- Also has nice side-effects, e.g. pools, class variables, class-side
instance variables, Environments/Namespaces/SystemDictionaries can be
domain specific.
Disadvantages:
- umm... well its a bit hard to implement, but no less so than any other
ways of doing this.
So who takes the credit for this? Alexandre Bergel and Klaus Witzel?
Michael.
More information about the Squeak-dev
mailing list
|