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

Optimization: Don't call -[BaseObject init] #133

Open
iccir opened this issue Aug 8, 2018 · 0 comments
Open

Optimization: Don't call -[BaseObject init] #133

iccir opened this issue Aug 8, 2018 · 0 comments

Comments

@iccir
Copy link
Member

iccir commented Aug 8, 2018

Assume the following code:

@implementation Foo

@property String name;

- (instancetype) initWithName:(String)name
{
    if ((self = [super init])) {
        _name = name;
    }
    return self;
}

- (Bar) makeBar
{
    return [[Bar alloc] init];
}

@end

@implementation Bar
@end

oj generates the following code:

$oj_oj._registerClass({$oj_c_Foo:1}, null, function($oj_s, $oj_m) { function $oj_c_Foo() { this.$oj_i_Foo$_name=null;this.constructor = $oj_c_Foo;this.$oj_id = ++$oj_oj._id;}

$oj_m.$oj_f_name = function() { return this.$oj_i_Foo$_name; } ; 

$oj_m.$oj_f_initWithName_ = function(name) 
{
    var self = this;if ((self = $oj_c_Foo.$oj_super.prototype.init.call(this))) {
        self.$oj_i_Foo$_name = name;
    }
    return self;
};

$oj_m.$oj_f_makeBar = function() 
{
    var $oj_t_0;return (($oj_t_0 = (new $oj_oj._cls.$oj_c_Bar())) && $oj_t_0.init());
};

return $oj_c_Foo;});

$oj_oj._registerClass({$oj_c_Bar:1}, null, function($oj_s, $oj_m) { function $oj_c_Bar() { this.constructor = $oj_c_Bar;this.$oj_id = ++$oj_oj._id;}return $oj_c_Bar;});

There are a few optimizations we can make here, based on the fact that -[BaseObject init] simply returns self.

-[Foo initWithName:] could be rewritten to:

$oj_m.$oj_f_initWithString_ = function(string) 
{
    self.$oj_i_Foo$_name = name;
    return self;
};

-[Foo makeBar] could be rewritten to:

$oj_m.$oj_f_makeBar = function() 
{
    return new $oj_oj._cls.$oj_c_Bar();
};

In practice, the performance improvement may be negligible. However, I'm trying to document all optimization ideas that I've been thinking about recently.

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

1 participant