HaxeFoundation / HaxeFoundation/haxe

Improve `StringBuf.add` to increase performance

Open
#5,440 38 comments 1 reaction 0 assignees View on GitHub
enhancement
Dominant language
Haxe
Stars
6.9k
Forks
715
Avg merge
2d 2h
Merged PRs (30d)
11

Description

My problem lies with this:

``` haxe
class StringBuf {
public inline function add(x :T):Void;
}
```

Because of this signature, anything added to a `StringBuf` gets wrapped in `Std.string`, which is true for `String` itself.

To milk the irony of the situation: there is no fast way to add a `String` to a `StringBuf`.

Therefore I propose this change:

``` haxe
class StringBuf {
public inline function add(x:StringBufFragment):Void;
}

private abstract StringBufFragment(String) from String to String {
// possibly add fast paths here for @:from Int/Float/Bool
@:from static inline function ofAny(a:A):StringBufFragment return Std.string(a);
}
```

That gives me a speedup of 1.5 - 3.5 depending on browser: http://try.haxe.org/#b3b92

What's more, we can see there's far less code in the output too: http://try.haxe.org/#F5816

Would such a change be ok?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.