You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Arnaud Le Hors <le...@us.ibm.com> on 2000/07/18 03:51:32 UTC
Managing/implementing several DOM implementations
Alright,
I sent a message a few days ago where I started talking about my
problems with managing/implementing several DOM implementations. Here is
more on
this.
There are many many different DOM implementations we could provide. I
don't think it's reasonable nor practical to provide them all. So, I'm
thinking of providing just a few, such as:
1) bare minimum: read-only, no support for fast random access
2) minimum + write
3) full (this is what we currently provide!)
As I said in my previous message the problem I have is to achieve this
without multiple inheritance which is not supported in Java).
Here is the problem:
For 1) I would basically have a class hierarchy where ReadOnlyNodeImpl
is the base class, from which other classes such as ReadOnlyElementImpl
and ReadOnlyAttrImpl inherit from.
Now, let's say I want to add write capabilities for 2). How do I do
that?
Ideally/naturally, *I* would do something like that:
Define a ReadWriteNodeImpl that inherits from ReadOnlyNodeImpl,
ReadWriteElementImpl that inherits both from ReadOnlyElementImpl and
ReadWriteNodeImpl, and ReadWriteAttrImpl that inherits both from
ReadOnlyAttrImpl and ReadWriteNodeImpl.
The only way to avoid the multiple inheritance diagrams that this uses
is to forget ReadWriteNodeImpl and have all of its behavior implemented
by each of the class ReadWriteElementImpl, ReadWriteAttrImpl, etc... But
this means duplicating the same code on every single subclass. Major
hassle! Unmanageable. Not really an option!
There is another way to do that: delegation. This would imply having
ReadOnlyNodeImpl provide for some extension mechanism which could be
used to extend its behavior. This model, however, has two major
drawbacks. The extension mechanism requires extra storage on the base
class (at least one pointer), and every method call requires at least
another method call. Both of these kills this option right off the bat,
since they defeat my original goal of getting smaller and faster DOM
implementations.
So what's left? The only option left that I know of is to implement the
full DOM, and extract from it parallel hierarchies each of which only
implements a subset of the full DOM. I can do that with some
preprocessing without too much trouble. I already do as an experiment!
I don't really like it because it makes the source code uglier, but
that's the best I've got so far. That's where I am right now.
Any ideas or comments welcome!
--
Arnaud Le Hors - IBM Cupertino, XML Technology Group
Re: Managing/implementing several DOM implementations
Posted by Arnaud Le Hors <le...@us.ibm.com>.
Paulo Gaspar wrote:
>
> There are a couple of twists there:
> 1. You only need to delegate half of it;
> 2. You do NOT need an extra pointer per node.
I don't see how.
> Detailing, let's consider some facts:
> a) Read functionality is used much more often than write
> functionality;
> b) Most of the time you DO NOT use writing without reading, but you
> often use reading without writing;
> c) Writing takes more code than reading (let's just mention
> validation, including validation against a DTD/Schema).
>
> A read-only parser would take much less memory without having to carry
> the write part. This opens the door for big savings on a non-validating
> read-only parser.
Just a minor clarification note: we're talking about the DOM here, not
the parser. Other than that I agree. Which is exactly why I'm looking
into this.
> Suggestions:
> - You can inherit a "ReadWriteSomethingImpl" from a
> "ReadOnlySomethingImpl";
> - Even if you delegate, you can use static methods only in the "write"
> part of the implementation, passing as parameter the storage block
> (maybe that one is the "ReadWriteSomethingImpl" itself?).
> Notice that this approach only adds an extra call since you do not
> need to keep a reference to each "WriteSomethingImpl" - at most you
> need a singleton per node type (not per node). Passing the data
> storage as parameter is something that non-static methods do all
> the time (behind the scenes) anyway, so, there is no extra cost
> there.
One thing I probably didn't explain well enough (these things are hard
to write down!) is that in many cases the extension layer really needs
the lower layer to be changed. This is what makes really hard to
separate features in layers.
If you look at the Deferred DOM implementation we have today for
instance, it is made of a set of classes that inherit from the
Non-Deferred DOM classes. However, for this to work, the base classes
have been modified to provide the hooks necessary for the subclasses
methods to be called. These methods are just no-ops in the base classes.
But already there is some (minor) overhead there.
And there is still a lot of code that is duplicated in the subclasses.
Code that would normally be shared through a common class. Even though,
I factorized some of this by moving some methods to the upper classes
and passing the data as parameter, like you suggest. By the way, one of
the drawbacks of this is also that it makes the base layer know about
(and therefore depend on) the extension layer. Far from being ideal...
In addition, this problem gets worse for every single extension you want
to implement as a layer. The number of possible combinations quickly
becomes dramatic.
> Are you giving some use to interfaces or are you working the C++ way?
> Are you "programming to inherit"?
I'm not sure what you mean here. But no, I do not use any interfaces
there. Using any interfaces here would mean more objects involved, which
is counter to my goal.
> Just trying to understand. Probably I am not understanding the "extract"
> operation you are talking about.
Well, the extract operation I'm talking about consist of running some
preprocessing tool to generate a new set of classes (java files) out of
the main ones to constitute a different DOM implementation (with a
different package name) supporting a different set of features.
--
Arnaud Le Hors - IBM Cupertino, XML Technology Group
RE: Managing/implementing several DOM implementations
Posted by Paulo Gaspar <pa...@krankikom.de>.
Alternatives... maybe...
> -----Original Message-----
> From: Arnaud Le Hors [mailto:lehors@us.ibm.com]
> Sent: Tuesday, July 18, 2000 03:52
>
> The only way to avoid the multiple inheritance diagrams that this uses
> is to forget ReadWriteNodeImpl and have all of its behavior implemented
> by each of the class ReadWriteElementImpl, ReadWriteAttrImpl, etc... But
> this means duplicating the same code on every single subclass. Major
> hassle! Unmanageable. Not really an option!
>
> There is another way to do that: delegation. This would imply having
> ReadOnlyNodeImpl provide for some extension mechanism which could be
> used to extend its behavior. This model, however, has two major
> drawbacks. The extension mechanism requires extra storage on the base
> class (at least one pointer), and every method call requires at least
> another method call. Both of these kills this option right off the bat,
> since they defeat my original goal of getting smaller and faster DOM
> implementations.
There are a couple of twists there:
1. You only need to delegate half of it;
2. You do NOT need an extra pointer per node.
Detailing, let's consider some facts:
a) Read functionality is used much more often than write
functionality;
b) Most of the time you DO NOT use writing without reading, but you
often use reading without writing;
c) Writing takes more code than reading (let's just mention
validation, including validation against a DTD/Schema).
A read-only parser would take much less memory without having to carry
the write part. This opens the door for big savings on a non-validating
read-only parser.
Suggestions:
- You can inherit a "ReadWriteSomethingImpl" from a
"ReadOnlySomethingImpl";
- Even if you delegate, you can use static methods only in the "write"
part of the implementation, passing as parameter the storage block
(maybe that one is the "ReadWriteSomethingImpl" itself?).
Notice that this approach only adds an extra call since you do not
need to keep a reference to each "WriteSomethingImpl" - at most you
need a singleton per node type (not per node). Passing the data
storage as parameter is something that non-static methods do all
the time (behind the scenes) anyway, so, there is no extra cost
there.
Keep in mind also that getting some overhead on the writting part does
not have that much impact if you keep efficient "batch" functionality
available. Examples:
- To create a subtree I would rather build the XML text, parse it
into a tree and then add it to the parent node than make API
calls for each element and attribute. It is much more readable,
maybe it is faster and if it is not it is usualy not much of a
problem for most of those writings;
- XSLT parsers (here performance is more problematic) tend to do
things in this way (batch) too, even if not by building strings.
In most stuff I do with XSLT there is a lot of subtree replication
going on from the input to the output stream.
(I already red in this list about strings being moved but I see
many subtrees going in the same way.)
> So what's left? The only option left that I know of is to implement the
> full DOM, and extract from it parallel hierarchies each of which only
> implements a subset of the full DOM. I can do that with some
> preprocessing without too much trouble. I already do as an experiment!
Are you giving some use to interfaces or are you working the C++ way?
Are you "programming to inherit"?
Just trying to understand. Probably I am not understanding the "extract"
operation you are talking about.
Have fun,
Paulo Gaspar