fuzion icon indicating copy to clipboard operation
fuzion copied to clipboard

add `StringBuilder` like effect

Open maxteufel opened this issue 9 months ago • 3 comments

maxteufel avatar Mar 12 '25 14:03 maxteufel

I do not think this must be an effect, it would be sufficient to have a String that internally uses expanding_array such that it has an efficient concatenation using s3 := s1 + s2.

As a first step, it would be good to start with a benchmark that performs repeated String concatenations and check the performance, in particular how the performance evolves if there are many thousands of small strings appended to a string that is getting longer and longer.

If the performance is good, no need to do anything. If it is bad, i.e., the average time to append a small String increases quicker than O(log length), then we might try to improve the performance. If that fails, a dedicated StringBuilder based on `expanding_array' might be needed.

fridis avatar Mar 12 '25 14:03 fridis

I was thinking more for the convenience of doing sb.append "new substring".

maxteufel avatar Mar 12 '25 14:03 maxteufel

Might be convenient because with this approach s3 := s1 + s2 we can't do:

s1 := ""
if cond
  code...
  s2 := s1 + "sth"
  code...
else
  code...
  s2 := s1 + "sthelse"
  code...
s3 := s2 + "end"

but with string builder it is possible:

sb := ""
if cond
  code...
  sb.append "sth"
  code...
else
  code...
  sb.append "sthelse"
  code...
sb.append "end"

michaellilltokiwa avatar Apr 03 '25 08:04 michaellilltokiwa