Hi list.
This is my first time posting here, so bare with me if I'm being an idiot.

I apologize for continuing with the performance discussion but I've noticed
unintuitive (at least to me) behaviour when using interfaces in Vala.

I know that calling interface methods is expensive and that it's a known issue
in GLib but there is another problem. When a class implements an interface,
calling methods is slow regardless whether you declare your objects as being
instances of the class or the interface.

I attached a simple example that demonstrates the problem.
Object1 implements the interface directly and it doesn't matter whether we
declare our object as Interface or Object1, both are slow.

Object1 through interface : 18.620215
Object1 directly          : 18.767607

Object2 does not implement Interface directly but inherits form AbsObject
(which in this case is abstract but it could also be a regular class). which in
turn implements Interface, now only the instance declared as Interface is 
slower.

Object2 through interface : 18.902430
Object2 directly          : 2.987541

Looking at the generated C code it seems to be a side effect of the way vala
handles inheritance but I don't have enough experience with GObject to judge.

So my question would be whether this is intentional, maybe I'm missing 
something?
If it is then interfaces have to be used even more carefully and you need to use
a dummy class in between the interface just to be able to use your class without
the extra overhead when the interface is not needed.

Regards, Sergej

int main() {
	int num_iterations = 300000000;
	Profiler p;
	
	Interface ob1_interface = new Object1(0);
	p = new Profiler("Object1 through interface");
	for(int i = 0; i <= num_iterations; i++) {
		ob1_interface.add(i);
	}
	p = null;
	Object1 ob1_directly = new Object1(0);
	p = new Profiler("Object1 directly         ");
	for(int i = 0; i <= num_iterations; i++) {
		ob1_directly.add(i);
	}
	p = null;
	Interface ob2_interface = new Object2(0);
	p = new Profiler("Object2 through interface");
	for(int i = 0; i <= num_iterations; i++) {
		ob2_interface.add(i);
	}
	p = null;
	Object2 ob2_directly = new Object2(0);
	p = new Profiler("Object2 directly         ");
	for(int i = 0; i <= num_iterations; i++) {
		ob2_directly.add(i);
	}
	p = null;
	stdout.printf("----------------------------------------\n");
	return 0;
}

public interface Interface : Object {
	public abstract void add(int i);
}
public abstract class AbsObject : Interface, Object {
	public abstract void add(int i);
}

public class Object1 : Interface, Object {
	private int val;
	
	public Object1(int i) {
		this.val = i;
	}	
	public void add(int i) {
		this.val += i;
	}
}
public class Object2 : AbsObject {
	private int val;
	
	public Object2(int i) {
		this.val = i;
	}	
	public override void add(int i) {
		this.val += i;
	}
}


public class Profiler {
	private string description;
	private Timer t;
	
	public Profiler(string description) {
		this.description = description;
		t = new Timer();
		t.start();
	}
	~Profiler() {
		t.stop();
		stdout.printf("%s : %lf\n", description, t.elapsed());
	}
}
_______________________________________________
vala-list mailing list
[email protected]
http://mail.gnome.org/mailman/listinfo/vala-list

Reply via email to