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

enhance: Change Schema.normalize interface #3134

Merged
merged 9 commits into from
Jul 8, 2024
Merged

enhance: Change Schema.normalize interface #3134

merged 9 commits into from
Jul 8, 2024

Conversation

ntucker
Copy link
Collaborator

@ntucker ntucker commented Jun 23, 2024

Motivation

Schema methods should have limitations in args sent

  • Input data (value, parent, key, args)
  • Functions to operate on and interface with larger normalize/denormalize process
    • NOT direct access to entire data tree like entities

Recursive calls should not require re-passing functions

  • Functions can all be closed around those operations, so it makes it easier for schema implementers, makes the code easier to read, and possibly improves performance

Ordering of args should be as consistent as possible to help with readability

  • schema always first if it’s an arg
  • next is inputs, if they exist (value, parent, key, args)
  • next is any recursive functions (like visit, unvisit)
  • next is remaining context acting functions (like addEntity, getEntity, checkLoop)

Solution

class MemoCache {
  denormalize(schema, input, entities, args): { data, paths };
  query(schema, args, entities, indexes): data;
  buildQueryKey(schema, args, entities, indexes): normalized;
}
normalize(schema, input, { args, date, expiresAt, fetchedAt }, { entities, indexes, entityMeta });
denormalize(schema, input, entities, args);
interface SchemaSimple {
  normalize(
    input: any,
    parent: any,
    key: any,
    args: any[],
    visit: (schema: any, value: any, parent: any, key: any, args: readonly any[]) => any,
    addEntity: (...args: any) => any,
    getEntity: (...args: any) => any,
    checkLoop: (...args: any) => any,
  ): any;
  denormalize(
    input: {},
    args: readonly any[],
    unvisit: (schema: any, input: any) => any,
  ): T;
}
const { result, entities, indexes, entityMeta } = normalize(
  action.endpoint.schema,
  payload,
  action.meta,
  state,
);

Open questions

Args order: There are legit cases where it's nice to not specify args because there are none. This means we should place args last to give it a default. However, this makes ordering inconsistent with other functions like query which always has an args.

Copy link

changeset-bot bot commented Jun 23, 2024

🦋 Changeset detected

Latest commit: 4096ece

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages
Name Type
@data-client/normalizr Minor
@data-client/endpoint Minor
@data-client/graphql Minor
@data-client/rest Minor
@data-client/react Minor
@data-client/core Minor
@data-client/test Minor
@data-client/img Minor
@data-client/ssr Patch
example-benchmark Patch
normalizr-github-example Patch
normalizr-redux-example Patch
normalizr-relationships Patch
coinbase-lite Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

codecov bot commented Jun 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.79%. Comparing base (3c95531) to head (4096ece).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3134   +/-   ##
=======================================
  Coverage   98.79%   98.79%           
=======================================
  Files         127      126    -1     
  Lines        2243     2249    +6     
  Branches      455      460    +5     
=======================================
+ Hits         2216     2222    +6     
  Misses         16       16           
  Partials       11       11           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 4096ece Previous: 7bd322d Ratio
normalizeLong 503 ops/sec (±0.65%) 450 ops/sec (±1.81%) 0.89
infer All 9696 ops/sec (±0.47%) 9576 ops/sec (±1.57%) 0.99
denormalizeLong 342 ops/sec (±1.29%) 328 ops/sec (±2.51%) 0.96
denormalizeLong donotcache 889 ops/sec (±0.52%) 888 ops/sec (±1.56%) 1.00
denormalizeShort donotcache 500x 1355 ops/sec (±0.23%) 1351 ops/sec (±0.14%) 1.00
denormalizeShort 500x 973 ops/sec (±0.22%) 986 ops/sec (±0.26%) 1.01
denormalizeShort 500x withCache 4821 ops/sec (±0.18%) 4966 ops/sec (±0.33%) 1.03
denormalizeLong with mixin Entity 301 ops/sec (±0.27%) 302 ops/sec (±0.30%) 1.00
denormalizeLong withCache 7129 ops/sec (±0.17%) 7339 ops/sec (±0.24%) 1.03
denormalizeLong All withCache 6690 ops/sec (±0.30%) 6888 ops/sec (±0.17%) 1.03
denormalizeLong Query-sorted withCache 6612 ops/sec (±0.59%) 6540 ops/sec (±0.97%) 0.99
denormalizeLongAndShort withEntityCacheOnly 1488 ops/sec (±0.53%) 1571 ops/sec (±0.42%) 1.06
getResponse 5946 ops/sec (±1.09%) 6046 ops/sec (±1.32%) 1.02
getResponse (null) 5346945 ops/sec (±0.84%) 6178925 ops/sec (±0.96%) 1.16
getResponse (clear cache) 292 ops/sec (±0.26%) 297 ops/sec (±0.34%) 1.02
getSmallResponse 2593 ops/sec (±0.16%) 2683 ops/sec (±0.53%) 1.03
getSmallInferredResponse 2069 ops/sec (±0.24%) 2027 ops/sec (±0.61%) 0.98
getResponse Query-sorted 6553 ops/sec (±0.66%) 6579 ops/sec (±0.76%) 1.00
getResponse Collection 6308 ops/sec (±1.13%) 6281 ops/sec (±1.16%) 1.00
get Collection 5517 ops/sec (±1.01%) 5455 ops/sec (±1.11%) 0.99
setLong 507 ops/sec (±0.24%) 454 ops/sec (±1.98%) 0.90
setLongWithMerge 194 ops/sec (±0.47%) 188 ops/sec (±1.48%) 0.97
setLongWithSimpleMerge 205 ops/sec (±0.54%) 195 ops/sec (±1.35%) 0.95
setSmallResponse 500x 871 ops/sec (±0.51%) 831 ops/sec (±1.14%) 0.95

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

@ntucker ntucker force-pushed the normalize-args branch 13 times, most recently from 4a96d27 to c43db74 Compare June 27, 2024 12:39
@ntucker ntucker force-pushed the master branch 2 times, most recently from 157a636 to 151602e Compare July 1, 2024 20:37
@ntucker ntucker force-pushed the normalize-args branch 12 times, most recently from 8e25661 to 2f5d1d8 Compare July 3, 2024 10:38
@ntucker ntucker merged commit 2ad1811 into master Jul 8, 2024
26 checks passed
@ntucker ntucker deleted the normalize-args branch July 8, 2024 10:40
@github-actions github-actions bot mentioned this pull request Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant