diff --git a/uppsrc/Core/Core.h b/uppsrc/Core/Core.h index 3e7b646c0..e258c1368 100644 --- a/uppsrc/Core/Core.h +++ b/uppsrc/Core/Core.h @@ -366,6 +366,8 @@ class JsonIO; #include "Huge.h" +#include "ValueCache.h" + #ifdef PLATFORM_WIN32 NTL_MOVEABLE(POINT) NTL_MOVEABLE(SIZE) diff --git a/uppsrc/Core/Core.upp b/uppsrc/Core/Core.upp index 0a434dca4..d0a08b267 100644 --- a/uppsrc/Core/Core.upp +++ b/uppsrc/Core/Core.upp @@ -163,6 +163,8 @@ file Topic.cpp, CoWork.h, CoWork.cpp, + ValueCache.h, + ValueCache.cpp, Hash.h, MD5.cpp, SHA1.cpp, diff --git a/uppsrc/Core/Cpu.cpp b/uppsrc/Core/Cpu.cpp index fadbc8a6f..de5cc8a0c 100644 --- a/uppsrc/Core/Cpu.cpp +++ b/uppsrc/Core/Cpu.cpp @@ -155,21 +155,6 @@ void GetSystemMemoryStatus(uint64& total, uint64& available) return; } #endif -#ifdef PLATFORM_FREEBSD - int64 page_size; - struct vmtotal vmt; - size_t vmt_size, uint_size; - - vmt_size = sizeof(vmt); - uint_size = sizeof(page_size); - - if(sysctlbyname("vm.vmtotal", &vmt, &vmt_size, NULL, 0) >= 0 && - sysctlbyname("vm.stats.vm.v_page_size", &page_size, &uint_size, NULL, 0) >= 0) { - available = vmt.t_free * page_size; - total = vmt.t_avm * page_size; - return; - } -#endif #ifdef PLATFORM_MACOS mach_msg_type_number_t count = HOST_VM_INFO_COUNT; vm_statistics_data_t vmstat; @@ -191,6 +176,21 @@ void GetSystemMemoryStatus(uint64& total, uint64& available) total = physical_memory; return; } +#endif +#ifdef PLATFORM_FREEBSD + int64 page_size; + struct vmtotal vmt; + size_t vmt_size, uint_size; + + vmt_size = sizeof(vmt); + uint_size = sizeof(page_size); + + if(sysctlbyname("vm.vmtotal", &vmt, &vmt_size, NULL, 0) >= 0 && + sysctlbyname("vm.stats.vm.v_page_size", &page_size, &uint_size, NULL, 0) >= 0) { + available = vmt.t_free * page_size; + total = vmt.t_avm * page_size; + return; + } #endif total = 256*1024*1024; available = 16*1024*1024; diff --git a/uppsrc/Core/Other.h b/uppsrc/Core/Other.h index 4e0600dc3..eba9d8a33 100644 --- a/uppsrc/Core/Other.h +++ b/uppsrc/Core/Other.h @@ -349,10 +349,10 @@ private: struct Key : Moveable { K key; - const void *type; + String type; bool operator==(const Key& b) const { return key == b.key && type == b.type; } - hash_t GetHashValue() const { return CombineHash(key, (uintptr_t)type); } + hash_t GetHashValue() const { return CombineHash(key, type); } }; Index key; @@ -365,7 +365,8 @@ private: int foundsize; int newsize; bool flag; - + + const int InternalSize = 3 * (sizeof(Item) + sizeof(Key) + 24) / 2; void Unlink(int i); void LinkHead(int i); @@ -464,7 +465,7 @@ void LRUCache::AdjustSize(P getsize) if(!key.IsUnlinked(i)) { int sz = getsize(*data[i].data); if(sz >= 0) - data[i].size = sz; + data[i].size = sz + InternalSize; size += data[i].size; count++; } @@ -540,12 +541,12 @@ T& LRUCache::Get(const Maker& m) { Key k; k.key = m.Key(); - k.type = &typeid(m); + k.type = typeid(m).name(); int q = key.Find(k); if(q < 0) { q = key.Put(k); Item& t = data.At(q); - t.size = m.Make(t.data.Create()); + t.size = m.Make(t.data.Create()) + InternalSize; size += t.size; newsize += t.size; t.flag = flag; diff --git a/uppsrc/Core/ValueCache.cpp b/uppsrc/Core/ValueCache.cpp new file mode 100644 index 000000000..c4056d14d --- /dev/null +++ b/uppsrc/Core/ValueCache.cpp @@ -0,0 +1,37 @@ +#include "Core.h" + +namespace Upp { + +bool sFinished; + +struct ValueMakeCacheClass : LRUCache { + ~ValueMakeCacheClass() { sFinished = true; } +}; + +LRUCache& TheValueCache() +{ + static ValueMakeCacheClass m; + return m; +} + +int sMaxSize = 1000000; + +void ShrinkValueCache() +{ + TheValueCache().Shrink(sMaxSize, 2000); +} + +void ShrinkValueCache(int maxsize) +{ + sMaxSize = maxsize; + ShrinkValueCache(); +} + +Value MakeValue(ValueMaker& m) +{ + Value v = TheValueCache().Get(m); + ShrinkValueCache(); + return v; +} + +}; \ No newline at end of file diff --git a/uppsrc/Core/ValueCache.h b/uppsrc/Core/ValueCache.h new file mode 100644 index 000000000..41c060a40 --- /dev/null +++ b/uppsrc/Core/ValueCache.h @@ -0,0 +1,20 @@ +typedef LRUCache::Maker ValueMaker; + +LRUCache& TheValueCache(); + +Value MakeValue(ValueMaker& m); + +void ShrinkValueCache(); +void ShrinkValueCache(int maxsize); + +template +int ValueCacheRemove(P what) +{ + return TheValueCache().Remove(what); +} + +template +int ValueCacheRemoveOne(P what) +{ + return TheValueCache().Remove(what); +}