File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes IDEs, Version Control and other tools and the fly likes DSL performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "DSL performance" Watch "DSL performance" New topic
Author

DSL performance

Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
As I understand it, creating a performant language is beyond the scope of the book. However, I am curious as to the performance of the kind of languages that the book does help us create as compared to a similar application written in java. I see this as another way to measure the cost of creating and using a DSL compared to the savings gained from it.

Thanks,
Burk


SCJP, SCJD, SCEA 5 "Any sufficiently analyzed magic is indistinguishable from science!" Agatha Heterodyne (Girl Genius)
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
What tools like ANTLR (which Terrence apparently uses in the book) give you is fast parsing for the textual input, turning a snippet of your language into an abstract syntax tree and from there, generating code (Java, C, Ruby, Python, ...) to match that AST. In other words, the input is a snippet of your DSL and the output is the equivalent code in your chosen target language. All in all, I suggest you'd have to be pretty darn good at writing compilers and code generators in order to beat the performance of a tool like ANTLR or other established "compiler-compilers".


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Lasse,
Sounds like you;ve got some experience with using ANTLR. My question is how efficient is the code ANTLR generates versus the code a good developer would create to do the same task?

Burk
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Burk Hufnagel wrote:Sounds like you;ve got some experience with using ANTLR. My question is how efficient is the code ANTLR generates versus the code a good developer would create to do the same task?

Actually, I don't have experience with ANTLR. I assume it's similar enough to other compiler-compilers, though.

Regarding the efficiency, the first question is whether the DSL is compiled or interpreted? Compiled DSL is, in theory, slightly slower at runtime than well-crafted manual implementation of the same functionality because the programmer may be able to make more specific assumptions, cutting corners if you will. In practice, I would guess that the performance advantage would be insignificant.

With interpreted DSLs you face an additional performance hit. Then again, you'd typically do runtime interpretation to accomplish something you couldn't couldn't solve at compile-time anyway. For example, to allow your users to customize their profile page with non-HTML markup language such as Markdown. You could implement the transformation yourself or you could delegate to a compiler-compiler-backed implementation, which comes back to whether your hand-crafted implementation is smarter than the defined syntax-based, tool-generated implementation.
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Lasse,
That sounds like well-thought out "it depends" -<grin> - which seems a fair answer to the question.
Burk
Terence Parr
author
Ranch Hand

Joined: Jan 13, 2010
Posts: 35
Burk Hufnagel wrote:As I understand it, creating a performant language is beyond the scope of the book. However, I am curious as to the performance of the kind of languages that the book does help us create as compared to a similar application written in java.


hi Burk, I'll limit my answer to interpreters since generated source code runs at the speed of the implementation language (such as Java).

Once you have the basic patterns down, the biggest factor in interpreter efficiency is the implementation language. The Dalvik VM (team led by Dan Borstein), for example, on the Google android system is written in assembly code and is very sensitive to the computer architecture. It avoids doing memory accesses whenever possible because flash memory is extremely slow. My interpreters follow the same pattern but the implementations look very different. Java is like programming with oven mitts on in comparison. You just don't have a lot of control over the underlying hardware. On the other hand, the interpreters are pretty fast because Java is pretty fast (if you ignore its libraries :wink and they are portable
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Terence,

I think I'm not being clear in what I was asking. I'm assuming that a DSL that generates code can't look at the code it's generated and determine that it's substandard in terms of efficiency the way that a human programmer could. If that's the case then one of the trade-offs in using the DSL is that the programmer can get done sooner and perhaps with fewer defects, but the code that is generated/executed is slower because it hasn't been optimized the way that the code in a general purpose language could be.

For example, a few months ago I was looking at some legacy Java code that validated a string containing a date and time value. There were over thirty lines of Java code in the method and I eventually replaced them with a single line of code using a regular expression to do the validation. Is it reasonable to believe that a DSL - whether interpreted or compiled - is going to be able to do?

Burk

PS Yes, I do think it's a bit ironic that I used a DSL to improve hand written code. <grin>
Terence Parr
author
Ranch Hand

Joined: Jan 13, 2010
Posts: 35
Burk Hufnagel wrote:I'm assuming that a DSL that generates code can't look at the code it's generated and determine that it's substandard in terms of efficiency the way that a human programmer could. If that's the case then one of the trade-offs in using the DSL is that the programmer can get done sooner and perhaps with fewer defects, but the code that is generated/executed is slower because it hasn't been optimized the way that the code in a general purpose language could be.


Hi Burk, well, I typically try to extract code generation templates from what I'm doing by hand. You can usually get it pretty efficient, though, you can always write an infinite loop in a DSL regardless of the code generation templates ;)

Yep, DSLs are trade-offs between runtime efficiency and expressivity. I would say that most problems that cry out for a DSL don't really care about efficiency. Heck, you might be waiting on the network or disk or user much of the time anyway. speed of generated code or execution in an interpreter matter most for general-purpose programming languages I think.
Burk Hufnagel
Ranch Hand

Joined: Oct 01, 2001
Posts: 814
    
    3
Terence Parr wrote:Yep, DSLs are trade-offs between runtime efficiency and expressivity. I would say that most problems that cry out for a DSL don't really care about efficiency. Heck, you might be waiting on the network or disk or user much of the time anyway. speed of generated code or execution in an interpreter matter most for general-purpose programming languages I think.


This sounds like the kind of answer I got when I first heard of Groovy and asked about performance. I suspect its a case of the right tool for the job. Sometimes high performance is critical, but many times it isn't as important as getting a working app out the door quickly.
Burk
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DSL performance