Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

yes, it's really crazy, if people think you're exaggerating look at this:

https://github.com/tinygrad/tinygrad/blob/master/tinygrad/co...

One of the cases why I think obsession with lines of code is one of the most counterproductive metrics, it always produces code like this.





That doesn’t look super awful to me? Hardly extreme code golfing.

The far more interesting part is the order of magnitude. If they can pull off a 20k LOC with zero dependencies (implying a pretty concise project size) and it still works well on meaningful applications, that’s pretty neat. A 1000x reduction in code size and matching/exceeding in perf is worth looking at. Probably also implying a better architecture as code golf isn’t gonna get you 1000x less code. Again - their claims not mine, so we’ll see.

But at that point they can triple the LOC to 60k with nothing but white space, new lines, and comments, for all I care. It won’t even add a zero.


it looks dense but perfectly readable. arguably more readable that way than if it had a bunch of extra new lines, definitions, and code blocks spreading the logic out into a larger visible area.

I'm inclined to agree. While not always appropriate or popular, it makes some sense to me to have the visual weight/area of the code being ~proportional to it's significance. Communicates the ideas more clearly. Instead of spending the majority of space telling me what _isn't_ going to happen, and hiding what is.

I often find myself wishing this was more ergonomic in languages.


I think the discomfort I felt at looking at that was because of the stylistic difference. There isn't any part of it that looks terribly hard to understand when compared to other code. I presume anyone dealing with this codebase would know what ctx entails and most things seem to logically flow from what it provides.

I think if I dealt with this on a daily basis I might end up finding if vastly more preferable, albeit at the risk of alienating fellow developers. In that respect it seems much like learning to use Emacs or vi


TBH that looks pretty decent to me. GPU programming is very very complicated. A simple kernel in torch is hundreds to thousands of LOCs, with much worse style.

I agree with some poster above, the code looks hard simply because the problem is very hard, not because they want to write it that way.


Looks fine to me.

I'm going to guess that you're used to codebases that use an autoformatter.

There's definitely a case to be made for autoformatters in projects with a very large (>100) number of developers, but the price you pay for that is much poorer information density per screenful of code.

For things like tinygrad or sqlite with a small dev team it's possible to use whitespace, style, and formatting to communicate important information, just like mathematicians do. The AMS journals don't autoformat mathematicians' equations!


Even with autoformatters, it's a choice of what criteria you pick. It's possible to have consistency without enforcing low information density.

No, it really isn't. Like I said, math journals don't autoformat the mathematicians' equations. There's signal encoded in the formatting that a tool can't understand.

But if you have hundreds of developers on a project it does in fact make sense to sacrifice that signal in order to keep them from fighting with each other over silly formatting choices.


I wish. Stuff like Prettier is purposely unconfigurable and leads to the unreadability of JS code across the ecosystem.

> I'm going to guess that you're used to codebases that use an autoformatter.

I'm used to Python code being "Pythonic" - which is one of those "I know it when I see it" terms.


Running a formatter on that codebase would probably at least triple it.

Depends on formatter's config

I don't think this is bad, if you know Python then most of the code will be fine for you. I think you're probably referring to pm_reduce_collapse, but while it looks daunting at first, it really isn't when you consider the alternatives, I'd be curious on how you'd improve it short of creating a DSL.

Doesn’t seem too bad to me. It reminds me a bit of Clojure. Like Clojure, though, the lack of static types would be a headache.

You’re getting some pushback but one look at pm_reduce_collapse and I’m glad I don’t have to work in this codebase.

I see the advantage of the inline lambdas, but also it seems like it would hamper the ability to test the functions which can often have intricacies that catch you out. I believe one of the principles of Tinygrad is that tests shouldn't be part of the core codebase and shouldn't add to the total linecount.

I think there is an opportunity there to have a tag in comments for tests.

Have a hotkey to generate an id so you could have

  # lift x+y out of reduce on lt               :testID: color_adjective_noun  
  ((UPat.var("x")+UPat.var("y")).or_casted() < UPat.var("c"), lambda x,y,c: (x < (c.cast(y.dtype)-y)) if no_range(y) and no_range(c) else None),
and it creates a test that exists away from the main codebase. A process that runs tests could scan the code for testID's, intelligently extract the relevant part of the code into the test harness and runs the tests. There would need to be some infrastructure needed to handle placing the code to be tested into the right context but you could have

  1. A codebase that contained no active code required simply to facilitate tests, 

  2. All test tags auto removable for distribution builds

You don't really want or need classical unit tests for compilers. You need a harness that you can feed a program representation into and get a transformed one out, then check the result -- ideally mostly just by running it, or alternatively by comparing the output against a reference.

How is that not a unit test?

I've hear people refer to it as an end-to-end test, where unit tests usually test a single class or function.

That's only if you subscribe to the London school of though, which results in a bunch of useless tests that are coupled to implementation details.

I agree. It is definitely readable but clearly formatting decisions were made to keep line count down over the flow of code. It isn't even just the variable names, things like having a function with a single return statement on one line, while legal, I cannot imagine why someone would do it in a non one off script code.

> having a function with a single return statement on one line, while legal, I cannot imagine why someone would do it in a non one off script code.

I agree. Seems to be a personal preference, but still feels odd.


It’s not crazy at all, but personally I like simple code that flows down the page more, not across

It's two-dimensional code, not one-dimensional code.

Declarations flow down the page, definitions flow across.


Huh. This is more readable than almost any large python codebase to me at first glance. It’s 2D, and well commented. I find it an excellent use of screen real estate. Perhaps it would be a different story if most of us weren’t stuck in 16:9?

to quote an old academic research programmer: "Two-letter variable names are fine, as long as both letters are meaningful" :-)

This is like a JIT, generating code (machine code?). It’s a complex operation and IMO the code doing it is very clean and clear.

It helps that a single person can read all related bits on one screen.


Is is code a form of AST rewrite rules for optimization? This operation still looks like a incomprehensible wall of code 40 years later (I looked in the past inside the C++ compiler).

Good code but on the way to terse Better comments needed

Funny to me that it is called simplify.py




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: