[swift-evolution] [Review] Require self for accessing instance members
Tino Heth
2th at gmx.de
Sat Dec 19 03:02:20 CST 2015
imho the most valuable vote for mandatory self, just one comment on it
> 1) Accessing a property, in a class, is more expensive than accessing a local variable. The above code would require three message sends and/or vtable accesses instead of one, if it were not assigning the property to a local variable. These unnecessary property accesses needlessly reduce the efficiency of the program.
True for Objective-C, and might be true for Swift in many cases as well - but just because of legacy reasons:
Correct me if I'm wrong, but I expect that simple getters have no performance penalty at all.
Nonetheless, you can't see how expensive the access is, and the effects in multi-threaded environments are a real issue - but those two reasons are no argument for mandatory self, but rather against it:
When there are issues with repetitive access, you can just insert a "let foo = self.foo", and the problem is gone; you can't do so when you already referenced the variable with "self.foo" all over the place.
This is imho one of the most important lessons to learn from this thread:
Just don't give your variables confusing names, unless you have good reason to do so!
Best regards,
Tino
p.s.:
I've have read an article that advises not to cache getters at all - but couldn't remember or search a reference for this. Afair the proposal came from a Java-background - which fits to my memory that the possible performance problems weren't addressed ;-)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151219/b0ce5181/attachment.html>
More information about the swift-evolution
mailing list