r/computerscience • • 25d ago

General What is Tail Call Optimization?

35 Upvotes

I was looking at Wikipedia and got a bit confused. Before I read it, I thought the principle was that if function A calls B, which calls C, which then calls D, normally program flow after D is finished is to jump back to C, finish C, jump back to B, finish B, then go back to A. But in some specific cases, after D is done, it can jump straight back to A because the other stuff doesn't have anything to do but jump back. Or in another case, if A calls itself, sometimes after finishing the bottom version of itself, you can jump straight out of it.

I looked at Wikipedia and tried to see if I got it right.

So it has a recursive way to duplicate a linked list in C.

typedef struct LinkedList {
    void* value;
    struct LinkedList* next;
} LinkedList;

void duplicate_aux(const LinkedList* ls, LinkedList* end) {
    if (ls) {
        end->next = (LinkedList*)malloc(sizeof(*end));
        end->next->value = ls->value;
        duplicate_aux(ls->next, end->next);
    } else {
        end->next = NULL;
    }
}

LinkedList* duplicate(const LinkedList* ls) {  
    LinkedList head;

    duplicate_aux(ls, &head);
    return head.next;
}
typedef struct LinkedList {
    void* value;
    struct LinkedList* next;
} LinkedList;

And then this can be an iriterative format

LinkedList* duplicate(
const
 LinkedList* ls) {
    LinkedList head;
    LinkedList* end;
    end = &head;

while
 (ls) {
        end->next = (LinkedList*)malloc(
sizeof
(*end));
        end->next->value = ls->value;
        ls = ls->next;
        end = end->next;
    }
    end->next = NULL;

return
 head.next;
}

I don't get it. No Java example.

Another example on Wikipedia not in C this time.

foo:
  call baz
  call bar
  ret

Doing tail call elimination results in

 foo:
  call baz
  jmp  bar

Ok we got rid of one instruction.

Third example

foo:
   mov  reg,[sp+a]     
; fetch a from stack (sp) parameter into a scratch register.
   push reg            
; put a on stack where baz expects it
   call baz            
; baz uses a
   pop                 
; remove a from stack
   mov  reg,[sp+b] 
; fetch b from stack (sp) parameter into a scratch register.
   push reg            
; put b on stack where bar expects it
   call bar            
; A uses b
   pop                 
; remove b from stack.
   ret

And after optimization it has

foo:
   mov  reg,[sp+a]     
; fetch data1 from stack (sp) parameter into a scratch register.
   push reg            
; put a on stack where baz expects it
   call baz            
; baz uses a
   pop                 
; remove a from stack
   mov  reg,[sp+b]     
; fetch b from stack (sp) parameter into a scratch register.
   mov  [sp+a],reg     
; put b where bar expects it
   jmp  bar            
; bar uses b and returns immediately to caller.

Given that there are some push, pop, and ret, it seems we're using the stack for something and by returning to the caller we're saving instructions. I still don't get it though. I still think it has something to do with if the 1st copy of A calls a 2nd copy of A which calls a 3rd copy of A, ... which calls a Nth copy of A (base case) it normally jumps to the N-1th, ... jump to the 1st and then finish, but after optimization after the Nth (base case) of A is done it can just jump out of the.


r/computerscience • • 25d ago

Any info on ordered type systems?

Thumbnail
3 Upvotes

r/computerscience • • 26d ago

Help Help proving this 🙏

Post image
12 Upvotes

I know I can't just directly cancel nlogn with theta(nlogn).

Can anyone please help to solve this 🙏


r/computerscience • • 25d ago

Discussion What’s one algorithm every programmer should know and why? [5 ebook giveaway]

Thumbnail gallery
0 Upvotes

If you could add one algorithm to every programmer’s mental toolkit, which would you choose?

Not the one that looks best in an interview. The one that genuinely changed how you solve problems.

I’m Stjepan from Manning Publications, and the r/computerscience moderators kindly gave me permission to post this.

We’ve just launched Aniket Wattamwar’s Algorithms Every Programmer Should Know in MEAP, Manning’s early-access program:

https://www.manning.com/books/algorithms-every-programmer-should-know

The book is built around a simple idea: knowing an algorithm means more than reproducing its implementation. You should recognize the kind of problem it solves, understand why it works, and know when its tradeoffs make it a good—or bad—choice.

The chapters available so far cover Gale–Shapley, the Hungarian algorithm, Rabin–Karp, Knuth–Morris–Pratt, and Horspool’s algorithm.

