• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DSL performance

 
Ranch Hand
Posts: 883
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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".
 
Burk Hufnagel
Ranch Hand
Posts: 883
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 883
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lasse,
That sounds like well-thought out "it depends" -<grin> - which seems a fair answer to the question.
Burk
 
author
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ) and they are portable :)
 
Burk Hufnagel
Ranch Hand
Posts: 883
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 883
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic