[stringtemplate-interest] v4 renderers
Udo Borkowski
ub at abego-software.de
Thu Dec 23 14:04:28 PST 2010
On 23.12.2010, at 18:58, Terence Parr wrote:
>
> On Dec 22, 2010, at 2:10 PM, Udo Borkowski wrote:
>
>>> Look ok? Same would work for registerModelAdaptor(type, adap), right? Upon <a.p> and a.getClass() is instanceof or implements type, then I'd ask adap how to get p from 'a', right? Later I can cache a.getClass -> adap mapping.
>>
>> Rather than caching a.getClass -> adap I would ask adap for an object I could use for caching (and accessing a property value). This is because it may still be expensive for the adapter to calculate the property value, but easier if some "calculation" is already done (and only done once).
>
> but before i can ask an adaptor for it's object->Method cache, i have to find the adaptor, which is O(n); i should cache that right?
Because the order of registration is important in you approach you need O(n) to find the adapter. Then you should definitely cache this.
In my implementation I just keep a map from Class to "my PropertyAccessor" (similar to your adapters). I still get something like the "instanceof" functionality using this approach: if I don't find a PropertyAccessor for class C in the map I explicitly check for the super classes and interfaces of C (recursively). If I find one this way I store the same PropertyAccessor also for C. If multiple "adapters" are installed this approach is typically faster than checking every adapter against the class. E.g. for the "Double" class the "Number" adapter would be found in one step.
However with this approach I cannot guarantee anything if multiple adapters would fit for class C.
Udo
More information about the stringtemplate-interest
mailing list