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

Minimize generated JSON size #1907

Open
byorgey opened this issue Jun 6, 2024 · 6 comments
Open

Minimize generated JSON size #1907

byorgey opened this issue Jun 6, 2024 · 6 comments
Assignees
Labels
C-Project A larger project, more suitable for experienced contributors. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. Z-Performance This issue concerns memory or time efficiency of the Swarm game. Z-Refactoring This issue is about restructuring the code without changing the behaviour to improve code quality.

Comments

@byorgey
Copy link
Member

byorgey commented Jun 6, 2024

As an experiment towards #50 (see also #347), I started up a creative scenario, loaded some code from a .sw file, ran a base program, and then serialized a snapshot of the base's CESK machine to a file. Unfortunately it was almost 3MB in size! However, most of it was unnecessary junk such as empty comments records. If we want saving games to be practical we will need to work a lot harder to minimize the size of generated JSON.

As one simple example, if a Comments record is empty we should be able to omit it entirely from the output. I'm sure there is lots more similar low-hanging fruit.

@byorgey byorgey added Z-Refactoring This issue is about restructuring the code without changing the behaviour to improve code quality. C-Low Hanging Fruit Ideal issue for new contributors. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. Z-Performance This issue concerns memory or time efficiency of the Swarm game. labels Jun 6, 2024
@byorgey byorgey changed the title Minimizegenerated JSON size Minimize generated JSON size Jun 6, 2024
@byorgey byorgey self-assigned this Jun 6, 2024
@byorgey
Copy link
Member Author

byorgey commented Jun 6, 2024

I suspect there is a lot of bloat from multiple closures containing Envs that contain duplicate bindings. In memory, there is a lot of sharing going on, but we lose the sharing when we serialize.

mergify bot pushed a commit that referenced this issue Jun 8, 2024
Towards #1907.  For the most part, eschew the default To/FromJSON deriving in favor of `sumEncoding = ObjectWithSingleField`, which encodes `Constructor val` as something like `Constructor: val` instead of `{tag: Constructor, contents: val}`.  Also omit empty `Comments` records.

In the context of #1907, this reduced the size of the generated JSON by about half.
@byorgey
Copy link
Member Author

byorgey commented Jun 12, 2024

Another bit of low-hanging fruit is to make custom instances for Type and Term/Syntax where ToJSON pretty-prints to a string and FromJSON runs the parser.

@byorgey byorgey added C-Project A larger project, more suitable for experienced contributors. and removed C-Low Hanging Fruit Ideal issue for new contributors. labels Jun 15, 2024
@xsebek
Copy link
Member

xsebek commented Jun 19, 2024

@byorgey, how about splitting low-hanging fruits from this one? 🙂

@byorgey
Copy link
Member Author

byorgey commented Jun 19, 2024

@byorgey, how about splitting low-hanging fruits from this one? 🙂

Well, to be honest, I'm not sure what they are. I already addressed the one example I mentioned.

@xsebek
Copy link
Member

xsebek commented Nov 22, 2024

@byorgey we could split off some improvements (numbers are for Blank scenario base):

  • empty comments:
    _sComments:
      _afterComments: []
      _beforeComments: []
  • empty location:
    _sLoc: NoLoc
  • capabilities include the complete description of the entity that provides it repeatedly (8x calculators):
    - - charat
      - - device:
            capabilities:
              getMap:
                - - charat
                  - ingredients: []
                - - tochar
                  - ingredients: []
            description: |-
              A handy lookup table for converting from characters to numeric codes and back, shaped for some reason into a ring.  When equipped, it enables two functions:
    
              `charat : Int -> Text -> Int` returns the numeric code of the character at a specific index in a (0-indexed) `Text` value.
    
              `tochar : Int -> Text` creates a singleton (length-1) `Text` value containing a character with the given numeric code.
            display:
              attr: silver
              char: O
              priority: 1
            name: decoder ring
            properties:
              - pickable
            tags: []
          useCost:
            ingredients: []
  • empty ingredients (399x):
    capabilities:
      getMap:
        - - neg
          - ingredients: []
  • empty tags (111x):
    tags: []
  • default priority (111x):
    priority: 1
    

@byorgey
Copy link
Member Author

byorgey commented Nov 23, 2024

@xsebek Good finds! Those would all indeed make for nice improvements. Some of them can probably be addressed with better options given to the generic FromJSON/ToJSON instance derivations. Some might require more manual intervention.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-Project A larger project, more suitable for experienced contributors. S-Nice to have The bug fix or feature would be nice but doesn't currently have much negative impact. Z-Performance This issue concerns memory or time efficiency of the Swarm game. Z-Refactoring This issue is about restructuring the code without changing the behaviour to improve code quality.
Projects
None yet
Development

No branches or pull requests

2 participants