News for hackers. Stuff that matters.
Re:Important PostgreSQL 14 update to avoid silent corruption... by msk20 on Thursday June 09, 2022 @07:39PM (#31688337)
Looking at the file with changes https://github.com/postgres/postgres/blob/master/src/backend... , I have to say this source code repository is so well documented/commented and structured, I really gives you a huge trust in postgres to be used in your stack.
Re:Important PostgreSQL 14 update to avoid silent corruption... by SonOfLilit on Thursday June 09, 2022 @09:02PM (#31688813)
Large C codebases _have_ to be exceptionally nice, or they immediately collapse under their own weight. As a dev team, the language teaches you this the hard way. I've never seen a terrible huge C codebase (but have seen many in other languages).
Re:Important PostgreSQL 14 update to avoid silent corruption... by bigiain on Friday June 10, 2022 @12:02AM (#31689881)
> I've never seen a terrible huge C codebase

I have 100% confidence they exist. They just don't get uploaded to Github out of shame or embarrasment.

Re:Important PostgreSQL 14 update to avoid silent corruption... by obudo on Friday June 10, 2022 @05:03AM (#31691716)
Can confirm they do. I worked at a company that was producing control systems for electric engines. Great environment and fun job but the code was beyond redemption. 15k line files with 2k+ lines #ifdef statements that ran different code for different customers, some variable names were just curses against pushy clients, not a single abstraction in sight.

Not only they exist, they power massive machines that could crush a person in the blink of an eye.

Re:Important PostgreSQL 14 update to avoid silent corruption... by urthor on Friday June 10, 2022 @07:42AM (#31692692)
I think OP just found a really well done C file.

You can definitely see many famous projects on GitHub, where even though the code is logical, you can't help feel it's a bit all over the place.

cpython isn't all sun and roses.

A broad generalization, but with C and C++ I'd say the standards for "good code" have dramatically risen in the last 10 years.

Variable names seem to have doubled in size.

Re:Important PostgreSQL 14 update to avoid silent corruption... by e12e on Friday June 10, 2022 @09:01AM (#31693468)
> I've never seen a terrible huge C codebase

libssl/openssl (before the rewrite/fixes especially)? Maybe doesn't qualify as huge?

See eg: https://www.youtube.com/watch?v=GnBbhXBDmwU "LibreSSL with Bob Beck" (the first 30 days)

Re:Important PostgreSQL 14 update to avoid silent corruption... by fathyb on Friday June 10, 2022 @12:14AM (#31689957)
Proprietary device drivers come to mind..
Re:Important PostgreSQL 14 update to avoid silent corruption... by SonOfLilit on Friday June 10, 2022 @04:15AM (#31691403)
They don't tend to be huge && in C
Re:Important PostgreSQL 14 update to avoid silent corruption... by ventuspilot on Friday June 10, 2022 @06:29AM (#31692188)
AMD's linux graphics driver has entered the chat
Re:Important PostgreSQL 14 update to avoid silent corruption... by doliveira on Friday June 10, 2022 @06:14AM (#31692098)
Thankfully they don't follow the whole mindset of "self-documenting code"
Re:Important PostgreSQL 14 update to avoid silent corruption... by chungy on Thursday June 09, 2022 @08:15PM (#31688547)
PostgreSQL code quality is exceedingly fine indeed.
Re:Important PostgreSQL 14 update to avoid silent corruption... by colordrops on Thursday June 09, 2022 @08:08PM (#31688502)
File could definitely be broken up a bit. Over 5000 lines! Just a nitpick though.
Re:Important PostgreSQL 14 update to avoid silent corruption... by gnulinux on Thursday June 09, 2022 @08:27PM (#31688615)
Quite the opposite, I hate it when projects have dozens upon dozens of modules with 1 function. Multiple huge files are the best sweet spot. (Only crazy adn exceptional things like putting everything into a single file damages the readability imho)
Re:Important PostgreSQL 14 update to avoid silent corruption... by colordrops on Thursday June 09, 2022 @11:11PM (#31689533)
Files with one function vs files with 5000 lines are not the only two options.
Re:Important PostgreSQL 14 update to avoid silent corruption... by fjfbsufhdvfy on Friday June 10, 2022 @12:41AM (#31690129)
I've got it! We could use files with one function that spans 5000 lines.
Re:Important PostgreSQL 14 update to avoid silent corruption... by colordrops on Friday June 10, 2022 @02:10AM (#31690658)
How about a function that spans 5000 files
Re:Important PostgreSQL 14 update to avoid silent corruption... by pcthrowaway on Friday June 10, 2022 @02:38AM (#31690812)
Why even use 5000 lines when you you can often combine them all into one line.
Re:Important PostgreSQL 14 update to avoid silent corruption... by pmontra on Friday June 10, 2022 @03:01AM (#31690962)
That's the job of the compiler, then we can use a disassembler to read the actual optimized source code.
Re:Important PostgreSQL 14 update to avoid silent corruption... by osigurdson on Thursday June 09, 2022 @10:19PM (#31689273)
300 to 1000 lines per file is best IMO.
Re:Important PostgreSQL 14 update to avoid silent corruption... by wahern on Friday June 10, 2022 @01:30AM (#31690406)
IMO, paintings with the color blue are the best.

What does number of lines have to do with anything?

Re:Important PostgreSQL 14 update to avoid silent corruption... by rgoulter on Friday June 10, 2022 @03:58AM (#31691304)
At each extreme:

A source code file with very few lines will mean more cognitive load is required to remember which file (or package) some functionality is, and likely more effort to maintain the code.

Whereas, source code files which are very large mean more cognitive effort to remember where in the file some function is. In many languages, variables can be local to a file, which means use of such variables is riskier, etc.

It may be desirable to combine smaller files into a more coherent whole, or to split up an overly complicated large file into several smaller files.

Sure, without seeing code, I don't think you can come up with a concrete rule which exists in all cases. Rules of thumb can still be useful as an indication of maintenance effort.

Re:Important PostgreSQL 14 update to avoid silent corruption... by jvolkman on Thursday June 09, 2022 @08:22PM (#31688592)
I get the impression that long files are culturally acceptable in systems-level C code. E.g., just a cherry-picked file from Linux: kernel/sched/core.c is over 11k lines.

https://github.com/torvalds/linux/blob/master/kernel/sched/c...

Re:Important PostgreSQL 14 update to avoid silent corruption... by bombcar on Thursday June 09, 2022 @08:36PM (#31688672)
I feel the long file issue is mostly gone - that the problem isn’t the file length but spaghetti code. If it makes sense to be in one file it should be in one file. Breaking it up simply to reduce file length is counter productive.
Re:Important PostgreSQL 14 update to avoid silent corruption... by cperciva on Friday June 10, 2022 @02:29AM (#31690754)
There are 18609 .c files in my checked-out copy of the FreeBSD src tree. The median length is 258 lines; 90% are 1373 lines or shorter; 99% are 5241 lines or shorter.

The statistics for the 6071 .c files in the FreeBSD kernel are somewhat higher -- median is 460 lines; 90th percentile is 2070 lines; 99th percentile is 7678 lines -- but your example of a 11133 line file is definitely at the extreme high end.

Re:Important PostgreSQL 14 update to avoid silent corruption... by jvolkman on Friday June 10, 2022 @12:56PM (#31696666)
I didn't run any stats when I found that file. Just clicked around in github a handful of times looking for something that seemed like it'd be complex.
Re:Important PostgreSQL 14 update to avoid silent corruption... by cozzyd on Thursday June 09, 2022 @11:16PM (#31689578)
yes are people using editors that don't let them have multiple views of the same file or something?
Re:Important PostgreSQL 14 update to avoid silent corruption... by colordrops on Friday June 10, 2022 @12:16AM (#31689971)
A filepath is an index and a hiearchy that adds information and structure. It can't be completely replaced by editor affordances.
Re:Important PostgreSQL 14 update to avoid silent corruption... by carlhjerpe on Friday June 10, 2022 @07:24AM (#31692545)
Depends on the language. C# somewhat replaces paths with namespaces, then you navigate classes and methods with editor tooling. I remember back when I was on Visual Studio writing C++ that they did something similar.
Re:Important PostgreSQL 14 update to avoid silent corruption... by RcouF1uZ4gsC on Thursday June 09, 2022 @08:20PM (#31688576)
Actually I don’t mind big files. It is simpler scanning through it or doing a quick search than if you had a bunch of smaller files. And 5000 lines is not awkward for most editors, especially as many editors have the ability to collapse functions.
Re:Important PostgreSQL 14 update to avoid silent corruption... by eyelidlessness on Thursday June 09, 2022 @08:44PM (#31688731)
This feels like a good area for tooling (editors, source hosts, SCM extensions) to improve experience. I don’t always mind large source files (and sometimes may prefer them over large file system hierarchies), but the can be a pain to navigate in some circumstances.

As an example, making several related changes in very different parts of a file, where you need to cross-reference between them. The changes themselves might be small, but it’s a huge cognitive burden to alternate/iterate through them. I’d love to have a view which temporarily projects those targets as if they’re isolated files without changing the actual structure on disk. I’d love it so much I actually do this manually for a lot of tasks, creating temp files to prepare edits for related areas of code. But then I lose a lot of the benefits of tools which understand what’s being referenced. It would be great to just type a quick command (or click or whatever) to say “don’t refactor this function to another file, but let’s pretend you did, for a while”.

Re:Important PostgreSQL 14 update to avoid silent corruption... by BlueDingo on Friday June 10, 2022 @12:02AM (#31689889)
This is how older editors like emacs work. You interact with views/windows/tabs called buffers and those buffers can have files loaded into them. Multiple buffers can reference the same code file but view different sections simultaneously. So you can investigate or edit different parts of one huge file the same way you would smaller ones.
Re:Important PostgreSQL 14 update to avoid silent corruption... by wahern on Friday June 10, 2022 @01:43AM (#31690484)
You just blew my mind.

I use JOE and JOE also supports multiple views into the same file. In fact, to open another file is two commands: the open view/window (^KO) command followed by edit file command (^KE). I've always used this facility for as long as I can remember and it never really occurred to me until now that people using more modern editors and especially GUI editors may not enjoy this same convenience--either not possible or no simple chain of command inputs to get there. And it's not like I don't use GUI editors, just not in situations where I would realize this feature was missing.

Re:Important PostgreSQL 14 update to avoid silent corruption... by joking on Friday June 10, 2022 @02:10AM (#31690664)
You can do this in vscode as well, even have it side by side. I’m missing something?
Re:Important PostgreSQL 14 update to avoid silent corruption... by qw on Friday June 10, 2022 @04:16AM (#31691411)
I just opened IntelliJ and it has this feature too, where you can "Split right" or "Split down" and have multiple views of the same file. Thanks for letting me know this is something editors might support.

For some reason I have not noticed that feature before. It's not like it is hidden either, as it is in the "right click" context menu that I use daily. I guess I need to learn the tool, so that I don't miss useful features like this.

Re:Important PostgreSQL 14 update to avoid silent corruption... by mjw1007 on Friday June 10, 2022 @04:26AM (#31691483)
For the "temporarily projects those targets as if they’re isolated files" part, you want something like narrow-indirect or edit-indirect.

https://www.emacswiki.org/emacs/NarrowIndirect

https://github.com/Fanael/edit-indirect

Re:Important PostgreSQL 14 update to avoid silent corruption... by djsavvy on Thursday June 09, 2022 @11:22PM (#31689610)
> The changes themselves might be small, but it’s a huge cognitive burden to alternate/iterate through them. I’d love to have a view which temporarily projects those targets as if they’re isolated files without changing the actual structure on disk.

This is exactly how vim buffers work (for instance in a split) work.

Re:Important PostgreSQL 14 update to avoid silent corruption... by eyelidlessness on Friday June 10, 2022 @02:00AM (#31690598)
I’m glad to see I’ve brought the emacs/vim people together for a common purpose!
Re:Important PostgreSQL 14 update to avoid silent corruption... by cozzyd on Thursday June 09, 2022 @11:18PM (#31689585)
this is what :[v]split is for.. .
Re:Important PostgreSQL 14 update to avoid silent corruption... by plq on Thursday June 09, 2022 @10:42PM (#31689379)
While I'm not sure that was a consideration here, sometimes C compilers produce better machine code when they got access to more function definitions. Eg. Sqlite recommends that embedders use the single ~10mb sqlite.c file[1] for both ease of use and performance reasons.

[1]: https://www.sqlite.org/amalgamation.html

Re:Important PostgreSQL 14 update to avoid silent corruption... by colordrops on Thursday June 09, 2022 @11:11PM (#31689536)
Seems like something the compiler should take care of.
Re:Important PostgreSQL 14 update to avoid silent corruption... by fjfbsufhdvfy on Friday June 10, 2022 @12:43AM (#31690138)
It can when you use LTO, but that tends to be very slow for large programs.
Re:Important PostgreSQL 14 update to avoid silent corruption... by plq on Friday June 10, 2022 @06:57AM (#31692362)
It is slow. Our (C++) project's MSVC release build ends with a glorious 2-minute run of link.exe with lto (/LTCG /O2) and aggressive inlining (/Ob3) enabled.

Limiting unit size in C++ helps with faster edit/compile/run cycles as well, which doesn't seem to be concern for C codebases in 21st century.

Re:Important PostgreSQL 14 update to avoid silent corruption... by anarazel on Friday June 10, 2022 @05:56PM (#31699801)
You hit most of the same issues (and some additional ones) when compiling the same code within one translation unit...
Re:Important PostgreSQL 14 update to avoid silent corruption... by throwawaylinux on Thursday June 09, 2022 @09:41PM (#31689023)
5000 lines looks like half are comments or whitespace, none of the functions look more than a couple hundred lines or a few levels of control flow depth. Pretty harsh nitpick.
Re:Important PostgreSQL 14 update to avoid silent corruption... by colordrops on Thursday June 09, 2022 @11:12PM (#31689547)
The point of calling it a nitpick was to indicate precisely that my comment wasn't meant to be taken harshly.
Re:Important PostgreSQL 14 update to avoid silent corruption... by throwawaylinux on Thursday June 09, 2022 @11:35PM (#31689703)
But nitpick still means it is a nit, I don't really think it is at all. And you're saying it in response to someone who said the code is very clean, which is quite petty.
Re:Important PostgreSQL 14 update to avoid silent corruption... by colordrops on Friday June 10, 2022 @12:08AM (#31689918)
> nitpick: engage in fussy or pedantic fault-finding.

Yes, I'm fully aware that the comment is petty. The point is to communicate that I don't disagree that the code is clean and well written.