Earlier quoted context omitted.
You can fault the docs but what's the problem with the API? Why should you be surprised that accessing a collection with 'nil' index is a runtime error? What else could it be? A simple fix in the doc seems to solve the confusion: "Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index [in which case it returns nil]". It does say "returns an index ..…
The problem is that `s.index` with an `offset` equal to `limitedBy` returns non-nil index, rather than nil, but that index is invalid (out of bounds) and causes the program to blow up... let s = "Swift" print(s.index(s.startIndex, offsetBy: 4, limitedBy: s.endIndex)) print(s.index(s.startIndex, offsetBy: 5, limitedBy: s.endIndex)) print(s.index(s.startIndex, offsetBy: 6, limitedBy: s.endIndex)) outputs: Optional(Swif…
It Can Happen to You
281–290 of 419 posts
Re: It Can Happen to You
#282Loving the progression here. Tomorrow, someone’s going to reduce the boot times of macOS by 90% by the same principle. A week from now, someone will prove P=NP because all the problems we thought were NP were just running strlen() on the whole input.
Re: It Can Happen to You
#283Earlier quoted context omitted.
You can fault the docs but what's the problem with the API? Why should you be surprised that accessing a collection with 'nil' index is a runtime error? What else could it be? A simple fix in the doc seems to solve the confusion: "Returns an index that is the specified distance from the given index, unless that distance is beyond a given limiting index [in which case it returns nil]". It does say "returns an index ..…
If s.index() returned nil, the "if" would test false and the s[i] would not be reached. The problem is that it returns non-nil, but the _limit_ is broken in this case: using s.endIndex as a limit means you can get non-nil but bogus indices returned. And yes, this is the fault of the docs for using a broken last arg to the API, but there's no really clean way to use this API as designed, afaict. At least not if you wa…
let s = "Swift"
if !s.isEmpty,
let index = s.index(s.startIndex, offsetBy: 5, limitedBy: s.index(before: s.endIndex)) {
print(s[index])
}
I am sure the equivalent code in other languages, barring Python, is going to be similarly verbose.Re: It Can Happen to You
#284Earlier quoted context omitted.
You're joking, but now I'm thinking about the XML we parse at work and the library we're using to do it. We parse a lot of it, but I've always had this vague feeling that it takes a bit too long (given the codebase is C++). The XML library we use is rather well-known, so if someone found a bug like this there, I'd suspect a general improvement of performance across the board in the entire industry. Efficient Market H…
I wonder of scanf on Playstation was not using strlen in that way. GTA was written for PS right?
Re: It Can Happen to You
#285Pro tip: the classic recursive approach to implementing the Fibonacci sequence exhibits eye-wateringly poor performance.
I've always hated how that's used as an example of recursive programming in early programming education. Invariably, students try a large n, get a stack overflow and conclude that recursion is inefficient, leads to mysterious errors, and must be avoided.
Re: It Can Happen to You
#286Earlier quoted context omitted.
It's easy to forget that the original C standards were largely codifying existing practice during an era when using gets() [1] was existing practice. The world wasn't quite ready for Ada, I guess. Best-laid plans of mice and men etc. etc.. Also, keep an eye out for "amortized" complexity. This does have a legitimately rigorous definition, but for latency-bound paths it can practically amount to "O(whatever), except f…
...I fully plan to use "O(whatever)". Not sure for what. But, yes. (naive) Quicksort's amortized complexity being O(nlogn), but its O(n^2) on already sorted data, is all I ever needed to learn to take away that lesson. When sorting already sorted data is worse than sorting randomized data, it's a quick realization that "amortized cost" = "read the fine print".
From: https://stackoverflow.com/a/185576/368409
/* This is O(scary), but seems quick enough in practice. */Re: It Can Happen to You
#287Earlier quoted context omitted.
You're joking, but now I'm thinking about the XML we parse at work and the library we're using to do it. We parse a lot of it, but I've always had this vague feeling that it takes a bit too long (given the codebase is C++). The XML library we use is rather well-known, so if someone found a bug like this there, I'd suspect a general improvement of performance across the board in the entire industry. Efficient Market H…
> it's unlikely the library has this problem Any sufficiently-complex library code likely has plenty of problems, often unavoidably so (e.g. trade-offs between best performance and edge cases). Whether they have been found or not is a function of many, many factors. > Efficient Market Hypothesis I've lived long enough to be very sceptical about that sort of thing. Markets tend to be efficient in aggregate , maybe, bu…
I've also seen this story unfold too many times:
code code code build run fail
> dammit, I could have sworn this was correct?!
think think think GOTO 1
> no way, my code has to be wrong, this can't be the compiler?! it's never the compiler! right?
reduce code to a generic two liner, build, run, fail
> oh.
open support ticket at compiler vendor
Re: It Can Happen to You
#288Loving the progression here. Tomorrow, someone’s going to reduce the boot times of macOS by 90% by the same principle. A week from now, someone will prove P=NP because all the problems we thought were NP were just running strlen() on the whole input.
>> So many years ago when I first took over the iOS string functions, I found that like 30% of boot time in debug builds was in strstr. >> Needless to say, we did not fix that issue by writing a more efficient strstr. Removed the parser and then removed strstr from the environment where it had been in use =) <<
Re: It Can Happen to You
#289Earlier quoted context omitted.
Obligatory: it is flabbergasting that this is a quality of language that is still in active use.
It's even more flabbergasting that all these problems haven't been fixed.
Re: It Can Happen to You
#290I don‘t get the heat of this topic. Yes they wrote some very slow code because it‘s easy to shoot in your foot with scanf. It‘s nothing new that most software could be heavily optimized by just benchmarking slow parts. There is no reason for this shit storm than to feel better than other developers. The real problem is that they shipped a game with a loading screen which is taking minutes and not looking whether they…
Thing is that they didnt ship it that way. Back when it came out the loading screens were "fast". Things just grew out of proportion with the exponential increase of new items in the online mode.
I am not blaming the developers, they have a lot to do every day. Maybe someone even wanted to try to fix it. But that it‘s still like this is clearly showing that management doesn‘t care and they are completely ok with a loading screen taking longer than brewing a new cup of coffee.