json-as 0.9.8-b → 0.9.9-a

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.
package/CHANGELOG CHANGED
@@ -15,10 +15,14 @@ v0.9.7 - Update testing framework and readme logo
15
15
  v0.9.8 - Update dependencies
16
16
  v0.9.8a - Fix #80 - Empty Maps were not serialized to {}, instead, threw memory out of bounds
17
17
  v0.9.8b - Fix #81 - Revert transform
18
- [UNRELEASED] v0.9.9
18
+ v0.9.9 - Fix #82 - Initialize maps
19
+ v0.9.9a - Remove extraneous logs from transform
20
+
21
+ [UNRELEASED] v1.0.0
19
22
  - Allow nullable primitives
20
23
  - Port over JSON.Value
21
24
  - Performance improvements
25
+ - Add SIMD support
22
26
  - Introduce options
23
27
  - Custom serializers
24
28
  - Support arbitrary-length integers (@hypercubed/as-mpz)
package/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  __| || __|| || | | ___ | _ || __|
4
4
  | | ||__ || | || | | ||___|| ||__ |
5
5
  |_____||_____||_____||_|___| |__|__||_____|
6
- v0.9.8b
6
+ v0.9.9a
7
7
  </pre>
8
8
  </h5>
9
9
 
@@ -187,16 +187,17 @@ Below are benchmark results comparing JavaScript's built-in JSON implementation
187
187
 
188
188
  My library beats JSON (written in C++) on all counts *and*, I see many places where I can pull at least a 60% uplift in performance if I implement it.
189
189
 
190
+ Note: SIMD is in-development and only available on the `v1` branch on GitHub
190
191
 
191
192
  Serialization Benchmarks:
192
193
 
193
- | Value | JavaScript (ops/s) | JSON-as (ops/s) | Difference |
194
- |----------------------------|--------------------|-----------------|------------|
195
- | "hello world" | 28,629,598 | 64,210,666 | + 124% |
196
- | 12345 | 31,562,431 | 56,329,066 | + 78% |
197
- | 1.2345 | 15,977,278 | 20,322,939 | + 27% |
198
- | [[],[[]],[[],[[]]]] | 8,998,624 | 34,453,102 | + 283% |
199
- | { x: f64, y: f64, z: f64 } | 15,583,686 | 17,604,821 | + 12% |
194
+ | Value | JavaScript (ops/s) | JSON-AS (ops/s) | JSON-AS (Pages) | JSON-AS (SIMD+Pages)| Max Throughput |
195
+ |----------------------------|--------------------|--------------------|---------------------|---------------------|----------------|
196
+ | "hello world" | 7,124,361 | 44,290,480 (6.2x) | 73,601,235 (10.3x) | NOT IMPLEMENTED | 1.91 GB/s |
197
+ | 12345 | 9,611,677 | 66,900,642 (6.9x) | 145,924,333 (15.2x) | NOT IMPLEMENTED | 0.58 GB/s |
198
+ | 1.2345 | 7,227,259 | 20,322,939 (2.8x) | NOT IMPLEMENTED | NOT IMPLEMENTED | 0.16 GB/s |
199
+ | [[],[[]],[[],[[]]]] | 5,655,429 | 34,453,102 (6.0x) | NOT IMPLEMENTED | NOT IMPLEMENTED | 1.32 GB/s |
200
+ | { x: f64, y: f64, z: f64 } | 3,878,604 | 44,557,996 (11.5x) | 113,203,242 (29.2x) | 172,023,231 (44.4x) | 8.61 GB/s |
200
201
 
201
202
 
202
203
 
@@ -72,6 +72,8 @@ import { deserializeFloat } from "./float";
72
72
  depth--;
73
73
  if (depth === 0) {
74
74
  ++objectValueIndex;
75
+ console.log("Index: " + nameof<indexof<T>>());
76
+ console.log("Value: " + nameof<valueof<T>>());
75
77
  map.set(deserializeMapKey<indexof<T>>(key), JSON.parse<valueof<T>>(data.slice(outerLoopIndex, objectValueIndex)));
76
78
  outerLoopIndex = objectValueIndex;
77
79
  isKey = false;
package/assembly/test.ts CHANGED
@@ -1,14 +1,78 @@
1
1
  import { JSON } from "json-as/assembly";
2
- import * as console from "as-console";
2
+
3
+ @json
4
+ class TokenMetaData {
5
+ id: u64;
6
+ name: string;
7
+ uri: string;
8
+
9
+ constructor(id: u64, name: string, uri: string) {
10
+ this.id = id;
11
+ this.name = name;
12
+ this.uri = uri;
13
+ }
14
+ }
15
+
3
16
  @json
4
- class Yo {
5
- map: Map<string, u64>;
17
+ class NonFungibleToken {
18
+ owner: string = "";
19
+ counter: u64 = 0;
20
+ tokens: Map<u64, TokenMetaData> = new Map<u64, TokenMetaData>();
21
+ owners: Map<u64, string> = new Map<u64, string>();
22
+ balances: Map<string, u64[]> = new Map<string, u64[]>();
23
+
24
+ constructor() { }
25
+
26
+ mint(name: string, uri: string, toAddress: string): u64 {
27
+ this.counter += 1;
28
+ const id = this.counter;
29
+
30
+ const tokenMetaData = new TokenMetaData(id, name, uri);
31
+
32
+ this.tokens.set(id, tokenMetaData);
33
+ this.owners.set(id, toAddress);
34
+
35
+ if (!this.balances.has(toAddress)) {
36
+ this.balances.set(toAddress, []);
37
+ }
6
38
 
7
- constructor() {
8
- this.map = new Map();
9
- }
39
+ this.balances.get(toAddress).push(id);
40
+
41
+ return id;
42
+ }
43
+ }
44
+ function readStringFromMemory(ptr: usize): string {
45
+ let len: i32 = load<u32>(ptr)
46
+ let buffer = new Uint8Array(len);
47
+
48
+ for (let i = 0; i < len; ++i) {
49
+ buffer[i] = load<u8>(ptr + 4 + i);
50
+ }
51
+
52
+ let s = String.UTF8.decode(buffer.buffer);
53
+ return s
54
+ }
55
+
56
+ function getLengthPrefixedString(s: string): ArrayBuffer {
57
+ let stringBuf = Uint8Array.wrap(String.UTF8.encode(s))
58
+ let newLen = stringBuf.byteLength
59
+ let buffer = new ArrayBuffer(4 + newLen);
60
+ let dataView = new DataView(buffer);
61
+
62
+ dataView.setUint32(0, newLen, true);
63
+
64
+ for (let i = 0; i < newLen; ++i) {
65
+ dataView.setInt8(4 + i, stringBuf[i])
66
+ }
67
+
68
+ return buffer
10
69
  }
11
70
 
12
- let y = new Yo();
13
- y.map.set("bhavya", 3000);
14
- console.log(JSON.stringify(y));
71
+ const s1 = getLengthPrefixedString("hello world");
72
+ console.log(Uint8Array.wrap(s1).join(" "));
73
+ const s2 = readStringFromMemory(changetype<usize>(s1));
74
+ console.log(s2);
75
+
76
+ let state = JSON.parse<NonFungibleToken>('{"owner":"","counter":1,"tokens":{"1":{"id":1,"name":"foo","uri":"bar"}},"owners":{"1":"baz"},"balances":{"baz":[1]}}');
77
+ state.mint("foo", "bar", "baz")
78
+ console.log(JSON.stringify(state))
Binary file