Jump to content
Excelsior Forums
Sign in to follow this  
roedy

StringBuilder Optimisation

Recommended Posts

I wrote some months back that I got my main app, http://mindprod.com/products1.html#HTMLMACROS running 10% faster just by semi-automatically optimising the size of each new StringBuilder.

This set me wondering how the optimisation process might be fine tuned further, and perhaps turned into a commercial tool to fine tune it for any app, or built into Jet.

Let's assume that for every new StringBuilder invocation I collected statistics of every string built and how long it was over a sample run.

Knowing that, how big should you make the initialisation parameter?

Right now I pretty well always set it to the longest string. But in a few cases I made it shorter. 

Setting it to the longest string conserves CPU time in an infinitely RAM rich environment.  However outside the government, you probably also want to conserve RAM, and garbage collection time, by making letting a few of the StringBuilders grow in return for most StringBuilders being smaller.

You need some measure of tradeoff of doublings vs wasted bytes in a given invocation.  I suppose you might find that number by experiment.

A mindless algorithm might work like this:

Simulate the doublings and wasted bytes of every for every possible StringBuilder size between min and max length and pick the optimal for each instance.  This would not be done while the app was running, so even a crude (o n^2) algorithm like that would likely be acceptable.  When you find them out, you modify the source, though in Jet's case it might just modify the object based on tables previously collected, somewhat the way JetPerfect used to work.

Back then you rejected the idea as not generally applicable. By now you might be running out of ideas to keep Jet significantly faster than Sun JVM, and this optimisation may give you quite a boost for relatively little effort on a fairly large class of problem.

Share this post


Link to post
Share on other sites

Hi Roedy,

Thank you for sharing the idea.

By now you might be running out of ideas to keep Jet significantly faster than Sun JVM

This time has not yet come.  B)

and this optimisation may give you quite a boost for relatively little effort on a fairly large class of problem.

We will have in mind your proposal. If we get convinced in its general applicability, we will look at it closely.

Just to make sure: under "somewhat the way JetPerfect used to work" and "StringBuilder size between min and max length and pick the optimal for each instance" you mean a profile-based optimization to optimally set the initial capacity of StringBuilder (and standard container) instances specific for each allocation site. Note that it's tough to differentiate instances created by the same allocation site.

Is it correct?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×