Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment with fast json stringify #67

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 36 additions & 19 deletions packages/tests/src/benchmark/Benchmark.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -114,37 +114,54 @@ S$RescriptSchema.serializeWith(data, schema);

console.timeEnd("s: 3");

run(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(new (Benchmark.default.Suite)(), "Parse string", (function () {
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith("Hello world!", S$RescriptSchema.string);
};
})), "Serialize string", (function () {
return function () {
return S$RescriptSchema.serializeOrRaiseWith("Hello world!", S$RescriptSchema.string);
};
})).add("Advanced object schema factory", makeAdvancedObjectSchema), "Parse advanced object", (function () {
run(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(addWithPrepare(new (Benchmark.default.Suite)(), "Parse string", (function () {
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith("Hello world!", S$RescriptSchema.string);
};
})), "Serialize string", (function () {
return function () {
return S$RescriptSchema.serializeOrRaiseWith("Hello world!", S$RescriptSchema.string);
};
})).add("Advanced object schema factory", makeAdvancedObjectSchema), "Parse advanced object", (function () {
var schema = makeAdvancedObjectSchema();
var data = makeTestObject();
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
};
})), "Create and parse advanced object", (function () {
var data = makeTestObject();
return function () {
var schema = makeAdvancedObjectSchema();
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
};
})), "Parse advanced strict object", (function () {
var schema = makeAdvancedStrictObjectSchema();
var data = makeTestObject();
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
};
})), "Serialize advanced object", (function () {
var schema = makeAdvancedObjectSchema();
var data = makeTestObject();
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
return S$RescriptSchema.serializeOrRaiseWith(data, schema);
};
})), "Create and parse advanced object", (function () {
})), "Stringify with JSON.stringify", (function () {
var data = makeTestObject();
return function () {
var schema = makeAdvancedObjectSchema();
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
return JSON.stringify(data);
};
})), "Parse advanced strict object", (function () {
var schema = makeAdvancedStrictObjectSchema();
})), "Stringify with S.serializeToJsonStringWith", (function () {
var data = makeTestObject();
var schema = makeAdvancedObjectSchema();
return function () {
return S$RescriptSchema.parseAnyOrRaiseWith(data, schema);
return S$RescriptSchema.serializeToJsonStringWith(data, schema, undefined);
};
})), "Serialize advanced object", (function () {
var schema = makeAdvancedObjectSchema();
})), "Stringify with S.jsonString", (function () {
var data = makeTestObject();
var schema = S$RescriptSchema.jsonString(makeAdvancedObjectSchema(), undefined);
return function () {
return S$RescriptSchema.serializeOrRaiseWith(data, schema);
return S$RescriptSchema.serializeToUnknownWith(data, schema);
};
})));

Expand Down
23 changes: 23 additions & 0 deletions packages/tests/src/benchmark/Benchmark.res
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,27 @@ Suite.make()
data->S.serializeOrRaiseWith(schema)
}
})
// V6.2 x 279,905 ops/sec
->Suite.addWithPrepare("Stringify with JSON.stringify", () => {
let data = makeTestObject()
() => {
data->Js.Json.stringifyAny
}
})
// V6.2 x 278,250 ops/sec
->Suite.addWithPrepare("Stringify with S.serializeToJsonStringWith", () => {
let data = makeTestObject()
let schema = makeAdvancedObjectSchema()
() => {
data->S.serializeToJsonStringWith(schema)
}
})
// V6.2 x 277,401 ops/sec
->Suite.addWithPrepare("Stringify with S.jsonString", () => {
let data = makeTestObject()
let schema = S.jsonString(makeAdvancedObjectSchema())
() => {
data->S.serializeToUnknownWith(schema)
}
})
->Suite.run
34 changes: 32 additions & 2 deletions packages/tests/src/core/S_jsonString_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test("Successfully parses JSON", t => {
t->Assert.deepEqual(`"Foo"`->S.parseAnyWith(S.jsonString(schema)), Ok("Foo"), ())
})

