Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
libhxcpp 6.0.0 (unreleased)
------------------------------------------------------------

* Sped up joining an Array<Int>, which made a String for each element and then joined those. The digits go straight into the result, so joining 8,192 integers 200 times takes 41 ms instead of 53 ms, what writing them with std::to_chars takes (libhxcpp #415)
* Fixed Gc.memInfo64(Gc.MEM_INFO_CURRENT) leaving out most of what was allocated since the last collection: a block taken from the free list without the lock, which is the usual way, was not counted until the next collection. Retaining 2 MB of arrays showed 90 KB more (libhxcpp #414)
* Fixed Date.now() on Windows going back to 1902 after January 2038, x64 builds too: the seconds since 1970 passed through a long, which is 32 bits on Windows (libhxcpp #413)
* Added timedWait to hx::thread::ConditionVariable, which Haxe 5's sys.thread.Condition.timedWait calls: with the Haxe nightly, every program that used Condition failed to compile. It returns true once a signal or broadcast came and false when the time ran out, and it waits out the wakeups MSVC's timed waits give just before the deadline with nothing signalled
* Fixed an exception from blit, setSize or resize under the cppia JIT ending the process instead of reaching the script's catch, on typed arrays and on Array<Dynamic>: their native calls had no exception bridge. The script catches it, as it does interpreted and in compiled code (libhxcpp #404)
* Fixed resize() and cpp.NativeArray.setSize() on an Array<Dynamic> under the cppia JIT evaluating the array expression twice, so a call in it ran twice and the array it returned the second time was the one resized. The JIT evaluates it once (libhxcpp #411)
Expand Down
60 changes: 60 additions & 0 deletions src/Array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,59 @@ String ArrayBase::joinArray(Array_obj<String> *inArray, String inSeparator)
}


static inline int intDigits(int inValue)
{
unsigned int u = inValue<0 ? (unsigned int)0 - (unsigned int)inValue : (unsigned int)inValue;
int digits = inValue<0 ? 2 : 1;
while(u>=10)
{
u /= 10;
digits++;
}
return digits;
}

// The digits go straight into the result, as String(int) writes them: a String
// for each element was an allocation for each value of more than one digit
// (libhxcpp #415)
static String joinInts(ArrayBase *inBase, String inSeparator)
{
int length = inBase->length;
const int *values = (const int *)inBase->GetBase();
long long total = (long long)(length-1) * inSeparator.length;
for(int i=0;i<length;i++)
total += intDigits(values[i]);
if (total > 0x7fffffff-8)
hx::Throw( HX_CSTRING("String too long") );
int len = (int)total;
char *buf = hx::NewString(len);

// A moving collection may have run for the allocation
values = (const int *)inBase->GetBase();
char *pos = buf;
for(int i=0;i<length;i++)
{
if (i && inSeparator.length)
{
memcpy(pos,inSeparator.raw_ptr(),inSeparator.length);
pos += inSeparator.length;
}
int value = values[i];
int digits = intDigits(value);
unsigned int u = value<0 ? (unsigned int)0 - (unsigned int)value : (unsigned int)value;
char *p = pos + digits;
do {
*--p = (char)('0' + (u % 10));
u /= 10;
} while (u);
if (value<0)
*--p = '-';
pos += digits;
}
buf[len] = '\0';
return String(buf,len);
}

// Per thread, as an anonymous object's is: a thread formatting a nested array
// must not use up the depth of another formatting its own
static thread_local int _hx_toString_depth = 0;
Expand All @@ -685,6 +738,13 @@ String ArrayBase::joinArray(ArrayBase *inBase, String inSeparator)
if (length==0)
return HX_CSTRING("");

#ifdef HX_SMART_STRINGS
if (inBase->getStoreType()==hx::arrayInt && !inSeparator.isUTF16Encoded())
#else
if (inBase->getStoreType()==hx::arrayInt)
#endif
return joinInts(inBase, inSeparator);

Array<String> stringArray = Array_obj<String>::__new(length, length);
_hx_toString_depth++;
try
Expand Down
3 changes: 2 additions & 1 deletion src/hx/Date.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,8 @@ double __hxcpp_date_now()
ularge.LowPart = file_time.dwLowDateTime;
ularge.HighPart = file_time.dwHighDateTime;

return (double)( (long) ((ularge.QuadPart - EPOCH) / 10000000L) ) +
// A long is 32 bits on Windows, x64 too, and the seconds pass it in 2038
return (double)( (__int64) ((ularge.QuadPart - EPOCH) / 10000000L) ) +
system_time.wMilliseconds*0.001;
#elif defined(USE_GETTIMEOFDAY)
struct timeval tv;
Expand Down
8 changes: 7 additions & 1 deletion src/hx/gc/Immix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4838,7 +4838,12 @@ class GlobalAllocator

// After zero/reclaim, it might be that the hole size is smaller than we thought.
if (result->mMaxHoleSize>=inRequiredBytes)
{
// Assume all will be used, as below: a block taken here was
// missing from the current memory until the next collection
mCurrentRowsInUse += result->GetFreeRows();
return result;
}

repoolReclaimedBlock(result);
continue;
Expand Down Expand Up @@ -7636,7 +7641,8 @@ class GlobalAllocator


size_t mRowsInUse;
size_t mCurrentRowsInUse;
// Threads take free blocks without a lock, and count them here
std::atomic_size_t mCurrentRowsInUse;
std::atomic_size_t mLargeAllocSpace;
size_t mLargeAllocForceRefresh;
// Read unlocked to decide on a collection, changed under mLargeListLock
Expand Down
24 changes: 24 additions & 0 deletions test/regression/DateNowAfter2038/FutureClock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#if defined(_WIN32) && defined(_MSC_VER)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>

static inline void WINAPI hxcppTestFutureSystemTime(LPSYSTEMTIME result)
{
SYSTEMTIME value = {};
value.wYear = 2040;
value.wMonth = 1;
value.wDay = 1;
value.wMilliseconds = 125;
*result = value;
}

#define GetSystemTime hxcppTestFutureSystemTime
#define HXCPP_TEST_FUTURE_CLOCK 1
#endif
29 changes: 29 additions & 0 deletions test/regression/DateNowAfter2038/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@:buildXml('<files id="runtime"><compilerflag value="/FI${HXCPP}/test/regression/DateNowAfter2038/FutureClock.h" if="isMsvc"/></files><files id="haxe"><compilerflag value="/FI${HXCPP}/test/regression/DateNowAfter2038/FutureClock.h" if="isMsvc"/></files>')
@:cppFileCode('
static bool futureClockActive() {
#ifdef HXCPP_TEST_FUTURE_CLOCK
return true;
#else
return false;
#endif
}
')
class Main {
static function main() {
var exact = true;
var year = true;
var control = true;
if (untyped __cpp__("futureClockActive()")) {
var expected = 2208988800125.0;
var date = Date.now();
exact = date.getTime() == expected;
year = date.getUTCFullYear() == 2040;
control = Date.fromTime(expected).getUTCFullYear() == 2040;
if (Sys.args().length > 0)
Sys.println(haxe.Json.stringify({actual: date.getTime(), expected: expected, utcYear: date.getUTCFullYear()}));
}
Sys.println("Date.now preserves the simulated 2040 timestamp: " + exact);
Sys.println("Date.now returns the simulated UTC year: " + year);
Sys.println("explicit post-2038 dates remain correct: " + control);
}
}
2 changes: 2 additions & 0 deletions test/regression/DateNowAfter2038/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-m Main
--cpp bin
3 changes: 3 additions & 0 deletions test/regression/DateNowAfter2038/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Date.now preserves the simulated 2040 timestamp: true
Date.now returns the simulated UTC year: true
explicit post-2038 dates remain correct: true
51 changes: 51 additions & 0 deletions test/regression/GcCurrentMemory/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#if !emscripten
@:cppFileCode('
#include <thread>
#include <hx/Native.h>
static void runJoined(Dynamic callback) {
std::thread worker([callback]() mutable { hx::NativeAttach attach; callback(); });
hx::AutoGCFreeZone freeZone;
worker.join();
}
')
#end
class Main {
static var arrays:Array<Array<Int>>;
static function allocate():Void {
arrays = [];
for (i in 0...2048) {
var values:Array<Int> = [];
values.resize(256);
values[0] = i;
values[255] = -i;
arrays.push(values);
}
}
static function main() {
var current = true;
var collected = true;
var intact = true;
#if !emscripten
for (_ in 0...3) cpp.vm.Gc.run(true);
var before = cpp.vm.Gc.memInfo64(cpp.vm.Gc.MEM_INFO_CURRENT);
var liveBefore = cpp.vm.Gc.memInfo64(4);
cpp.vm.Gc.enable(false);
untyped __cpp__("runJoined({0})", allocate);
var after = cpp.vm.Gc.memInfo64(cpp.vm.Gc.MEM_INFO_CURRENT);
cpp.vm.Gc.enable(true);
var payload = 2048.0 * 256 * 4;
current = after - before >= payload;
cpp.vm.Gc.run(true);
var liveAfter = cpp.vm.Gc.memInfo64(4);
var refreshed = cpp.vm.Gc.memInfo64(cpp.vm.Gc.MEM_INFO_CURRENT);
collected = liveAfter - liveBefore >= payload && refreshed - before >= payload;
for (i in 0...arrays.length)
if (arrays[i][0] != i || arrays[i][255] != -i) intact = false;
if (Sys.args().length > 0)
Sys.println(haxe.Json.stringify({payload: payload, before: before, after: after, afterCollection: refreshed, liveGrowth: liveAfter - liveBefore}));
#end
Sys.println("current heap estimate includes the newly allocated payload: " + current);
Sys.println("full collection accounts for the retained payload: " + collected);
Sys.println("all retained arrays remain intact: " + intact);
}
}
2 changes: 2 additions & 0 deletions test/regression/GcCurrentMemory/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-m Main
--cpp bin
3 changes: 3 additions & 0 deletions test/regression/GcCurrentMemory/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
current heap estimate includes the newly allocated payload: true
full collection accounts for the retained payload: true
all retained arrays remain intact: true
27 changes: 27 additions & 0 deletions test/regression/IntArrayJoin/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// An Array<Int> writes its digits straight into the joined string
// (openfl/libhxcpp#415), which must give what joining the elements' own
// strings gave, with any separator
class Main {
static function expected(values:Array<Int>, separator:String):String {
var b = new StringBuf();
for (i in 0...values.length) {
if (i > 0)
b.add(separator);
b.add(Std.string(values[i]));
}
return b.toString();
}

static function main() {
var values:Array<Int> = [0, -1, 1, 9, 10, -10, 99, 100, 2147483647, -2147483648, -2147483647];
for (i in 0...1000)
values.push((i * 7919) ^ (i << 20));
var same = true;
for (separator in [",", "", ", ", " - ", "é", "\u{1F600}"])
if (values.join(separator) != expected(values, separator))
same = false;
Sys.println("joined integers match their strings: " + same);
Sys.println("one: " + [42].join(",") + ", none: [" + ([] : Array<Int>).join(",") + "]");
Sys.println("printed: " + Std.string([-3, 0, 12]) + " " + [[1, -2], [30]]);
}
}
2 changes: 2 additions & 0 deletions test/regression/IntArrayJoin/build.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-m Main
--cpp bin
3 changes: 3 additions & 0 deletions test/regression/IntArrayJoin/stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
joined integers match their strings: true
one: 42, none: []
printed: [-3,0,12] [[1,-2],[30]]
Loading