Skip to content

Commit

Permalink
ppx: Investigate variants with payload
Browse files Browse the repository at this point in the history
  • Loading branch information
DZakh committed Oct 18, 2023
1 parent 65862f1 commit 875116d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
27 changes: 17 additions & 10 deletions IDEAS.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,30 @@ let trimContract: S.contract<string => string> = S.contract(s => {

## v5.2

- S.matcher
- ppx

## v5.3

- stop reallocate objects without transformations
- S.matcher
- Add `~space` to `S.jsonString` ?
- Make S.serializeToString super fast
- Add serialiseToString to js api
- Make operations more treeshakable by starting passing the actual operation to the initialOperation function. Or add a condition (verify performance)
- S.validateWith
- S.toJSON/S.castToJson ???
- nestedField for object
- s.spread for object
- S.produce
- S.mutator

## v6

- `S.json` -> `S.json(~unsafe: bool)` to improve tree-shaking
- Add `~space` to `S.jsonString` ?
- Make S.serializeToString super fast
- Make operations more treeshakable by starting passing the actual operation to the initialOperation function. Or add a condition (verify performance)
- Remove `s.failWithError` since there's `Error.raise` 🤔
- Remove `S.classify` & `S.name` in favor of fields on `t` ???
- Make `error.reason` tree-shakeable
- Update `Literal` `tagged` to include `text`, `value` and `kind`. So it's more convinient and smaller bundle-size
- Turn `String.email` -> `email`, `String.min` -> `stringMin` for tree-shaking

## v???

- S.toJSON/S.castToJson ???
- nestedField for object
- s.spread for object
- S.produce
- S.mutator
6 changes: 4 additions & 2 deletions packages/ppx/src/ppx/variants.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ open Parsetree
open Ast_helper
open Utils

let parse_decl { pcd_name = { txt = name } } =
[%expr S.literal [%e Exp.construct (lid name) None]]
let parse_decl { pcd_name = { txt = name; loc }; pcd_args } =
match pcd_args with
| Pcstr_tuple [] -> [%expr S.literal [%e Exp.construct (lid name) None]]
| _ -> fail loc "Variants with payload not supported yet"

let generate_struct_expr constr_decls =
let union_items = List.map parse_decl constr_decls in
Expand Down
10 changes: 9 additions & 1 deletion packages/tests/src/ppx/Ppx_Variants_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ test("Variant with partial @as usage", t => {
})

// TODO: Support
// type variantWithPayloads = One | Two(int) | Three(bool)
// @struct
// type variantWithPayloads = Constant | SinglePayload(int)
// test("Variant with tuple payloads", t => {
// t->assertEqualStructs(
// variantWithPayloadsStruct,
// S.union([S.literal(Constant), S.unknown->Obj.magic]),
// )
// })

// TODO: Support @unboxed
// TODO: Support @tag

2 comments on commit 875116d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 875116d Previous: 80ac4f1 Ratio
Parse string 585898635 ops/sec (±0.98%) 706149725 ops/sec (±0.41%) 1.21
Serialize string 591412957 ops/sec (±0.07%) 693539015 ops/sec (±2.10%) 1.17
Advanced object struct factory 282148 ops/sec (±0.51%) 286556 ops/sec (±0.58%) 1.02
Parse advanced object 31712558 ops/sec (±0.26%) 22341504 ops/sec (±0.24%) 0.70
Create and parse advanced object 24672 ops/sec (±0.63%) 58042 ops/sec (±1.08%) 2.35
Parse advanced strict object 15226826 ops/sec (±0.18%) 13141235 ops/sec (±0.73%) 0.86
Serialize advanced object 578190698 ops/sec (±0.10%) 32617508 ops/sec (±0.33%) 0.056413062529068914

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 875116d Previous: 80ac4f1 Ratio
Create and parse advanced object 24672 ops/sec (±0.63%) 58042 ops/sec (±1.08%) 2.35

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.