test("Successfully serializes JSON", t => {
test("Successfully serializes string to JSON", t => {
let schema = S.string

t->Assert.deepEqual(
Expand All @@ -17,6 +17,36 @@ test("Successfully serializes JSON", t => {
)
})

test("Successfully serializes string literal to JSON", t => {
let schema = S.literal("foo")

t->Assert.deepEqual(
`foo`->S.serializeToUnknownWith(S.jsonString(schema)),
Ok(%raw(`'"foo"'`)),
(),
)
})

test("Successfully serializes float to JSON", t => {
let schema = S.float

t->Assert.deepEqual(
123.4->S.serializeToUnknownWith(S.jsonString(schema)),
Ok(%raw(`'123.4'`)),
(),
)
})

test("Successfully serializes tuple", t => {
let schema = S.tuple2(S.int, S.string)

t->Assert.deepEqual(
(12, "foo")->S.serializeToUnknownWith(S.jsonString(schema)),
Ok(%raw(`'[12,"foo"]'`)),
(),
)
})

test("Successfully serializes JSON object", t => {
let schema = S.schema(_ =>
{
Expand Down Expand Up @@ -88,7 +118,7 @@ test("Compiled async parse code snapshot", t => {
test("Compiled serialize code snapshot", t => {
let schema = S.jsonString(S.bool)

t->U.assertCompiledCode(~schema, ~op=#serialize, `i=>{return JSON.stringify(i)}`)
t->U.assertCompiledCode(~schema, ~op=#serialize, `i=>{return i.toString()}`)
})

test("Compiled serialize code snapshot with space", t => {
Expand Down
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_literal_Array_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ module Common = {
t->U.assertCompiledCode(
~schema,
~op=#parse,
`i=>{(i===e[0]||Array.isArray(i)&&i.length===2&&i[0]===e[1]&&i[1]===e[2])||e[3](i);return i}`,
`i=>{(i===e[0]||Array.isArray(i)&&i.length===2&&i[0]===e[1]&&i[1]===true)||e[2](i);return i}`,
)
})

Expand All @@ -109,7 +109,7 @@ module Common = {
t->U.assertCompiledCode(
~schema,
~op=#serialize,
`i=>{(i===e[0]||Array.isArray(i)&&i.length===2&&i[0]===e[1]&&i[1]===e[2])||e[3](i);return i}`,
`i=>{(i===e[0]||Array.isArray(i)&&i.length===2&&i[0]===e[1]&&i[1]===true)||e[2](i);return i}`,
)
})
}
4 changes: 2 additions & 2 deletions packages/tests/src/core/S_literal_Boolean_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ module Common = {
test("Compiled parse code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#parse, `i=>{i===e[0]||e[1](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#parse, `i=>{i===false||e[0](i);return i}`)
})

test("Compiled serialize code snapshot", t => {
let schema = factory()

t->U.assertCompiledCode(~schema, ~op=#serialize, `i=>{i===e[0]||e[1](i);return i}`)
t->U.assertCompiledCode(~schema, ~op=#serialize, `i=>{i===false||e[0](i);return i}`)
})
}
1 change: 1 addition & 0 deletions packages/tests/src/utils/U.bs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function cleanUpSchema(schema) {
switch (key) {
case "f" :
case "i" :
case "j" :
case "n" :
case "op" :
case "opa" :
Expand Down
2 changes: 1 addition & 1 deletion packages/tests/src/utils/U.res
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let rec cleanUpSchema = schema => {
->Dict.toArray
->Array.forEach(((key, value)) => {
switch key {
| "s" | "p" | "i" | "f" | "n" | "os" | "op" | "opa" => ()
| "s" | "p" | "i" | "f" | "n" | "os" | "op" | "opa" | "j" => ()
| _ =>
if typeof(value) === #object && value !== %raw(`null`) {
new->Dict.set(
Expand Down
Loading
Loading