Article 56KR6 CodeSOD: A Slow Moving Stream

CodeSOD: A Slow Moving Stream

by
Remy Porter
from The Daily WTF on (#56KR6)

We've talked about Java's streams in the past. It's hardly a new" feature at this point, but its blend of being really useful" and based on functional programming techniques" and different than other APIs" means that we still have developers struggling to figure out how to use it.

Jeff H has a co-worker, Clarence, who is very anti-stream". It creates too many copies of our objects, so it's terrible for memory, and it's so much slower. Don't use streams unless you absolutely have to!" So in many a code review, Jeff submits some very simple, easy to read, and fast-performing bit of stream code, and Clarence objects. It's slow. It wastes memory."

Sometimes, another team member goes to bat for Jeff's code. Sometimes they don't. But then, in a recent review, Clarence submitted his own bit of stream code.

schedules.stream().forEach(schedule -> visitors.stream().forEach(scheduleVisitor -> { scheduleVisitor.visitSchedule(schedule); if (schedule.getDays() != null && !schedule.getDays().isEmpty()) { schedule.getDays().stream().forEach(day -> visitors.stream().forEach(dayVisitor -> { dayVisitor.visitDay(schedule, day); if (day.getSlots() != null && !day.getSlots().isEmpty()) { day.getSlots().stream().forEach(slot -> visitors.stream().forEach(slotVisitor -> { slotVisitor.visitSlot(schedule, day, slot); })); } })); }}));

That is six nested for each" operations, and they're structured so that we iterate across the same list multiple times. For each schedule, we look at each visitor on that schedule, then we look at each day for that schedule, and then we look at every visitor again, then we look at each day's slots, and then we look at each visitor again.

Well, if nothing else, we understand why Clarence thinks the Java Streams API is slow. This code did not pass code review.

proget-icon.png [Advertisement] ProGet's got you covered with security and access controls on your NuGet feeds. Learn more. TheDailyWtf?d=yIl2AUoC8zAq_koMjsdpiU
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments