Jan Obdržálek wrote:
Hello,

thank you very much for your help. Here are the answers.

  
1) Did you you tune the token and tree node allocation pool to reflect
something like the number of tokens you might expect (this really just
small, large) the default is usually good for most things. This grammar is
creating 909856 tree nodes according to your stats and this does not seem
quite right but possibly is - if it is then you need to change the
allocation pools to be much bigger, in which case the algorithm will make
far less. Have you by chance turned on output=AST but not shaped the tree in
anyway, resulting in a flat tree that must be rewritten all the time? The
other possibility is the tree building mechanism - currently the way that
the code is generated is not optimal, even though the runtime mechanisms are
- we do a lot of stream rewriting which isn;t strictly speaking necessary
and the gcc compiler won't be doing this.
    

The grammar generates a pretty standard AST tree (definitely not a
flat one). 
OK - I still need to see the grammar really because the only reason I can think of that your run is producing nearly a million tree nodes, is that something isn't quite right. In the current release, when a subtree is rewritten in a higher rule, it is supposed to be copying the pointers and not creating new nodes. I can only think that it isn't doing that here.

Turning off the tree-building mechanism made all the
difference - it saved about 80% of the running time. Being on par with
gcc here (although we still need to remember that gcc does a full
compilation) is quite a nice result for a generated parser.
  
Parsing and pre-processing will take more time than compiling usually. If you optimize the code to death and include optimization time then there will be a lot more work being done, but code generation and related things should not be a large part of the time.
<<<
~/tmp/test$ time ./parser
Reading a.c

real    0m0.481s
user    0m0.360s
sys     0m0.108s
  

The problem is I need the AST :(
  
Of course :-) I spent a lot of time optimizing the tree building code in the current release - I don't think that there is anything inherently wrong with the  mechanism, but I am beginning to think that something is causing way more nodes to be produced than should be. The constant, redundant rewriting of node streams is definitely an issue, but it should not be this big of an issue I think.
2) You don't say whether you are doing anything in the grammar, such as
accessing text and so on - you have to take out any action code to see what
the actual baseline is for the parser. For instance accessing token text is
a convenience function and you would not use it in a compiler that requires
speed;
    

There is no action code except for handling typedefs. This is not a problem.
  
OK -
  
3) As per the examples, when you know that the input stream is 8 bit or 16
bit, you can set a macro in your grammar such that it traverses the input
stream directly using pointers and not via function calls. Did you turn this
on.
    

I tried

@lexer::header{
#define ANTLR3_INLINE_INPUT_ASCII
}

This actually makes the parser a tiny bit slower than without the #define.
  
Hmm, it shouldn't in theory and all my own tests said it was faster, but perhaps there is some optimization going on that means the function calls are being inlined (though as pointers to functions that seems unlikely) or generated very efficiently.
  
4) I would need to see your grammar rather than a gprof (BTW - use
kcachegrind for better views of this). Realistically, the gcc compiler has
been worked on for years and is hand crafted, whereas if your grammar has
predicates or other inefficient methodology, it isn't much of a comparison.
It may not be so much that ANTLR generates slower code as that the grammar
could be better, but there is no way to know without your grammar to look at
(which I would like to see before looking further).
    

Thanks for suggesting kcachegrind. It is much better than gprof :) I
do not think valgrind existed the last time I did any serious
programming in plain C ... My only complaint is the cycle detection.
Having <cycle1> accounting for most of the time is not what I really
want. However if I skip cycle detection, kcachegring grabs 100% of one
CPU core and keeps crunching ...
  
There are various options of course, but the GUI allows you to view the results without the cycle getting in the way.
The profiling info in kcachegring after turning off the tree-building
seems to be about what I expected. To give you the idea:
20.6% (inclusive time) mTokens[called 255 744 times] - this is the lexer part
12% (inclusive time) antlr3dfapredict  - that's not too bad. I can
definitely decrease this significantly.
12% (inclusive time) isTypeName [called 21 800 times] - this function
checks whether the given identifier is a type name or not. It calls ~3
800 000 times strcmp (4.13%) That can also be improved.
  
Right - so your one function dealing with strings is 12% of the time (ignoring the tree stuff for the moment), rather than strcmp you should use memcmp, but it would be faster to use a TRIE. My general purpose hash map is convenient but when you are trying to squeeze optimal performance it might be slower than what you have (at least once you stop using strcmp) - give it a try though as hash maps are generally reasonable performers if sized correctly (the one I provide is not dynamic so you have to pick a modulo that is big enough up front). I provide int and int64 tries in the runtime, but I did not implement a string based version as I did not need it in the runtime - I always meant to come back and do that though.

Also, in determining that something is a typedef, you obviously need the string that represents the typedef and the token. How are you gathering that? If you are using $text, then it will be much slower than it needs to be. Use the pointer to the token start and end/length - you should not even need to make a copy of it, just point directly into the source and store the length of it.

The dfa prediction times can only really be improved if there is a better way to specify your lexer tokens than the way you are doing it. There may not be.

But basically, most of the time otherwise being in the lexer is what you would expect.
  
Sure, I'm not expecting miracles :) That time without the
tree-building seems reasonable to me.
  
Yeah - but the tree building should not be taking up this amount of time - it could be just that that is how the code is currently generated, but I am not convinced - I need the grammar - perhaps you can send me a copy privately before you publish it.
To sum up - the main culprit is the tree-building mechanism. Other
than that the performance of the generated parser is quite reasonable.
The problem is that tree-building/rewriting capabilities of ANTLR are
the main reason why we use ANTLR. 
Yes - I need to work out what is going wrong performance wise there. Do you happen to know how many node are in the final tree?
Second - I need (or would prefer) a
fast Java target. Java does not have to be much slower than C. 
Except for startup time, there is no way around that. However I think that for tasks like this Java is never going be close to the C target and while it is true that Sun's JIT can beat C in a lot of things such as branching, there are design patterns in C that you just cannot do in Java and generally that will be the biggest time difference. There is too much overhead in object creation and method calls. Apart from the startup time for Java being a huge problem for utilities like this (a long running application probably doesn't care that much - I have got used to the 15 second startup time for Netbeans  on my QX9660 ;-), the inability to lay down static code that is set out at compile time means that the Java target has to uncompress the data for DFA tables before it even gets going.
If Java
is that much slower, we will probably end up using C target for the
parser and then translating the AST into a Java structure ... I will
turn off the tree-building in Java and see what happens.
  
By all means tell us your results. In general I have found that parsing without tree building can get to be about half as fast as the C, so long as you take out the startup time. However, when you take out the time spent reading the source files (which is going to be about the same overhead for both), you find the difference is quite marked.

If the time for Java without tree building is a lot less, then it tends to indicate that the inefficiency is in the generated code for tree node streams, which as Ter has already indicated, needs some optimization work in terms of removing the generation of code that is redundant.


Jim
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to