react-native-ble-mesh 2.0.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +2 -2
  2. package/docs/OPTIMIZATION.md +165 -52
  3. package/package.json +1 -1
  4. package/src/MeshNetwork.js +16 -8
  5. package/src/crypto/AutoCrypto.js +14 -3
  6. package/src/crypto/providers/ExpoCryptoProvider.js +3 -2
  7. package/src/crypto/providers/QuickCryptoProvider.js +30 -6
  8. package/src/crypto/providers/TweetNaClProvider.js +1 -1
  9. package/src/hooks/AppStateManager.js +9 -1
  10. package/src/hooks/useMesh.js +17 -4
  11. package/src/hooks/useMessages.js +4 -4
  12. package/src/hooks/usePeers.js +13 -9
  13. package/src/mesh/dedup/BloomFilter.js +44 -57
  14. package/src/mesh/dedup/DedupManager.js +32 -1
  15. package/src/mesh/fragment/Fragmenter.js +1 -1
  16. package/src/mesh/monitor/ConnectionQuality.js +42 -17
  17. package/src/mesh/monitor/NetworkMonitor.js +58 -13
  18. package/src/mesh/peer/Peer.js +5 -2
  19. package/src/mesh/peer/PeerManager.js +15 -3
  20. package/src/mesh/router/MessageRouter.js +14 -6
  21. package/src/mesh/router/RouteTable.js +17 -7
  22. package/src/mesh/store/StoreAndForwardManager.js +13 -2
  23. package/src/protocol/deserializer.js +9 -10
  24. package/src/protocol/header.js +13 -7
  25. package/src/protocol/message.js +15 -3
  26. package/src/protocol/serializer.js +7 -10
  27. package/src/protocol/validator.js +23 -5
  28. package/src/service/BatteryOptimizer.js +4 -1
  29. package/src/service/HandshakeManager.js +12 -16
  30. package/src/service/SessionManager.js +12 -10
  31. package/src/service/text/TextManager.js +21 -15
  32. package/src/storage/MessageStore.js +55 -2
  33. package/src/transport/BLETransport.js +11 -2
  34. package/src/transport/MultiTransport.js +35 -10
  35. package/src/transport/WiFiDirectTransport.js +4 -3
  36. package/src/utils/EventEmitter.js +6 -9
  37. package/src/utils/bytes.js +12 -10
  38. package/src/utils/compression.js +5 -3
  39. package/src/utils/encoding.js +33 -8
@@ -57,6 +57,9 @@ class MessageCompressor {
57
57
  bytesIn: 0,
58
58
  bytesOut: 0
59
59
  };
60
+
61
+ // Pre-allocate hash table to avoid per-call allocation
62
+ this._hashTable = new Int32Array(this._config.hashTableSize);
60
63
  }
61
64
 
62
65
  /**
@@ -177,9 +180,8 @@ class MessageCompressor {
177
180
  output[outputPos++] = (inputLen >> 16) & 0xff;
178
181
  output[outputPos++] = (inputLen >> 24) & 0xff;
179
182
 
180
- // Hash table for finding matches
181
- // Using Knuth's multiplicative hash constant (2654435761) for good distribution
182
- const hashTable = new Int32Array(this._config.hashTableSize);
183
+ // Reuse pre-allocated hash table, reset for this call
184
+ const hashTable = this._hashTable;
183
185
  hashTable.fill(-1);
184
186
 
185
187
  let anchor = 0;
@@ -14,18 +14,41 @@ for (let i = 0; i < 16; i++) {
14
14
  HEX_LOOKUP[HEX_CHARS.toUpperCase().charCodeAt(i)] = i;
15
15
  }
16
16
 
17
+ // Pre-computed byte-to-hex lookup table (avoids per-byte toString + padStart)
18
+ const HEX_TABLE = new Array(256);
19
+ for (let i = 0; i < 256; i++) {
20
+ HEX_TABLE[i] = HEX_CHARS[i >> 4] + HEX_CHARS[i & 0x0f];
21
+ }
22
+
23
+ // Cached TextEncoder/TextDecoder singletons (avoid per-call allocation)
24
+ let _cachedEncoder = null;
25
+ let _cachedDecoder = null;
26
+
27
+ function _getEncoder() {
28
+ if (!_cachedEncoder && typeof TextEncoder !== 'undefined') {
29
+ _cachedEncoder = new TextEncoder();
30
+ }
31
+ return _cachedEncoder;
32
+ }
33
+
34
+ function _getDecoder() {
35
+ if (!_cachedDecoder && typeof TextDecoder !== 'undefined') {
36
+ _cachedDecoder = new TextDecoder();
37
+ }
38
+ return _cachedDecoder;
39
+ }
40
+
17
41
  /**
18
42
  * Converts a byte array to a hexadecimal string
19
43
  * @param {Uint8Array} bytes - Bytes to convert
20
44
  * @returns {string} Hexadecimal string
21
45
  */
22
46
  function bytesToHex(bytes) {
23
- let result = '';
47
+ const parts = new Array(bytes.length);
24
48
  for (let i = 0; i < bytes.length; i++) {
25
- result += HEX_CHARS[bytes[i] >> 4];
26
- result += HEX_CHARS[bytes[i] & 0x0f];
49
+ parts[i] = HEX_TABLE[bytes[i]];
27
50
  }
28
- return result;
51
+ return parts.join('');
29
52
  }
30
53
 
31
54
  /**
@@ -62,8 +85,9 @@ function hexToBytes(hex) {
62
85
  * @returns {Uint8Array} UTF-8 encoded bytes
63
86
  */
64
87
  function stringToBytes(str) {
65
- if (typeof TextEncoder !== 'undefined') {
66
- return new TextEncoder().encode(str);
88
+ const encoder = _getEncoder();
89
+ if (encoder) {
90
+ return encoder.encode(str);
67
91
  }
68
92
 
69
93
  // Fallback for environments without TextEncoder
@@ -99,8 +123,9 @@ function stringToBytes(str) {
99
123
  * @returns {string} Decoded string
100
124
  */
101
125
  function bytesToString(bytes) {
102
- if (typeof TextDecoder !== 'undefined') {
103
- return new TextDecoder().decode(bytes);
126
+ const decoder = _getDecoder();
127
+ if (decoder) {
128
+ return decoder.decode(bytes);
104
129
  }
105
130
 
106
131
  // Fallback for environments without TextDecoder