[Nitro] Serious discussion on Annotaitons - Part II

George Moschovitis george.moschovitis at gmail.com
Sun Nov 12 06:18:54 EST 2006


Hello Tom,

I had a look at your new annotation implementation. The main
thing that concerns me is how an annotation lookup relates to
a plain hash lookup.

In Nitro and Og we are mainly reading the
annotation (the writes are very infrequent, typically on
application startup). I am worried that the search in heritage
on every read introduces a time penalty. Perhaps you should copy
annotations from the heritage in the current class to avoid the
'deep' search the next time, something like a cache. You could
set a dirty flag on annotation write and purge this annotation
'cache'.

I guess we should profile the current annotation lookup against
a plain hash lookup. Perhaps the speed penalty is not much after
all.

About the global variable. I don't mind having global variables
but why would we need to scan all annotations?

BTW, please notice that the code in 'ann' and 'ann!' is essentially
the same, perhaps you could refactor this?

In any case, thanks for your hard work. I really hope we can have
the annotation issue resolved the coming week, so I can port Nitro
to the new implementation.


regards,
George


On 11/7/06, George Moschovitis <george.moschovitis at gmail.com> wrote:
> Thanks!
>
> I will have a look at this imediately after the 0.40.0 release (will
> happen this week)
>
> -g.
>
> On 11/6/06, transfire at gmail.com <transfire at gmail.com> wrote:
> >
> > George Moschovitis wrote:
> > > >    harp:~ > cat a.rb
> > > > ...
> > > > :baz}
> > > >
> > > > Now this implementatin leaves out some of the needed shortcuts (like
> > > > implied class annotation) and it also leaves out the BIG issue of
> > > > inheritance, but those can of course can be worked in. The main thing
> > > > here is the simplicty of the interface. I realize it's a big change but
> > > > I think Ara has a point. The simplicy, uniformity and consistancy has
> > > > it's own merits over the elegance of the magic dot notation.
> > >
> > > Well I like this  simplicity as well. Could you introduce the
> > > inheritance feature?
> > > And what is implied class annotation? I would like to replace nitro's
> > > annotation with this simpler version. If you remember, I was always
> > > worried about the performance (and complexity) issues of the current
> > > implementation.
> >
> > Your wish is my command. Here it is. The just about the most
> > straight-foward and concise implementation of Annotations possible. I
> > have only two outstanding issues with it. 1) Whether to use the global
> > variable or not (see remarks for the difference); and 2) Whether
> > #heritage should remain it's own method or be integrated into #ann
> > itself, or perhaps just rename it? FYI heritage returns the complete
> > inheritance chain of annotations.
> >
> > I'm sure other improvments are possible. Please let me know it you have
> > any suggestions. Thanks.
> >
> >
> > # --- ann.rb
> >
> > require 'facets/core/hash/to_h'
> > require 'facets/core/hash/symbolize_keys'
> > require 'facets/core/hash/op_add'
> >
> > # By using a global veriable rather the definining a class instance
> > variable
> > # for each class/module, it is possible to quicky scan all annotations
> > for the
> > # entire system. To do the same without this would require scanning
> > through
> > # the ObjectSpace. (Still which is better?)
> >
> > #$annotations = Hash.new { |h,k| h[k] = {} }
> >
> > class Module
> >
> >   def annotations
> >     #$annotations[self]  # global?
> >     @annotations ||= {}
> >   end
> >
> >   def heritage(ref)
> >     ref = ref.to_sym
> >     ancestors.inject({}) { |memo, ancestor|
> >       ancestor.annotations[ref] ||= {}
> >       ancestor.annotations[ref] + memo
> >     }
> >   end
> >
> >   # Set or read annotations.
> >
> >   def ann( ref, keys_or_class=nil, keys=nil )
> >     return heritage(ref) unless keys_or_class or keys
> >
> >     if Class === keys_or_class
> >       keys ||= {}
> >       keys[:class] = keys_or_class
> >     else
> >       keys = keys_or_class
> >     end
> >
> >     if Hash === keys
> >       ref = ref.to_sym
> >       annotations[ref] ||= {}
> >       annotations[ref].update(keys.symbolize_keys)
> >     else
> >       key = keys.to_sym
> >       heritage(ref)[key]
> >     end
> >   end
> >
> >   # To change an annotation's value in place for a given class or
> > module
> >   # it first must be duplicated, otherwise the change may effect
> > annotations
> >   # in the class or module's ancestors.
> >
> >   def ann!( ref, keys_or_class=nil, keys=nil )
> >     return heritage(ref) unless keys_or_class or keys
> >
> >     if Class === keys_or_class
> >       keys ||= {}
> >       keys[:class] = keys_or_class
> >     else
> >       keys = keys_or_class
> >     end
> >
> >     if Hash === keys
> >       ref = ref.to_sym
> >       annotations[ref] ||= {}
> >       annotations[ref].update(keys.symbolize_keys)
> >     else
> >       key = keys.to_sym
> >       annotations[ref][key] = heritage(ref)[key].dup
> >     end
> >   end
> >
> > end
> >
> > _______________________________________________
> > Nitro-general mailing list
> > Nitro-general at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/nitro-general
> >
>
>
> --
> http://gmosx.com/blog
> http://nitroproject.org
>


-- 
http://gmosx.com/blog
http://nitroproject.org


More information about the Nitro-general mailing list