You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Lance Java <la...@googlemail.com> on 2012/06/15 09:57:57 UTC

Element.getChildren() returns ArrayList

When manipulating the serverside DOM in a mixin etc, the only public API for
accessing a node's children is via Element.getChildren().

The getChildren() method creates a new ArrayList, adds all the children and
returns it which I think is inneficcient.  I would have expected
getChildren() to return a List implementation that was able to iterate from
firstChild to lastChild without copying all children to an array.
Element.isEmpty() calls getChildren() under the hood.

Here is the code from org.apache.tapestry5.dom.Element:


   public List<Node> getChildren()
   {
       List<Node> result = CollectionFactory.newList();
       Node cursor = firstChild;
   
       while (cursor != null)
       {
           result.add(cursor);
           cursor = cursor.nextSibling;
       }
   
       return result;
   }

    public boolean isEmpty()
    {
        List<Node> children = getChildren();

        if (children.isEmpty())
            return true;

        for (Node n : children)
        {
            if (n instanceof Text)
            {
                Text t = (Text) n;

                if (t.isEmpty())
                    continue;
            }

            // Not a text node, or a non-empty text node, then the element
isn't empty.
            return false;
        }

        return true;
    }


--
View this message in context: http://tapestry.1045711.n5.nabble.com/Element-getChildren-returns-ArrayList-tp5713921.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Element.getChildren() returns ArrayList

Posted by Howard Lewis Ship <hl...@gmail.com>.
Importantly, the critical thing done with an Element, rendering markup to a
character stream, walks the list and doesn't use this method.

Earlier versions of Tapestry uses an internal ArrayList for storing the
children; this was changed in 5.1 or 5.2 to help reduce the memory
footprint of pages.

On Fri, Jun 15, 2012 at 8:46 AM, Thiago H de Paula Figueiredo <
thiagohp@gmail.com> wrote:

> On Fri, 15 Jun 2012 12:35:07 -0300, Lance Java <la...@googlemail.com>
> wrote:
>
>  Have you checked http://tapestryxpath.**sourceforge.net<http://tapestryxpath.sourceforge.net>
>>>
>> I'm aware of the library but I haven't used it. I can only assume that it
>> makes use of Element.getChildren() too (unless it accesses the private
>> "firstChild" and "nextSibling" fields which I highly doubt).
>>
>
> I guess you're right.
>
>
>  Have you done any benchmarks to know how much resources (CPU time,
>>> memory) it actually uses
>>>
>> No, but I think we can both see that getChildren() will require 2N
>> iterations to iterate the list of children. I think we can also see that
>> isEmpty() will cause N iterations where 1 would do.
>>
>
> Unless N is very high, it won't make a difference. Still O(n).
>
>
>  If I spent my time benchmarking things like this instead of just fixing
>> them, I'd never get anything done ;)
>>
>
> Yep, but you risk optimizing something that will make a very low
> difference in the end. And this specific code is not broken. ;)
>
>
> --
> Thiago H. de Paula Figueiredo
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.**apache.org<us...@tapestry.apache.org>
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Howard M. Lewis Ship

Creator of Apache Tapestry

The source for Tapestry training, mentoring and support. Contact me to
learn how I can get you up and productive in Tapestry fast!

(971) 678-5210
http://howardlewisship.com

Re: Element.getChildren() returns ArrayList

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Fri, 15 Jun 2012 12:35:07 -0300, Lance Java <la...@googlemail.com>  
wrote:

>> Have you checked http://tapestryxpath.sourceforge.net
> I'm aware of the library but I haven't used it. I can only assume that it
> makes use of Element.getChildren() too (unless it accesses the private
> "firstChild" and "nextSibling" fields which I highly doubt).

I guess you're right.

>> Have you done any benchmarks to know how much resources (CPU time,
>> memory) it actually uses
> No, but I think we can both see that getChildren() will require 2N
> iterations to iterate the list of children. I think we can also see that
> isEmpty() will cause N iterations where 1 would do.

Unless N is very high, it won't make a difference. Still O(n).

> If I spent my time benchmarking things like this instead of just fixing
> them, I'd never get anything done ;)

Yep, but you risk optimizing something that will make a very low  
difference in the end. And this specific code is not broken. ;)

-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Element.getChildren() returns ArrayList

Posted by Lance Java <la...@googlemail.com>.
> Have you checked http://tapestryxpath.sourceforge.net
I'm aware of the library but I haven't used it. I can only assume that it
makes use of Element.getChildren() too (unless it accesses the private
"firstChild" and "nextSibling" fields which I highly doubt).

> Have you done any benchmarks to know how much resources (CPU time, 
> memory) it actually uses
No, but I think we can both see that getChildren() will require 2N
iterations to iterate the list of children. I think we can also see that
isEmpty() will cause N iterations where 1 would do.

If I spent my time benchmarking things like this instead of just fixing
them, I'd never get anything done ;)

--
View this message in context: http://tapestry.1045711.n5.nabble.com/Element-getChildren-returns-ArrayList-tp5713921p5713929.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Element.getChildren() returns ArrayList

Posted by Thiago H de Paula Figueiredo <th...@gmail.com>.
On Fri, 15 Jun 2012 04:57:57 -0300, Lance Java <la...@googlemail.com>  
wrote:

> When manipulating the serverside DOM in a mixin etc, the only public API  
> for accessing a node's children is via Element.getChildren().

Have you checked http://tapestryxpath.sourceforge.net/? I haven't yet, but  
I should. :)

> The getChildren() method creates a new ArrayList, adds all the children  
> and returns it which I think is inneficcient.

Well, have you done any benchmarks to know how much resources (CPU time,  
memory) it actually uses to know whether it's really a bottleneck?  
Otherwise, we could have a case of premature optimization.

Anyway, thanks for the tip. It look like a good improvement, specially in  
isEmpty(), which doesn't seem to need to use getChildren(). :)

I would have expected
> getChildren() to return a List implementation that was able to iterate  
> from
> firstChild to lastChild without copying all children to an array.
> Element.isEmpty() calls getChildren() under the hood.
>
> Here is the code from org.apache.tapestry5.dom.Element:
>
>
>    public List<Node> getChildren()
>    {
>        List<Node> result = CollectionFactory.newList();
>        Node cursor = firstChild;
>       while (cursor != null)
>        {
>            result.add(cursor);
>            cursor = cursor.nextSibling;
>        }
>       return result;
>    }
>
>     public boolean isEmpty()
>     {
>         List<Node> children = getChildren();
>
>         if (children.isEmpty())
>             return true;
>
>         for (Node n : children)
>         {
>             if (n instanceof Text)
>             {
>                 Text t = (Text) n;
>
>                 if (t.isEmpty())
>                     continue;
>             }
>
>             // Not a text node, or a non-empty text node, then the  
> element
> isn't empty.
>             return false;
>         }
>
>         return true;
>     }
>
>
> --
> View this message in context:  
> http://tapestry.1045711.n5.nabble.com/Element-getChildren-returns-ArrayList-tp5713921.html
> Sent from the Tapestry - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>


-- 
Thiago H. de Paula Figueiredo

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org