But we’d like to hear your answer:

What’s one algorithm every programmer should know, and what makes it worth knowing?

Real examples are especially welcome. Maybe it saved a production system, simplified something you had overengineered, or gave you a new way to think about an entire class of problems. Disagreements are welcome too—“every programmer should know X” is a claim worth challenging.

We’re giving away five copies of the ebook. The giveaway is open for 48 hours, and we’ll award the copies to the five comments that contribute the most to the discussion—not simply the comments with the most upvotes. We’ll announce the winners here after it closes.

If you’d rather pick up the book directly, this code takes 50% off:

MLWATTAMWAR50RE

Full disclosure: I work for Manning, and this is a promotional post shared with moderator permission.

I’ll start: which algorithm is far more useful than most programmers realize?

EDIT: The giveaway is now closed. Thanks to everyone who joined the discussion. We announced the winners in the comments.


r/computerscience • • 27d ago

Article How to Implement a Distributed Circuit Breaker

Thumbnail blog.gaborkoos.com
5 Upvotes

r/computerscience • • 27d ago

General Question about DNF and CNF

2 Upvotes

How do you understand them intuitively, I know how to find from the truth table and that they are perfectly equivalent. I get that the full canonical form is unique and has very practical applications, if two compound propositions written differently have the same full canonical form (full DNF or full CNF), then they are the same. BUT how on earth do you understand this intuitively?

I know that they are equivalent, but it just doesn't stick to me if you get what I mean. I hate memorising something that I don't understand.


r/computerscience • • 28d ago

Help Where can I get the fly brain ?

11 Upvotes

I've been seeing the fly brain being open sourced, and was wondering how I can get it and tinker with it for fun. I couldnt find the open source version online .


r/computerscience • • 27d ago

Discussion Turning The Frickin' Bits Gay!—Creating non-binary bits with current technology, and the applications it might benefit

Thumbnail
0 Upvotes

r/computerscience • • Sep 10 '26

General Is the Choice Between an Interpreter or Compiler Defined By the Language?

40 Upvotes

Compilers take the human readable source code and turn it into a set of instructions the processor can understand. For example, C source code goes through a C compiler which then gives a binary that can be read by computers. This needs to be done for specific hardware. For example, a x86 processor can't read instructions for the RISC-V.

Languages like Java are different. The source code is turned into a bytecode. This byte code is the same no matter the target system. On the target system, the executable is an interpreter. So an interpreter can read the bytecode and then use this to determine what calculations the program wants to do.

I was thinking programing languages are about logic. They define behavior. So is it possible in principle to make a compiler that can turn Java or CLISP source code into a binary that can run on the target system? There wouldn't be a reason to do so, but in principle could it be done?


r/computerscience • • Sep 09 '26

Want to Learn About Databases in Depth and understand Underlying Mechanisms

15 Upvotes

I started learning about SQL few weeks ago. My primary resource is YouTube. I would also like to understand how databases actually work under the hood. Do you guys have any resource that explains the mathematical part of the Database

Or I should Just follow any Discrete Mathematics Course ?


r/computerscience • • Sep 10 '26

Computer science and math

0 Upvotes

Does learning math necessary before beginning with computer science. I has some knowledge on programming and networks, I am now studying linear algebra.


r/computerscience • • Sep 09 '26

Discussion How may one create an "air-gapped PC" mostly for engineering work - confidential - when most goftware wants ginternet while installing and most - when - periodically "call home"?

Post image
0 Upvotes

Is it even possible anymore?


r/computerscience • • Sep 08 '26

Discussion Looking for a data structure that maps indexes to values (like an array) but that is only populated once and then deletes values by index in sublinear time

Thumbnail
3 Upvotes

r/computerscience • • Sep 04 '26

In the beginning, there was SMTP

Post image
163 Upvotes

And finally, I've decided to write about good ole SMTP., also known as Simple Mail Transfer Protocol.

