0

I have a series of StrBuf objects and want to know the most efficient way to concatenate them together.

There's the add() method but the docs say, "Add x.toStr to the end of this buffer". If I'm doing this over and over and over again, I'd imagine that StrBuf.toStr() is not that performant.

(I know the real answer is to just use the one StrBuf, but humour me here!)

Cheers.

UPDATE:

Looking at the Java source, under the hood StrBuf uses a Java StringBuilder which uses a char array as it's internal buffer. So @Adrian, yeah, it's important to have a large initial buffer.

As far as StrBuf.toStr() is concerned, a new Java String is created using Arrays.copyOfRange() - which is reasonable but unnecessary given there's an append(StringBuffer sb) method.

Steve Eynon
  • 4,979
  • 2
  • 30
  • 48

2 Answers2

1

I suppose you could create a new StrBuf with a big enough capacity:

capacity
    Int capacity
    The number of characters this buffer can hold without allocating more memory.

make
    new make(Int capacity := 16)
    Create with initial capacity (defaults to 16)
Adrian Panasiuk
  • 7,249
  • 5
  • 33
  • 54
  • Cheers, though I am currently creating a large initial StrBuf - I was more concerned with the overhead of the `toStr()` method before I add it. – Steve Eynon Dec 03 '13 at 22:07
1

Not really - there are no optimizations in StrBuf for that case. You seeing performance issues with what you have today?

Generally for our APIs we would pass around the OutStream instance from StrBuf.out and not the actual StrBuf instance. Not sure if that helps your case or not.

afrankvt
  • 181
  • 3
  • Thanks, "there are no optimizations in StrBuf for that case" is the answer I was looking for. :( Yeah, looking at the src I can see `add(Obj x)` essentially calls `x.toStr` - as this method is core, I guess I was wondering if it tried to do anything clever. – Steve Eynon Dec 03 '13 at 22:30
  • No performance issues, I'm just attempting some premature optimisation! – Steve Eynon Dec 03 '13 at 22:39