Skip to content

Commit 86ac98d

Browse files
Merge deque, CFFI, formatting, map layout and process pipe fixes
2 parents 92724f4 + a6766c0 commit 86ac98d

30 files changed

Lines changed: 1221 additions & 48 deletions

‎Changes.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22
libhxcpp 6.0.0 (unreleased)
33
------------------------------------------------------------
44

5+
* Fixed a sys.thread.Deque keeping the storage a burst grew it to once it drained: a thread mailbox that once held a million messages held 8 MB for as long as it lived. A deque that drains now gives back storage above 16,384 messages (libhxcpp #316)
6+
* Sped up sys.thread.Deque.push, which adds at the front and moved every queued item for each push while holding the queue's lock, so filling a deque from the front was quadratic. 65,536 pushes and pops went from 489k to 4.73M messages a second, the rate of adding at the back (libhxcpp #317)
7+
* Sped up val_string on a boxed non-ASCII string, which converted the whole string to UTF-8 into a new buffer on every call. The box now keeps the first conversion: reading a boxed 65,536-character string 256 times went from 3.3k to 284M calls a second, and peak memory from 40 MB to 9 MB (libhxcpp #322)
8+
* Sped up val_callN and val_ocallN with up to five arguments, which built an argument array for every call. They now call the fixed-arity entry points, as val_call0 to val_call3 do: a three-argument callback through val_callN went from 31M to 62M calls a second (libhxcpp #324)
9+
* Sped up Std.string on deeply nested enums, which copied each parameter's text into its parent's, quadratic in the depth, and recursed once per level, so a chain a million deep overflowed the native stack. Enums are now formatted in one pass: a 2,048-deep enum formats 33x faster (libhxcpp #328)
10+
* Fixed formatting a map that holds itself, directly or through other maps, ending the program with a stack overflow. Past 5 nested maps a map now formats as "...", as arrays and anonymous objects already did (libhxcpp #343)
11+
* Map entries with Int values in StringMap, ObjectMap and Int64 maps take 8 bytes less on 64-bit: the hash sat after the key, leaving padding beside the value. A 200,000-entry StringMap<Int> holds 9.6 MB instead of 11.2 MB (libhxcpp #370)
12+
* Fixed process pipes reaching an unrelated child on Linux, macOS and other POSIX systems: they were created with pipe and marked close-on-exec afterwards, so a process another thread started in between kept them, and the first child's output could stay open until that process exited. They are now created close-on-exec with pipe2 where the system has it, and elsewhere no process the runtime starts can fork in between (libhxcpp #340)
513
* Fixed a copy of an hx::Ref not keeping its object alive: copying one of the same type shared the original's GC root, so once the original went the collector could free the object the copy still held (libhxcpp #355)
614
* Fixed hx::Ref::setPtr leaving its GC root at the old offset when it moved to another interface of the same object, so the collector could free the object while the reference still held it (libhxcpp #361)
715
* Fixed an exception thrown by a finalizer leaving the collector paused: it unwound out of the collection with every other thread still stopped. The collection now finishes, running the other finalizers, and the first exception is thrown to the code that started it (libhxcpp #358)

‎bench/src/CffiBenchmarks.hx‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package;
2+
3+
/**
4+
CFFI calls from native code, the way an extension library makes them.
5+
The entry points are looked up through hx_cffi once, untimed; both
6+
runtimes export the same ones.
7+
**/
8+
@:cppFileCode('
9+
extern "C" void *hx_cffi(const char *inName);
10+
typedef hx::Object *(*BenchCallN)(hx::Object *, hx::Object **, int);
11+
typedef const char *(*BenchValString)(hx::Object *);
12+
13+
static BenchCallN bench_callN = 0;
14+
static BenchValString bench_val_string = 0;
15+
16+
static int bench_call_vector(Dynamic func, Dynamic a, Dynamic b, Dynamic c, int count)
17+
{
18+
if (!bench_callN)
19+
bench_callN = (BenchCallN)hx_cffi("val_callN");
20+
hx::Object *args[3] = { a.mPtr, b.mPtr, c.mPtr };
21+
int sum = 0;
22+
for(int i=0;i<count;i++)
23+
sum += (int)Dynamic(bench_callN(func.mPtr, args, 3));
24+
return sum;
25+
}
26+
27+
static int bench_string_views(Dynamic str, int count)
28+
{
29+
if (!bench_val_string)
30+
bench_val_string = (BenchValString)hx_cffi("val_string");
31+
int sum = 0;
32+
for(int i=0;i<count;i++)
33+
{
34+
const unsigned char *s = (const unsigned char *)bench_val_string(str.mPtr);
35+
sum = sum * 31 + s[0] + s[1];
36+
}
37+
return sum;
38+
}
39+
')
40+
class CffiNative {
41+
public static function callVector(func:Dynamic, a:Dynamic, b:Dynamic, c:Dynamic, count:Int):Int
42+
return untyped __cpp__('bench_call_vector({0}, {1}, {2}, {3}, {4})', func, a, b, c, count);
43+
44+
public static function stringViews(str:Dynamic, count:Int):Int
45+
return untyped __cpp__('bench_string_views({0}, {1})', str, count);
46+
}
47+
48+
/** val_callN with three arguments, the uniform call a CFFI library makes. **/
49+
class CffiCallVector extends Benchmark {
50+
static inline var N = 1000000;
51+
52+
final func:Dynamic = (a:Int, b:Int, c:Int) -> a + b + c;
53+
54+
public function new() {
55+
super("cffi_call_vector", "calls", N);
56+
}
57+
58+
override public function run():Int {
59+
// Small Ints are shared boxes, so no allocation per argument is timed
60+
return CffiNative.callVector(func, 1, 2, 3, N);
61+
}
62+
}
63+
64+
/** val_string on one boxed non-ASCII string, as a library reading the same value again. **/
65+
class CffiStringView extends Benchmark {
66+
static inline var N = 256;
67+
68+
var str:Dynamic;
69+
70+
public function new() {
71+
super("cffi_string_view", "string views", N);
72+
}
73+
74+
override public function setup() {
75+
var b = new StringBuf();
76+
for (i in 0...65536)
77+
b.addChar(0x3bb);
78+
str = b.toString();
79+
}
80+
81+
override public function run():Int {
82+
return CffiNative.stringViews(str, N);
83+
}
84+
}

‎bench/src/EnumBenchmarks.hx‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package;
2+
3+
enum Chain {
4+
End;
5+
Link(next:Chain);
6+
Pair(left:Chain, right:Int);
7+
}
8+
9+
/** Std.string on a deeply nested enum, as a debug print of a list or syntax tree. **/
10+
class EnumFormat extends Benchmark {
11+
static inline var DEPTH = 2048;
12+
static inline var REPS = 16;
13+
14+
var value:Chain;
15+
16+
public function new() {
17+
super("enum_format", "enum values", DEPTH * REPS);
18+
}
19+
20+
override public function setup() {
21+
value = End;
22+
for (i in 0...DEPTH)
23+
value = (i & 1) == 0 ? Link(value) : Pair(value, i);
24+
}
25+
26+
override public function run():Int {
27+
var sum = 0;
28+
for (r in 0...REPS) {
29+
var s = Std.string(value);
30+
var mid:Int = s.charCodeAt(s.length >> 1);
31+
sum = (sum * 31 + s.length + mid) | 0;
32+
}
33+
return sum;
34+
}
35+
}

‎bench/src/Main.hx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package;
22

33
import AllocBenchmarks;
4+
import CffiBenchmarks;
5+
import EnumBenchmarks;
46
import MapBenchmarks;
57
import SortBenchmarks;
68
import StringBenchmarks;
9+
import ThreadBenchmarks;
710
import cpp.vm.Gc;
811
import haxe.Json;
912
import haxe.Timer;
@@ -28,7 +31,7 @@ class Main {
2831
return [
2932
new AllocSmall(), new AllocStrings(), new AllocArrays(), new GcPauses(), new MapInt(), new MapString(), new MapObject(),
3033
new StringBuild(), new StringIndex(), new StringSubstr(), new StringSplit(), new StringCase(), new SortInt(), new SortString(),
31-
new SortObject(),
34+
new SortObject(), new DequePush(), new DequeBurst(), new CffiCallVector(), new CffiStringView(), new EnumFormat(),
3235
];
3336
#end
3437
}

‎bench/src/ThreadBenchmarks.hx‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package;
2+
3+
import sys.thread.Deque;
4+
5+
/**
6+
A deque filled at the front, as a work queue that puts urgent items
7+
first, then drained. Messages are the shared boxed 7, so no allocation
8+
per message is timed.
9+
**/
10+
class DequePush extends Benchmark {
11+
static inline var N = 65536;
12+
13+
public function new() {
14+
super("deque_push", "messages", N * 2);
15+
}
16+
17+
override public function run():Int {
18+
var q = new Deque<Dynamic>();
19+
for (i in 0...N)
20+
q.push(7);
21+
var sum = 0;
22+
for (i in 0...N)
23+
sum = (sum + (q.pop(false) : Int)) | 0;
24+
if (q.pop(false) != null)
25+
throw "queue not empty";
26+
return sum;
27+
}
28+
}
29+
30+
/**
31+
Repeated bursts through one long-lived deque: a mailbox that fills up,
32+
drains completely and fills up again. Kept to 8,192 messages, as stock
33+
hxcpp moves the whole queue for every pop.
34+
**/
35+
class DequeBurst extends Benchmark {
36+
static inline var N = 8192;
37+
static inline var BURSTS = 20;
38+
39+
final q = new Deque<Dynamic>();
40+
41+
public function new() {
42+
super("deque_burst", "messages", N * 2 * BURSTS);
43+
}
44+
45+
override public function run():Int {
46+
var sum = 0;
47+
for (b in 0...BURSTS) {
48+
for (i in 0...N)
49+
q.add(7);
50+
for (i in 0...N)
51+
sum = (sum + (q.pop(false) : Int)) | 0;
52+
}
53+
if (q.pop(false) != null)
54+
throw "queue not empty";
55+
return sum;
56+
}
57+
}

‎src/Enum.cpp‎

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <hxcpp.h>
2+
#include <vector>
23

34

45
// -------- Enums ---------------------------------
@@ -119,18 +120,60 @@ Dynamic EnumBase_obj::__GetItem(int inIndex) const
119120

120121

121122

123+
// Enum parameters are formatted in place, with a stack of the enums still
124+
// open, and every piece is joined once at the end. Building each parameter's
125+
// string and then copying it into its parent's copied the deepest text once
126+
// per level, O(n^2) for a chain n deep, and recursed once per level on the
127+
// native stack. Other parameters are converted in the same order as before
122128
String EnumBase_obj::toString() {
123129
if (mFixedFields==0)
124130
return _hx_tag;
125-
if (mFixedFields==1)
126-
return _hx_tag + HX_CSTRING("(") + _hx_getFixed()->asString() + HX_CSTRING(")");
127131

128-
Array<String> args = Array_obj<String>::__new(mFixedFields);
129-
cpp::Variant *v = _hx_getFixed();
130-
for(int i=0;i<mFixedFields;i++)
131-
args->__unsafe_set(i, v[i].asString());
132-
133-
return _hx_tag + HX_CSTRING("(") + args->join(HX_CSTRING(",")) + HX_CSTRING(")");
132+
Array<String> parts = Array_obj<String>::__new(0, 16);
133+
// Held in an Array, so the collector sees and moves them while a
134+
// parameter's own toString runs
135+
Array<Dynamic> open = Array_obj<Dynamic>::__new(0, 8);
136+
std::vector<int> nextParam;
137+
138+
parts->push(_hx_tag);
139+
parts->push(HX_CSTRING("("));
140+
open->push(Dynamic(this));
141+
nextParam.push_back(0);
142+
while(open->length > 0)
143+
{
144+
int top = open->length - 1;
145+
EnumBase_obj *e = static_cast<EnumBase_obj *>(open[top].mPtr);
146+
int i = nextParam[top];
147+
if (i==e->mFixedFields)
148+
{
149+
parts->push(HX_CSTRING(")"));
150+
open->pop();
151+
nextParam.pop_back();
152+
continue;
153+
}
154+
nextParam[top] = i + 1;
155+
if (i>0)
156+
parts->push(HX_CSTRING(","));
157+
158+
cpp::Variant &v = e->_hx_getFixed()[i];
159+
if (v.type==cpp::Variant::typeObject && v.valObject && v.valObject->__GetType()==vtEnum)
160+
{
161+
EnumBase_obj *child = static_cast<EnumBase_obj *>(v.valObject);
162+
parts->push(child->_hx_tag);
163+
if (child->mFixedFields>0)
164+
{
165+
parts->push(HX_CSTRING("("));
166+
open->push(Dynamic(child));
167+
nextParam.push_back(0);
168+
}
169+
}
170+
else
171+
{
172+
String s = v.asString();
173+
parts->push(s.raw_ptr() ? s : HX_CSTRING("null"));
174+
}
175+
}
176+
return parts->join(HX_CSTRING(""));
134177
}
135178

136179
}

‎src/String.cpp‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "hx/Hash.h"
1010
#include <hx/Thread.h>
1111
#include <locale>
12+
#include <atomic>
1213

1314
using namespace hx;
1415

@@ -3155,6 +3156,54 @@ class StringData : public hx::Object
31553156
String mValue;
31563157
};
31573158

3159+
#ifdef HX_SMART_STRINGS
3160+
// A boxed UTF-16 string keeps its UTF-8 form once asked for it, so native
3161+
// code calling val_string on the same value again does not convert and
3162+
// allocate the whole string each time. Only boxes made from a UTF-16 string,
3163+
// as containers, have the extra field
3164+
class Utf16StringData : public StringData
3165+
{
3166+
public:
3167+
Utf16StringData(String inValue) : StringData(inValue), mUtf8(0) { }
3168+
3169+
const char *__CStr() const HXCPP_OVERRIDE
3170+
{
3171+
const char *utf8 = mUtf8.load(std::memory_order_acquire);
3172+
if (utf8)
3173+
return utf8;
3174+
utf8 = mValue.utf8_str();
3175+
// Keep the first one made, so a pointer handed out stays alive with the box
3176+
const char *expected = 0;
3177+
if (!mUtf8.compare_exchange_strong(expected, utf8, std::memory_order_acq_rel))
3178+
return expected;
3179+
HX_OBJ_WB_GET(const_cast<Utf16StringData *>(this), (void *)utf8);
3180+
return utf8;
3181+
}
3182+
3183+
void __Mark(hx::MarkContext *__inCtx) HXCPP_OVERRIDE
3184+
{
3185+
StringData::__Mark(__inCtx);
3186+
const char *utf8 = mUtf8.load(std::memory_order_relaxed);
3187+
HX_MARK_ARRAY(utf8);
3188+
}
3189+
3190+
#ifdef HXCPP_VISIT_ALLOCS
3191+
void __Visit(hx::VisitContext *__inCtx) HXCPP_OVERRIDE
3192+
{
3193+
StringData::__Visit(__inCtx);
3194+
const char *utf8 = mUtf8.load(std::memory_order_relaxed);
3195+
if (utf8)
3196+
{
3197+
HX_VISIT_ARRAY(utf8);
3198+
mUtf8.store(utf8, std::memory_order_relaxed);
3199+
}
3200+
}
3201+
#endif
3202+
3203+
mutable std::atomic<const char *> mUtf8;
3204+
};
3205+
#endif
3206+
31583207

31593208

31603209

@@ -3188,6 +3237,10 @@ hx::Object *String::__ToObject() const
31883237
}
31893238
}
31903239

3240+
#ifdef HX_SMART_STRINGS
3241+
if (isUTF16Encoded())
3242+
return new (NewObjContainer) Utf16StringData(*this);
3243+
#endif
31913244
bool isConst = __s[HX_GC_CONST_ALLOC_MARK_OFFSET] & HX_GC_CONST_ALLOC_MARK_BIT;
31923245
NewObjectType type = isConst ? NewObjAlloc : NewObjContainer;
31933246
return new (type) StringData(*this);

0 commit comments

Comments
 (0)