diff --git a/Changes.md b/Changes.md index d3372b98b..e9d59ed8f 100644 --- a/Changes.md +++ b/Changes.md @@ -2,6 +2,9 @@ libhxcpp 6.0.0 (unreleased) ------------------------------------------------------------ +* Sped up joining an Array, 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: 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 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) diff --git a/src/Array.cpp b/src/Array.cpp index f1a5865b3..69e1f2ba7 100644 --- a/src/Array.cpp +++ b/src/Array.cpp @@ -674,6 +674,59 @@ String ArrayBase::joinArray(Array_obj *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 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;igetStoreType()==hx::arrayInt && !inSeparator.isUTF16Encoded()) + #else + if (inBase->getStoreType()==hx::arrayInt) + #endif + return joinInts(inBase, inSeparator); + Array stringArray = Array_obj::__new(length, length); _hx_toString_depth++; try diff --git a/src/hx/Date.cpp b/src/hx/Date.cpp index b2d508077..af3d900f4 100644 --- a/src/hx/Date.cpp +++ b/src/hx/Date.cpp @@ -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; diff --git a/src/hx/gc/Immix.cpp b/src/hx/gc/Immix.cpp index 77e204196..66dc35a62 100644 --- a/src/hx/gc/Immix.cpp +++ b/src/hx/gc/Immix.cpp @@ -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; @@ -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 diff --git a/test/regression/DateNowAfter2038/FutureClock.h b/test/regression/DateNowAfter2038/FutureClock.h new file mode 100644 index 000000000..2a0f3166b --- /dev/null +++ b/test/regression/DateNowAfter2038/FutureClock.h @@ -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 + +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 diff --git a/test/regression/DateNowAfter2038/Main.hx b/test/regression/DateNowAfter2038/Main.hx new file mode 100644 index 000000000..7dd5708eb --- /dev/null +++ b/test/regression/DateNowAfter2038/Main.hx @@ -0,0 +1,29 @@ +@:buildXml('') +@: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); + } +} diff --git a/test/regression/DateNowAfter2038/build.hxml b/test/regression/DateNowAfter2038/build.hxml new file mode 100644 index 000000000..b484e67e7 --- /dev/null +++ b/test/regression/DateNowAfter2038/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/DateNowAfter2038/stdout.txt b/test/regression/DateNowAfter2038/stdout.txt new file mode 100644 index 000000000..7d0a2af25 --- /dev/null +++ b/test/regression/DateNowAfter2038/stdout.txt @@ -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 diff --git a/test/regression/GcCurrentMemory/Main.hx b/test/regression/GcCurrentMemory/Main.hx new file mode 100644 index 000000000..ac91fa3ff --- /dev/null +++ b/test/regression/GcCurrentMemory/Main.hx @@ -0,0 +1,51 @@ +#if !emscripten +@:cppFileCode(' +#include +#include +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>; + static function allocate():Void { + arrays = []; + for (i in 0...2048) { + var values:Array = []; + 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); + } +} diff --git a/test/regression/GcCurrentMemory/build.hxml b/test/regression/GcCurrentMemory/build.hxml new file mode 100644 index 000000000..b484e67e7 --- /dev/null +++ b/test/regression/GcCurrentMemory/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/GcCurrentMemory/stdout.txt b/test/regression/GcCurrentMemory/stdout.txt new file mode 100644 index 000000000..60128b382 --- /dev/null +++ b/test/regression/GcCurrentMemory/stdout.txt @@ -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 diff --git a/test/regression/IntArrayJoin/Main.hx b/test/regression/IntArrayJoin/Main.hx new file mode 100644 index 000000000..176d944e9 --- /dev/null +++ b/test/regression/IntArrayJoin/Main.hx @@ -0,0 +1,27 @@ +// An Array 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, 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 = [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).join(",") + "]"); + Sys.println("printed: " + Std.string([-3, 0, 12]) + " " + [[1, -2], [30]]); + } +} diff --git a/test/regression/IntArrayJoin/build.hxml b/test/regression/IntArrayJoin/build.hxml new file mode 100644 index 000000000..b484e67e7 --- /dev/null +++ b/test/regression/IntArrayJoin/build.hxml @@ -0,0 +1,2 @@ +-m Main +--cpp bin diff --git a/test/regression/IntArrayJoin/stdout.txt b/test/regression/IntArrayJoin/stdout.txt new file mode 100644 index 000000000..b4ad41ca5 --- /dev/null +++ b/test/regression/IntArrayJoin/stdout.txt @@ -0,0 +1,3 @@ +joined integers match their strings: true +one: 42, none: [] +printed: [-3,0,12] [[1,-2],[30]]