Smallest Useful Attribute System Part 2

Reader Alexey provided some useful feedback on The Smallest Useful Attribute System. The first point I want to address is the extra copies of strings. When I embedded the strings in the map and the AttributeDescriptor, I had been thinking that if the strings were part of a DLL, and the DLL got unloaded, there would be trouble. On further reflection, it occurred to me that I had over-thought it. If a DLL got unloaded, and then one attempted to fetch attributes on the now dead classes, there are bigger problems going on. So, here’s the fix to the string copies; we go from one static string built into the data and two dynamic copies to just the static string.

class AttributeBinder
{
...snip...
    class AttributeDescriptor
    {
...snip...
        AttributeDescriptor(const char* name, const std::type_info& ti, ptrdiff_t off) :
            name(name), ti(&ti), off(off) { }
        const char* name;
...snip...
    };

    class Predicate
    {
    public:
        bool operator()(char const*const& lhs, char const*const& rhs) const
        {
            return strcmp(lhs, rhs) < 0;
        }
    };

    std::map<const char*, AttributeDescriptor, Predicate> attribs;
};

The std::string has been replaced by const char*, and the map is supplied with an ordering predicate that does the comparisons from the name of the attribute.

Click to read the original post: The Smallest Useful Attribute System.

Post to Twitter Post to Delicious Post to Facebook

  • admin
    I agree with your point about the maps not changing, although I went down a different path - I have a new version almost ready to post. I ended up using a Ternary Search Tree, as described here:

    http://www.cs.princeton.edu/~rs/strings/

    It gets rid of string compares for lookup, so finding an attribute is really quick; search is on the order of the length of the string you're looking for, instead of log2(n) of the number of strings. Strcmp no longer shows up on my profile, which makes me very happy.
  • Once the binding is done, the map will never change, so I'd suggest a sorted vector instead. It's not a premature optimization, rather it's simplifying the code by making it explicit that once you've got things set up, it's just a lookup from then on. Although, [] is more readable than lower_bound and all that. It also gets rid of the redundancy where the name is stored in the attribute _and_ in the map as a key.
blog comments powered by Disqus

Bad Behavior has blocked 804 access attempts in the last 7 days.