From a3602403c63d37692d1a3dc83087ace17680f981 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B5=D0=BB=D0=B8=D0=BF=D0=B5=D0=BD=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= Date: Sat, 10 Aug 2019 17:27:54 +0000 Subject: [PATCH] git-svn-id: svn://db.shs.com.ru/pip@840 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5 --- main.cpp | 57 +++++++++++++++++++++++++++++------ src_main/core/pibase.h | 41 +++++++++++++++++++++++++ src_main/core/pibytearray.cpp | 5 +++ src_main/core/pibytearray.h | 3 ++ src_main/core/piobject.cpp | 8 +---- src_main/core/piobject.h | 23 +++++++------- src_main/core/pistring.cpp | 6 ++++ src_main/core/pistring.h | 2 ++ 8 files changed, 117 insertions(+), 28 deletions(-) diff --git a/main.cpp b/main.cpp index 05b38edb..0e84caf2 100644 --- a/main.cpp +++ b/main.cpp @@ -28,20 +28,20 @@ PISystemMonitor sys_mon; class TestRec: public PIObject { PIOBJECT(TestRec) public: - EVENT_HANDLER(void, reinitSpec) {piCout << "reinitSpec";int a = 10; (void*)&a;} + EVENT_HANDLER(void, reinitSpec) {/*piCout << "reinitSpec";*/int a = 10; (void*)&a;} EVENT_HANDLER(void, coeffsChanged) {piCout << "coeffsChanged";} EVENT_HANDLER(void, guiAboutSend) {piCout << "guiAboutSend";} EVENT_HANDLER(void, guiAfterSend) {piCout << "guiAfterSend";} EVENT_HANDLER(void, guiReceived) {piCout << "guiReceived";} - EVENT_HANDLER(void, playerReceived) {piCout << "playerReceived";int a = 10; (void*)&a;} + EVENT_HANDLER(void, playerReceived) {/*piCout << "playerReceived";*/int a = 10; (void*)&a;} EVENT_HANDLER(void, playerValuesReceived) {piCout << "playerValuesReceived";} EVENT_HANDLER(void, calculate) {piCout << "calculate";} EVENT_HANDLER(void, autostart) {piCout << "autostart";} - EVENT_HANDLER(void, syncOff) {piCout << "syncOff";int a = 10; (void*)&a;} + EVENT_HANDLER(void, syncOff) {/*piCout << "syncOff";*/int a = 10; (void*)&a;} EVENT_HANDLER(void, del) {piCout << "DEL";} }; @@ -77,7 +77,12 @@ class CC: public CB { }; #define CONN(sn) CONNECTU(snd, sn, rec, sn) - +uint HashLy(const void* d, int l) { + uint hash = 0u; + for(int i = 0; i < l; ++i) + hash = (hash * 1664525u) + ((uchar*)d)[i] + 1013904223u; + return hash; +} int main(int argc, char * argv[]) { //sys_mon.startOnSelf(); @@ -106,7 +111,7 @@ int main(int argc, char * argv[]) { CONN(syncOff); // 0.6 us on call - /*for (int j = 0; j < 10; ++j) { + for (int j = 0; j < 10; ++j) { tm.reset(); for (int i = 0; i < 10000; ++i) { snd->reinitSpec(); @@ -115,11 +120,45 @@ int main(int argc, char * argv[]) { } double el = tm.elapsed_m(); piCout << el; - }*/ - piCout << "****"; - PIPeer().dump(); - piCout << "----"; + } + /*piCout << "****"; + PIPeer().dump(); + piCout << "----";*/ + +/* PIString _s = "012345678901234567890123456789"; + CRC_32 crc = standardCRC_32(); + PIByteArray _sd = _s.toByteArray(); + double el; + uint ret; + + tm.reset(); + for (int i = 0; i < 1000000; ++i) { + ret = crc.calculate(_sd.data(), _sd.size_s()); + } + el = tm.elapsed_m(); + piCout << ""; + piCout << ret; + piCout << "crc" << el; + + tm.reset(); + for (int i = 0; i < 1000000; ++i) { + ret = HashLy(_sd.data(), _sd.size_s()); + } + el = tm.elapsed_m(); + piCout << ""; + piCout << ret; + piCout << " ly" << el; + + tm.reset(); + for (int i = 0; i < 1000000; ++i) { + ret = murmur3_32((const uchar*)_sd.data(), _sd.size_s()); + } + el = tm.elapsed_m(); + piCout << ""; + piCout << ret; + piCout << "mur" << el; +*/ delete snd; delete rec; diff --git a/src_main/core/pibase.h b/src_main/core/pibase.h index 7d0c9500..9ebb3b2c 100644 --- a/src_main/core/pibase.h +++ b/src_main/core/pibase.h @@ -30,6 +30,7 @@ #include "piplatform.h" #include "pip_export.h" #include "pip_defs.h" +#include "string.h" //! Version of PIP in hex - 0x##(Major)##(Minor)##(Revision) #define PIP_VERSION ((PIP_VERSION_MAJOR << 16) | (PIP_VERSION_MINOR < 8) | PIP_VERSION_REVISION) @@ -459,6 +460,46 @@ uint letobe_i(uint v) {return (v >> 24) | ((v >> 8) & 0xFF00) | ((v << 8) & 0xFF #endif +/// \brief Generic hash function, impements murmur3/32 algorithm +inline uint piHash(const uchar * data, uint len, uint seed = 0) { + if (!data || len <= 0) return 0u; + uint h = seed; + if (len > 3) { + uint i = len >> 2; + do { + uint k; + memcpy(&k, data, sizeof(uint)); + data += sizeof(uint); + k *= 0xcc9e2d51; + k = (k << 15) | (k >> 17); + k *= 0x1b873593; + h ^= k; + h = (h << 13) | (h >> 19); + h = h * 5 + 0xe6546b64; + } while (--i); + } + if (len & 3) { + uint i = len & 3; + uint k = 0; + do { + k <<= 8; + k |= data[i - 1]; + } while (--i); + k *= 0xcc9e2d51; + k = (k << 15) | (k >> 17); + k *= 0x1b873593; + h ^= k; + } + h ^= len; + h ^= h >> 16; + h *= 0x85ebca6b; + h ^= h >> 13; + h *= 0xc2b2ae35; + h ^= h >> 16; + return h; +} + + #define piRoundf piRound #define piRoundd piRound #define piFloorf piFloor diff --git a/src_main/core/pibytearray.cpp b/src_main/core/pibytearray.cpp index b68a42b5..c8ccc751 100755 --- a/src_main/core/pibytearray.cpp +++ b/src_main/core/pibytearray.cpp @@ -277,6 +277,11 @@ uint PIByteArray::checksumPlain32() const { } +uint PIByteArray::hash() const { + return piHash(data(), size_s()); +} + + PIString PIByteArray::toString(int base) const { PIString ret; int sz = size_s(); diff --git a/src_main/core/pibytearray.h b/src_main/core/pibytearray.h index 7ad58147..9c022f84 100755 --- a/src_main/core/pibytearray.h +++ b/src_main/core/pibytearray.h @@ -119,6 +119,9 @@ public: //! Returns plain 32-bit checksum uint checksumPlain32() const; + //! Returns hash + uint hash() const; + void operator =(const PIDeque & d) {resize(d.size()); memcpy(data(), d.data(), d.size());} static PIByteArray fromUserInput(PIString str); diff --git a/src_main/core/piobject.cpp b/src_main/core/piobject.cpp index b31921f6..8aa326a6 100755 --- a/src_main/core/piobject.cpp +++ b/src_main/core/piobject.cpp @@ -430,12 +430,6 @@ CRC_32 & PIObject::__meta_crc() { } -uint PIObject::__string_id(const PIString & s) { - if (s.isEmpty()) return 0; - return __meta_crc().calculate(s.toByteArray()); -} - - void PIObject::callQueuedEvents() { mutex_queue.lock(); PIVector<__QueuedEvent> qe = events_queue; @@ -629,6 +623,6 @@ bool dumpApplicationToFile(const PIString & path) { void PIObject::__MetaData::addScope(const PIString & s) { if (!scope_list.contains(s)) { scope_list << s; - scope_id << __string_id(s); + scope_id << s.hash(); } } diff --git a/src_main/core/piobject.h b/src_main/core/piobject.h index c70bf33d..2cb83591 100755 --- a/src_main/core/piobject.h +++ b/src_main/core/piobject.h @@ -199,9 +199,9 @@ typedef name __PIObject__; \ public: \ static const PIString __classNameS() {return PIStringAscii(#name);} \ - static const uint __classNameIDS() {static uint ret = __string_id(PIStringAscii(#name)); return ret;} \ + static const uint __classNameIDS() {static uint ret = PIStringAscii(#name).hash(); return ret;} \ virtual const char * className() const {return #name;} \ - virtual const uint classNameID() const {static uint ret = __string_id(PIStringAscii(#name)); return ret;} \ + virtual const uint classNameID() const {static uint ret = PIStringAscii(#name).hash(); return ret;} \ private: \ virtual int ptrOffset() const {name * o = (name*)100; return int(llong((PIObject*)o) - llong(o));} \ class __BaseInitializer__ { \ @@ -437,11 +437,11 @@ #define EVENT_VHANDLER EVENT_VHANDLER0 -#define EVENT0(name) EVENT_HANDLER0(void, name) {static uint eid = __string_id(PIStringAscii(#name)); PIObject::raiseEvent(this, eid);} -#define EVENT1(name, a0, n0) EVENT_HANDLER1(void, name, a0, n0) {static uint eid = __string_id(PIStringAscii(#name)); PIObject::raiseEvent(this, eid, n0);} -#define EVENT2(name, a0, n0, a1, n1) EVENT_HANDLER2(void, name, a0, n0, a1, n1) {static uint eid = __string_id(PIStringAscii(#name)); PIObject::raiseEvent(this, eid, n0, n1);} -#define EVENT3(name, a0, n0, a1, n1, a2, n2) EVENT_HANDLER3(void, name, a0, n0, a1, n1, a2, n2) {static uint eid = __string_id(PIStringAscii(#name)); PIObject::raiseEvent(this, eid, n0, n1, n2);} -#define EVENT4(name, a0, n0, a1, n1, a2, n2, a3, n3) EVENT_HANDLER4(void, name, a0, n0, a1, n1, a2, n2, a3, n3) {static uint eid = __string_id(PIStringAscii(#name)); PIObject::raiseEvent(this, eid, n0, n1, n2, n3);} +#define EVENT0(name) EVENT_HANDLER0(void, name) {static uint eid = PIStringAscii(#name).hash(); PIObject::raiseEvent(this, eid);} +#define EVENT1(name, a0, n0) EVENT_HANDLER1(void, name, a0, n0) {static uint eid = PIStringAscii(#name).hash(); PIObject::raiseEvent(this, eid, n0);} +#define EVENT2(name, a0, n0, a1, n1) EVENT_HANDLER2(void, name, a0, n0, a1, n1) {static uint eid = PIStringAscii(#name).hash(); PIObject::raiseEvent(this, eid, n0, n1);} +#define EVENT3(name, a0, n0, a1, n1, a2, n2) EVENT_HANDLER3(void, name, a0, n0, a1, n1, a2, n2) {static uint eid = PIStringAscii(#name).hash(); PIObject::raiseEvent(this, eid, n0, n1, n2);} +#define EVENT4(name, a0, n0, a1, n1, a2, n2, a3, n3) EVENT_HANDLER4(void, name, a0, n0, a1, n1, a2, n2, a3, n3) {static uint eid = PIStringAscii(#name).hash(); PIObject::raiseEvent(this, eid, n0, n1, n2, n3);} #define EVENT EVENT0 #define RAISE_EVENT0(src, event) (src)->event(); @@ -510,10 +510,10 @@ public: //! Returns object class name virtual const char * className() const {return "PIObject";} - virtual const uint classNameID() const {static uint ret = __string_id(PIStringAscii("PIObject")); return ret;} + virtual const uint classNameID() const {static uint ret = PIStringAscii("PIObject").hash(); return ret;} static const PIString __classNameS() {return PIStringAscii("PIObject");} - static const uint __classNameIDS() {static uint ret = __string_id(PIStringAscii("PIObject")); return ret;} + static const uint __classNameIDS() {static uint ret = PIStringAscii("PIObject").hash(); return ret;} //! Returns parent object class name virtual const char * parentClassName() const {return "";} @@ -775,10 +775,9 @@ public: typedef PIPair __EHPair; static PIMutex & __meta_mutex(); - static PIMap & __meta_data(); // [__string_id(classname)]=__MetaData + static PIMap & __meta_data(); // [hash(classname)]=__MetaData static CRC_32 & __meta_crc(); - static uint __string_id(const PIString & s); //! \brief Execute all posted events from CONNECTU_QUEUED connections void callQueuedEvents(); @@ -814,7 +813,7 @@ private: slot = sl; signal = si; event = e; - eventID = __string_id(e); + eventID = e.hash(); dest_o = d_o; dest = d; args_count = ac; diff --git a/src_main/core/pistring.cpp b/src_main/core/pistring.cpp index 1da45031..2b47a08e 100755 --- a/src_main/core/pistring.cpp +++ b/src_main/core/pistring.cpp @@ -418,6 +418,12 @@ const char * PIString::dataAscii() const { } +uint PIString::hash() const { + buildData(); + return piHash(data_.data(), data_.size_s() - 1); +} + + PIByteArray PIString::toUTF8() const { if (isEmpty()) return data_.resized(0); buildData( diff --git a/src_main/core/pistring.h b/src_main/core/pistring.h index 7e1c2a0b..c6603f3b 100755 --- a/src_main/core/pistring.h +++ b/src_main/core/pistring.h @@ -470,6 +470,8 @@ public: * execution of this function.\n */ const char * dataAscii() const; + //! Returns hash + uint hash() const; //! \brief Return \a PIByteArray contains \a data() of this string without terminating null-char PIByteArray toByteArray() const {buildData(); return data_.resized(data_.size_s() - 1);}