Skip to main content
GameDev.net gamedev.net
🔒 Locked

I've learnt my lesson

Started by Sc4Freak Oct 15, 2007 at 7:36 AM 4 replies 1.5k views
Original Post
Sc4Freak
Sc4Freak
I, like many others, had previously set out to write my own custom version of std::vector. I was initially happy with the result; it was a container that routinely exceeded the performance of std::vector by sacrificing contiguity for speed. For a while, I continued to use this class in my code with an air of blissful ignorance. Then came the time where I needed to fix some things that I had previously forgotten to implement. Things like copy constructors, comparison operators, assignment, and the like. "OK", I said, "That's not too hard". I implemented them and continued on with the rest of my program, fixing bugs as they popped up. Later, as I began to use std algorithms more, I found that I hadn't implemented iterators for my container, so it wasn't STL compatible at all. After attempting to implement custom iterators (and failing miserably), I discovered that my code wasn't exception safe, either. I found a lot of potentials for memory leaks and lost data if an exception occurred. I then realised that whatever tiny performance benefit this class could give me was being heavily outweighed by all its problems. I found myself constantly looking through the std::vector code in an attempt to understand (and copy) it, which I realised was quite a silly endeavour. I've switched it all over to std::vector, now, and everything's working wonderfully. I've learnt my lesson now, and I'm not going to attempt to reinvent the wheel "just because I can". For anyone else that is thinking of implementing their own version of an existing STL container/algorithm/function or reinventing the wheel, then I can only warn you to stop and consider the costs and benefits. The STL is well designed, guaranteed to work, and still relatively fast. From my experience here, it's not worth the work, effort and time to implement your own version of the STL unless you have a really, really good reason for it.
Antheus
Antheus
Quote:
and I'm not going to attempt to reinvent the wheel "just because I can"


The reason it usually doesn't work out is the cost. Creating a general purpose class is hard, and takes a lot of work and time.

It's not until you try to do it yourself and fail that you understand why using "slow" and "inefficient" and "unoptimized" standard library will usually be a better choice.

A good follow-up lesson is abstracting and data encapsulation, so that your API does not become too dependent on specific classes.
Quote:
I then realised that whatever tiny performance benefit this class
This isn't necessarily true. Custom data structures can give huge performance benefits. But almost always, the algorithmic solution can be implemented using existing classes and facilities (reserving vector size, custom allocator, changing insertion behavior, ...)
Wyrframe
Wyrframe
Quote:
Custom data structures can give huge performance benefits.


Indeed. If you need to store or map using a crapload of relatively short strings for your key, like an english-language dictionary, std::map (which is usually a red/black tree) just plain won't cut it; you've got to implement a Trie.

If you ever do implement a Trie, however, be sure to make it std:: compatible... which mostly just means templating it to accept any iterable container for a key, instead of over just std::string or std::list.
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.
JohnBolton
JohnBolton
Quote:
Original post by Sc4Freak
I found myself constantly looking through the std::vector code in an attempt to understand (and copy) it, which I realised was quite a silly endeavour.

That's funny. It's also a common experience for wheel inventors.

Quote:
Original post by Sc4Freak
For anyone else that is thinking of implementing their own version of an existing STL container/algorithm/function or reinventing the wheel, then I can only warn you to stop and consider the costs and benefits.

While reinventing a standard container is a generally waste of time, the library does provide a lot of framework for implementing your own specialized container. The trick is to make sure that your container meets the requirements so that it is compatible with the rest of the standard library.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
Trillian
Trillian
Good to hear that! I'm quite like you, I understood how nice was the stl a couple of months ago after a year running on my own classes. Creating and debbugging them was a lot of fun though. I don't regret having done all that, maybe it'd have been better if I hadn't done it for so long but it is a good experience. Hell, I'm even starting to use some parts of boost in my code (smart_ptr is awesome)!

I wouldn't necessarly recommand against rewriting some stl functionnality because :
1- You learn a lot
2- Ironically, you get to know more the stl's inner workings by copying them
3- You learn WHY such thing is done in such way
4- You learn the advantages of coe reuse
However, all of this is a big trap : you can remain stubborn for quite a long time before realizing why you should be using stl. It has both good and bad sides.

Same thing for the "Write games not engines" thing, I spent one or two years writing only engins with occasionnal "tech demoes". I've really, really learned a lot (OOP design) but now I understood and I'm currently working on a real GAME, with rapid progress.

The moral is : You can (should?) make mistakes, but be sure to learn something out of them!! =)
iMalc
iMalc
Great to hear![smile]

Now we have something to refer people to next time[cool]
Though sadly the best way to learn these things really is through your own experience.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.