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

Performance improvement and easy of use. #36

Open
alexlee002 opened this issue Jul 7, 2018 · 5 comments
Open

Performance improvement and easy of use. #36

alexlee002 opened this issue Jul 7, 2018 · 5 comments

Comments

@alexlee002
Copy link

Hi, I read the doc and source code, Hydrant is a good design framework! It's a good model to programmers to learn how to design a program.

But I also found some small issues:

  • It's not very convenient to developers to use. (Here is another JSON mapping framework that it's easy to use: YYModel). If the name of object's property is equals to the JSON's keyword, the framework will automatically mapping it, AND, most of the type (eg: NSString, cstring, Number, int, float, double, NSData, char *, NSDate, NSURL etc) will be automatically transformed.
  • The KVC mapping performance: Yes, KVC is the most compatible transformer, but it waste lots of time in message forward. could you consider use objc_msgSend or IMP directly? and cache some most used mapper?
@jeffh
Copy link
Owner

jeffh commented Jul 7, 2018 via email

@alexlee002
Copy link
Author

Thanks JeffH,

Here is my test:

@interface HYDObj1: NSObject
@property(nonatomic, assign)    NSInteger   intVal;
@property(nonatomic, assign)    float       floatVal;
@property(nonatomic, copy)      NSString    *str;
@property(nonatomic, strong)    NSURL       *url;
@property(nonatomic, strong)    NSDate      *date;
@end

    NSDictionary *json = @{
                            @"int_val": @(123), @"float_val": @(12.34), @"str": @"this is a string",
                            @"url": @"http://www.baidu.com", @"date": @"2018-07-05 00:00:00"
                            };
    
    CFTimeInterval t = CFAbsoluteTimeGetCurrent();
    for (int i = 0; i < 10000; ++i) {
        [HYDObj1 yy_modelWithJSON:json];
    }
    NSLog(@"YYModel: %.4f", CFAbsoluteTimeGetCurrent() - t);
    
    
    id<HYDMapper> mapper = HYDMapReflectively(HYDObj1.class).withNoRequiredFields.customMapping(@{@"int_val": @"intVal", @"float_val": @"floatVal"});
    HYDError *error = nil;
    t = CFAbsoluteTimeGetCurrent();
    for (int i = 0; i < 10000; ++i) {
        [mapper objectFromSourceObject:json error:&error];
    }
    NSLog(@"Hydrant: %.4f", CFAbsoluteTimeGetCurrent() - t);

the result is:

HydrantTest[48230:2407977] YYModel: 0.5854
HydrantTest[48023:2391766] Hydrant: 71.5280

It seems the performance is an issue?

@jeffh
Copy link
Owner

jeffh commented Jul 12, 2018 via email

@alexlee002
Copy link
Author

alexlee002 commented Aug 30, 2018

Thanks @jeffh.

These days I try to use Hydrant in my project. There are two problems I don't know how to solve:

Problem 1:

If there is a data struct like a tree, how to define the mapper?

eg:

@interface MyFileMeta : NSObject
@property(nonatomic, copy) NSString *basePath;
@property(nonatomic, copy) NSString *name;
@property(nonatomic, copy, nullable) NSArray< MyFileMeta  *> *children;
@end

how to define a mapper that can support uncertain depth of path tree?


Problem 2:

How to support if else branch in a mapper?

eg:

typedef NS_ENUM(NSInteger, MyProductType) {
    MyProductTypeVedio = 1,
    MyProductTypeImage,
};

@interface MyProduct : NSObject
@property(nonatomic,  copy) NSString *name;
@property(nonatomic)            double      price;
@property(nonatomic)            MyProductType type;
// ...
@property(nonatomic)            MyProductAttributes *attributes;
@end

@interface MyProductAttributes : NSObject
@property(nonatomic, copy)  NSString   *author;
// ...
@end

@interface MyVedioAttributes :  MyProductAttributes
@property(nonatomic)              NSTimeInterval   duration;
//...
@end

@interface MyImageAttributes: MyProductAttributes
@property(nonatomic)              long long fileSize;
@property(nonatomic)              long long width;
@property(nonatomic)              long long height;
// ...
@end

and here is a JSON:

[
    {"name": "move 1", "price": 111.11, "type":1,  "attributes": {"duration": "2:00:00"}}, 
    {"name": "pic1", "price": "200.00", "type":2,  "attributes": {
        "size": "10.0MB", "width": 1024, "height": 768
    }}
]

the type of "attribute" is base on the value of "type", so how to define the mapper?

Thanks a lot!

@alexlee002
Copy link
Author

Maybe it can be solve via HYDMapReflectively? I'm not sure, I try it tomorrow.

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

No branches or pull requests

2 participants