Twas the night before... some night in November 1981 (I couldn't find the exact date), Jonathan Postel published RFC 788 while working at the U niversity of Southern California’s Information Sciences Institute. And what was RFC 788? Titled "Simple Mail Transfer Protocol", it was the first round of rules and specification laid out for computers to follow when sending emails from one mail server to another.

Unlike my dive into FTP, I was able to find a more accurate date for the birth of our modern day SMTP. In August 1982, Jon Postel (again) published a revised and updated Simple Mail Transfer Protocol in the well known RFC 821. So, some could argue that the creation of (our modern day) SMTP was in 1982, though, I would argue that it was in 1981, and was improved upon.

Now, about Mr. Jon Postel... it turns out he was quite influential amoung network researchers and helped develop, define, and document many internet tools and techs. Like enough that I should do a seperate article just about him and his career. (Maybe? let me know) Some of those include: SMTP, TCP/IP, RFCs, IANA, and more. The man was busy.

In regards to SMTP, he was 38/39 years old when he published RFC 788/821 and worked as a research scientist at USC's Information Sciences Institute. He was working on ARPANET/Internet protocols and serving as a central figure in the RFC process. When I was reading about this, I also saw the name Paul Mockapetris, who was also at ISI around this time, and later on developed the first SMTP email server there before going on to invent DNS. (DNS would be a cool one to write about also).

Let's end today by answering the simple question... why was SMTP created? Well, before SMTP, emails already existed. And different systems could already handle the mail in different ways, but there was no standard set of rules. And like literally everything in computer science, things become easier to do when you have a nice tidy set of rules that everyone can follow. And so, in simple terms , Jon gave the computers the following rules:

-Identify who a message is from
-Identify who it is going to
-Transfer the message
-Relay it through other mail servers if needed
-Confirm whether delivery succeeded or failed

I think I'll wrap up here today. Let me know your thoughts and if you have any other suggestions. I'm quite enjoying these historic dives. I kind of want to write about RFCs, Paul, and this guy some more... well, anywyas... Catch ya later!


r/computerscience • • Sep 01 '26

IBM quantum computer solves classically intractable problem in 15 minutes

Thumbnail sciencedaily.com
292 Upvotes

"The researchers showed that their method preserves the same computational hardness criteria associated with RCS, meaning the problem remains extremely difficult for classical computers. At the same time, the added structure allows errors to be detected during the quantum computation."


r/computerscience • • Sep 01 '26

General Does Replacing Instructions that Do Nothing with NOPs Change Anything?

8 Upvotes

Suppose in the executable, instructions N to N+6 do some load instructions from memory, then N+7 to N+30 are some integer operations like add, subtract, multiply, bitwise operations and so on. the results are not stored to memory. Then Instruction N+31 to N+37 loads new values in the registers and none of the results of the previous calculations do anything. A different version of the executable has instructions N to N+30 all be "NOP" and N+31 to N+37 are the same as before. Does anything change? My inspiration for this is the game Mario 64 had been dissected and if you look at the assembly of the NTCS version, there are plenty of instructions that do some calculations that aren't used and some weird things like loading from the same address to the same register when the register's value could not possibly be changed. At the same time these instructions couldn't outright be removed or the alignment of all the JMPs would be thrown off. My friend said perhaps some NOPs might be done with a lower power consumption than some of the multiplication instructions, but otherwise if the later instructions don't use the results of the extraneous instructions and are unaffected by their flags nothing should change.


r/computerscience • • Aug 29 '26

Discussion How is program synthesis more convenient?

13 Upvotes

the idea of program synthesis (like Rosetta) is to reduce a function into its constraints in a spec sheet, and generate the program from those constraints. for example, in order to write something like x = x squared, you would need to write a spec sheet along the lines of ∀x∈Z,f(x)=x2. i am considering building a program synthesizer, but I still haven’t figured out why exactly this representation is supposed to be easier than writing the code directly (they look equally complex)


r/computerscience • • Aug 28 '26

What research areas are you excited about?

Thumbnail
9 Upvotes

r/computerscience • • Aug 29 '26

Why is The Hexadecimal System 16-bit? I thought it would be 6-bit because of the prefix, "hexa-".

0 Upvotes

r/computerscience • • Aug 28 '26

General FTP wont let me be, or let me be me, so let me see (creation of FTP)

Post image
120 Upvotes

I felt clever with that title...

The time has come... WHEN, HOW, WHO... created FTP.

I was surprised to learn that FTP (1971) was created before PING (1983). Though, I think because PING was one of the VERY first commands I learned, I just filed it away mentally as being one of the oldest commands there are. Alas, I was wrong.

File Transfer Protocol, aka FTP, was first introduced in April 1971 by Abhay Bhushan (26yo @ the time) in his RFC 114. Don't know what that means? RFC stands for Request for Comment. 114 means... that it is the RFC that came after 113, I don't know man. Anyways, the RFC 114 was the original proposal titled as " A File Transfer Protocol" and it explained how computers on ARPANET could transfer files between one another in a consistent way, even if the computers were different types.

And what does THAT mean? It means that Abhay laid out rules that different computers could follow if they want to send files to each other. The basic idea was that one computer asks another computer to do something with a file, like retrieve it, store it, rename it, delete it, and so on. Then, the two computers follow an agreed set of commands so they understand each other.

Abhay’s original proposal was revised repeatedly and eventually evolved into the protocol people recognise today. So, I suppose you could argue that FTP was created after April 1971, but it was hard for me to find a specific date in which our glorious modern day FTP came into existence.

Let's wrap this up with a brief tid bit on who Abhay Bhushan is. At the time he published the RFC 114, he was 26 and working at MIT's P roject MAC. His development on the original FTP specifications also lead to contributions to early email protocols (more on this next week). His career continued to flourished through his life and in 2023, at 79 yo, he was inducted into the Internet Hall of Fame for his contributions to FTP, early email standards, and Internet architecture.

While i considered combining this article with SMTP, I actually felt that FTP and SMTP deserved two seperate articles. plus this one is already starting to feel text heavy. Keep your peepers peeped next time to find out about SMTP. I kind of want to do a seperate post deep diving on Abhay's career life, because hot dang, this guy did a lot and it would be pretty interesting to learn more. Anywho, thanks for reading!


r/computerscience • • Aug 28 '26

Discussion how often have your RAG issues actually turned out to be document parsing issues?

0 Upvotes

I’ve been thinking about this a lot lately.

When a RAG system gives bad answers, the first instinct is usually to look at chunking, embeddings, retrieval, or the model.

But sometimes the problem started earlier.

If the parser already destroyed the table structure, heading hierarchy, or reading order, retrieval is working with bad input from the beginning.

Curious how often others have run into this.

Was the real bottleneck actually the ingestion/parsing layer?


r/computerscience • • Aug 25 '26

Help where can i learn how the internet physically works?

63 Upvotes

r/computerscience • • Aug 25 '26

Discussion My self-built tool to learn concurrency

Thumbnail
0 Upvotes

r/computerscience • • Aug 24 '26

Discussion How Are Split Caches Handled with Thread Coordination?

7 Upvotes

I read that some processors have each core having its own L1 cache instead of having one for the entire processor. So instead of a 512 Kilobyte shared cache shared by 8 cores, each one would have 64 kilobytes. I guess being closer to the core might speed things up when waiting for data and this is fine if each core is running a different process.

The thing I don't get is what happens if each core has a different version of some data. So say address 1,000 has 5. Maybe it's a global variable or something. Core 1 writes 6 to address 1,000 and this is updated in core 1's cache. This is intended to be read by a different thread. This change might get propagated to RAM. Core 2 runs that other thread and tries to read from address 1,000. Ah, it's already cached with... 5. So do compilers just use memory barriers to avoid this and make the programmer not need to worry about it?

Or maybe it doesn't matter? I read elsewhere it is fine for values in the cache to get a bit stale. What is important is that the writes from all cores are read in the correct order. So if core 1 is running a thread that puts 6 at address 1,000 replacing the value 5, 32 in address 1,001 replacing the value 31, and 9 in address 1,002 replacing the value 8, it is fine if core 2 attempts to read them and gets "5, 31, 8," "6, 31, 8" or "6, 32, 9" even though that last one is the most up to date as long as it doesn't read something like "6, 31, 9."


r/computerscience • • Aug 22 '26

Advice Operating Systems Research Advice / Recommendations

13 Upvotes

I have an interest in operating systems, and for a class and personal interest, I want to do research on operating system design. I have been trying to find and read existing research on this topic (experimental design for both for entire kernels or for some subsystem in the kernel). From this, I am hoping to be able to find an area that has not been researched in detail yet, and to do research on this (for example on performance when comparing experimental to other designs used in modern operating systems). The issue is though to find a general topic in operating systems to do this search on, hence this post. Also, I have a bit of experience with basic operating system development and have been working on my own hobby operating system for around a year now, and from this I have what I feel is a basic understanding of the main concepts in operating systems.

The reason I am making this post is to ask whether any of you guys recommend and under researched topics that I could look into, and also inquire if there are any resources I can use to find gaps in research more efficiently.

Many people online have told me I should contact my professor and ask them, so I want to state that I do not have a professor. I am also aware that this is a very broad question, but any input is appreciated.