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

Optimize serialization #132

Merged
merged 7 commits into from
Oct 25, 2024
Merged

Optimize serialization #132

merged 7 commits into from
Oct 25, 2024

Conversation

sisyphusSmiling
Copy link
Contributor

@sisyphusSmiling sisyphusSmiling commented Oct 23, 2024

Related: #80

Description

  • Updates Serialize.cdc & SerializeMetadata.cdc by replacing .concat with String.join <- credit to @bluesign for the suggestion
  • Removes previewnet from flow.json

For contributor use:

  • Targeted PR against main branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@codecov-commenter
Copy link

codecov-commenter commented Oct 23, 2024

Codecov Report

Attention: Patch coverage is 95.45455% with 3 lines in your changes missing coverage. Please review.

Project coverage is 86.73%. Comparing base (02aab3f) to head (fae3977).
Report is 156 commits behind head on main.

Files with missing lines Patch % Lines
cadence/contracts/utils/SerializeMetadata.cdc 93.33% 2 Missing ⚠️
cadence/contracts/utils/Serialize.cdc 97.22% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #132      +/-   ##
==========================================
+ Coverage   86.19%   86.73%   +0.53%     
==========================================
  Files          18       18              
  Lines         884      965      +81     
==========================================
+ Hits          762      837      +75     
- Misses        122      128       +6     

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

@sisyphusSmiling sisyphusSmiling marked this pull request as ready for review October 23, 2024 22:18
@sisyphusSmiling sisyphusSmiling requested a review from a team as a code owner October 23, 2024 22:18
Copy link
Member

@joshuahannan joshuahannan left a comment

Choose a reason for hiding this comment

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

Looks fine to me, but I'm probably not the best one to approve this. Would be good to get Austin or Deniz to take a look too

@bluesign
Copy link

bluesign commented Oct 24, 2024

I think switch can be optimized with isInstance or better if let

fun tryToJSONString(_ value: AnyStruct): String? {
        // Recursively serialize array & return
        if value.getType().isSubtype(of: Type<[AnyStruct]>()) {
            return arrayToJSONString(value as! [AnyStruct])
        }
        // Recursively serialize map & return
        if value.getType().isSubtype(of: Type<{String: AnyStruct}>()) {
            return dictToJSONString(dict: value as! {String: AnyStruct}, excludedNames: nil)
        }
        
        if let v=value as? String {
          return String.join(["\"", v , "\"" ], separator: "")
        }
        if let v=value as? Number {
          return String.join(["\"", v.toString() , "\"" ], separator: "")
        }
        if let v=value as? Character {
          return String.join(["\"", v.toString() , "\"" ], separator: "")
        }
        if let v=value as? Bool {
          return String.join(["\"", v?"true":"false" , "\"" ], separator: "")
        }
        if let v=value as? Address {
          return String.join(["\"", v.toString() , "\"" ], separator: "")
        }
        

        if value==nil{
          return "\"nil\""
        }
        
        return nil
       
    }

@sisyphusSmiling
Copy link
Contributor Author

Thanks for the suggestions @bluesign. Some quick comparisons indicate there doesn't seem to be much of a difference between switch and if let and isInstance seems to be less efficient if I understood you suggestion correctly.

@bluesign
Copy link

bluesign commented Oct 24, 2024

Yeah they are pretty much same, but less code basically, I think switch is converting to if else technically but not sure

@sisyphusSmiling sisyphusSmiling merged commit d346978 into main Oct 25, 2024
2 checks passed
@sisyphusSmiling sisyphusSmiling deleted the gio/optimize_serialization branch October 25, 2024 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Status: ✅ Done
Development

Successfully merging this pull request may close these issues.

4 participants