Live data from Hacker News

It Can Happen to You

mattkeeter.com

281–290 of 419 posts

Re: It Can Happen to You

#281

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…

The index is perfectly valid; it's just that not every index may be used for subscripting. This is exactly how C++ does it too.

Re: It Can Happen to You

#282
post #32

Loving 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.

I don't think I've laughed this much since the pandemic started, well done.

Re: It Can Happen to You

#283

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 ..…

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…

The index is not bogus, the API is working as designed. The example provided shows the use of the index, which I understand can be confusing because the index returned may not always be valid for this, but the index is decidedly valid. FWIW, since String is a BiderectionalCollection, this code works for what you are probably trying to do:

  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

#284

Earlier 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?

It also runs on PC

Re: It Can Happen to You

#285
post #25
post #2

Pro 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.

Fibonacci is generally more likely to take forever than stack overflow.

Re: It Can Happen to You

#286

Earlier 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".

I (sadly) have to resort to use "O(scary)" too often for my taste.

From: https://stackoverflow.com/a/185576/368409

    /* This is O(scary), but seems quick enough in practice. */

Re: It Can Happen to You

#287
post #266

Earlier 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 lived long enough to be very sceptical about that sort of thing.

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

#288
post #32

Loving 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.

Well, https://twitter.com/stephentyrone/status/1366573121365565444

>> 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

#289
post #153

Earlier 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.

That would likely break backward compatibility of existing implementation-defined behavior.

Re: It Can Happen to You

#290
post #188

I 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.

And that‘s ok. But it seems no one in management approves benchmarking it now that loading is taking several minutes (!).

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.

Post reply on HN