Today, the queue data structure! Twitter: / panpit0 • 🧠 DESIGN PATTERNS 🧠 • 🎥 GO FOR IT! 🎥 • 📚 PUBLISHING YOUR LIBR... • 💥 ALL ABOUT GO 💥 --- Golang version: 1.19 IDE: Goland OS: Windows Accent: French
Пікірлер: 6
@finicuro2744 Жыл бұрын
what a surprise, you're back!
@fringefringe7282 Жыл бұрын
Hm. Doesnt Pop() method have a memory leak? You are re-slicing which means that underneath new slice will reference the same array, just wont have access to 0th element. With large queues and complex elements (ie. structs) this can pose a problem. Would you agree? Many thanks.
@panpit0 Жыл бұрын
I don't think it's memory leak as in you can access to that memory address by mistake, the original slice still holds the value in memory, it is just a delayed garbage collection. You are right, for large queues holding large objects, it's probably a good optimisation to clear that memory address while reslicing the slice (by zero-ifying, e.g 0 for an int, "" for a string etc). Does that make sense to you? Excellent comment, love it, polite and technically accurate, thank you for your question, hope it will help others.
@fringefringe7282 Жыл бұрын
@@panpit0 Hm, I dont know what you mean by "the original slice still holds the value". In line 19 (queue.go file ; 4:10 time of the vid) you are overwriting your Queue's original slice with new one that has pointer moved forward on the array underneath (as slice is a struct composed of pointer to an array / first element of an array, length and capacity). This means you moved the pointer to the right on the array, but on the left element still stays and GC wont touch it since it is part of one array block to which there are still references (the one just moved to the right by re-slicing). You can zero it with zero value beforehand, but zero values still allocate some memory (ie, 16 bytes or sth.). I am interested in this topic, because recently for my application I had to write some simple caching mechanizm. That got me into this whole rabbit hole of slices. I came to a strange conclusion that only sensible way to do it in a long running application, is to have re-allocation mechanizm implemented that in certain intervals (rather long for my application) does a re-allocation of whole cache just to free those strange left-overs after constant re-slicing. Maybe I am wrong, maybe I dont understand something. I am just a Unix guy that took an adventurous route :)
@fringefringe7282 Жыл бұрын
BTW, Many thanks for all the videos. I am learning a lot from those design patterns vids.
@panpit0 Жыл бұрын
I’m on holiday, I’ll reply to you later next week!