News for hackers. Stuff that matters.
Re:For processing strings, streams in C++ can be slow by jonstewart on Thursday December 07, 2023 @07:40AM (#38555681)
These criticisms may apply to Java, but are not so clear cut with C++. Boxing/unboxing doesn’t exist, constructors/destructors have only the overhead you’ve put into them (and as bad as iostreams is, C++’s RAII is as glorious), vtables are only built if you’re using inheritance, and std::vector stores objects linearly in memory, yielding terrific cache and prefetch performance (in contrast with Java and its pointer chasing).
Re:For processing strings, streams in C++ can be slow by nicbn on Thursday December 07, 2023 @07:50AM (#38555755)
If you aren't using OOP features (such as inheritance), you're not really doing OOP, despite using C++.

In the case of C++ I'd put something like: you can use free or costly abstractions, and OOP in general has a preference towards costly ones.

Also vector is a weird point to make, it's been some time I had to deal with Java (luckily) but arrays there are also linear AFAIK. And there are GCs that have a bump allocator for new objects (not sure if Java fits here), so cache would benefit more than in sparse malloc allocations in C/C++.

Re:For processing strings, streams in C++ can be slow by simiones on Thursday December 07, 2023 @08:09AM (#38555891)
> Also vector is a weird point to make, it's been some time I had to deal with Java (luckily) but arrays there are also linear AFAIK. And there are GCs that have a bump allocator for new objects (not sure if Java fits here), so cache would benefit more than in sparse malloc allocations in C/C++.

I think the point is that Object[] in Java is a linear block of pointers to objects, whereas vector<Object> in C++ is a linear block of the objects themselves.

Re:For processing strings, streams in C++ can be slow by nicbn on Thursday December 07, 2023 @08:20AM (#38555982)
Fair enough. But it is needed to point out that there's a catch in that in order to use dynamic dispatch (subclasses, interfaces, ...) you'd still need to use pointers in C++.

Deep down the problem could be rephrased as "there are no structs in Java". In C# for example you could have a vector of structs and enjoy linear memory access.

Re:For processing strings, streams in C++ can be slow by simiones on Thursday December 07, 2023 @08:39AM (#38556126)
That's true. It's also important to note that this feature of C++ actually breaks encapsulation - the size of an object, including all of its private fields, private parent classes etc, is part of the public interface of the object in C++. So whenever you add a new private field to an object, you need to recompile all uses of your class, even if your class is used through a DLL.