Computer
Reddit Must Share IP Addresses of Piracy-Discussing Users, Film Studios Say
Read more of this story at Slashdot.
Lazy Use of AI Leads To Amazon Products Called 'I Cannot Fulfill That Request'
Read more of this story at Slashdot.
Can The AI Industry Continue To Avoid Paying for the Content They're Using?
Read more of this story at Slashdot.
Why a School Principal Switched from Smartphones to Flip Phones
Read more of this story at Slashdot.
CodeSOD: A Simple List Copy
Mike's team had a new hire. They did great on the interview, really shined in the coding challenges, so it was a no-brainer hire. In the middle of on-boarding, the team got slammed, so this new hire ended up being left to fend for themselves.
This was a mistake.
public class ElementHandler { private final List<Element> elements = new ArrayList<Element>(); public final List<Element> getElements() { final Element[] allElements= elements.toArray(new Element[elements.size()]); final List<Element> result = new ArrayList<Element>(); for(int i =0; i<allElements.length; i++){ result.add(allElements[i]); } return result; } }The getElements function here looks like a basic accessor method. Clearly, the developer was concerned about just returning a reference to the class's internal state- a wise concern- and opted to return a copy.
They then just failed to use any of the basic .NET Framework methods for making a copy and made their own. Badly.
First, they convert the list toArray. This does a deep copy on all the elements. Then they iterate across all of those elements to populate a new List, which is the return value.
There are some fun .NET internals here- allocating an empty ArrayList creates an array list with an internal buffer of 0 elements. When you add the first element, the ArrayList creates a buffer that can hold 4 elements. When that buffer gets filled, the ArrayList creates a new buffer, twice that size, and copies all the elements into the new buffer. Each time the buffer fills up, all the items get copied again.
So, depending on how big this input list is, this code could be inserting a lot of copy operations that you never see, copying the same elements over and over again.
What makes this so frustrating is that .NET has a perfectly simple way to create a copy and preallocate an array of the correct size, all in one step:
return new ArrayList<int>(elements)This version has only one copy operation per element in the list, not potentially many. And, honestly, it's easier to read and clearer about its purpose.
But, I guess the "right" way doesn't give you the option to stress test the garbage collector.
[Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!Can Pumping CO2 Into California's Oil Fields Help Stop Global Warming?
Read more of this story at Slashdot.
The Global Project To Make a General Robotic Brain
Read more of this story at Slashdot.
Scientists Film Genetically-Altered Plants 'Talking' to Neighboring Plants With Biochemicals
Read more of this story at Slashdot.
Ask Slashdot: Could a Form of Watermarking Prevent AI Deep Faking?
Read more of this story at Slashdot.
Python Software Foundation Says EU's 'Cyber Resilience Act' Includes Wins for Open Source
Read more of this story at Slashdot.
Linux Mint 21.3: Its First Official Release with Wayland Support
Read more of this story at Slashdot.
California Tech Company's 'Return-to-Office' Video Mocked as Bizarre, Cringe-Worthy
Read more of this story at Slashdot.
Seeing Blue At Night May Not Be What's Keeping You Up After All
Read more of this story at Slashdot.
What Laws Will We Need to Regulate AI?
Read more of this story at Slashdot.
Atari Will Release a Mini Edition of Its 1979 Atari 400 (Which Had An 8-Bit MOS 6502 CPU)
Read more of this story at Slashdot.
Chinese Company Announces Mass Production of Small Nuclear Battery With 50-Year Lifespan
Read more of this story at Slashdot.
Should Chatbots Teach Your Children?
Read more of this story at Slashdot.
Private US Moon Lander Now Headed For Earth, Might Burn Up In Atmosphere
Read more of this story at Slashdot.
Post-Quantum Encryption Algorithm KyberSlash Patched After Side-Channel Attack Discovered
Read more of this story at Slashdot.
Bill Gates Interviews Sam Altman, Who Predicts Fastest Tech Revolution 'By Far'
Read more of this story at Slashdot.