Settings

Theme

Show HN: C++11 immutable string

github.com

23 points by syvex 12 years ago · 6 comments · 1 min read

Reader

I'm looking for feedback on this approach to an immutable string (istring). The idea is to provide a super fast immutable string that can be efficiently passed to lambdas as well as be 100% thread safe.

Another bonus is faster hashing. If a string literal is given to istring, then the hash is computed at compile time! Other strings will have their hashes cached for later use. The istring is designed more or less after the python string in this regard.

It should also consume less memory than std::string and perform faster in all other areas.

Nican 12 years ago

Just as a note, on Java 7, developers opted for not having a single buffer backing multiple strings due to the backing references never been deleted. [1] [2]

[1] http://stackoverflow.com/questions/16123446/java-7-string-su...

[2] http://www.reddit.com/r/programming/comments/1qw73v/til_orac...

  • nikbackm 12 years ago

    Probably more of an issue when all of your strings work(ed) like that, as opposed to here where you must explicitly opt-in to use immutable strings with shared buffers.

thegeomaster 12 years ago

Great job. I like it how peole are moving to and developing for C++11 as I think it's a huge step forward in the standard. The one cosmetic complaint I have is that the slicing syntax is not verbose enough for my taste. Also, what hash algo did you use? Asking just out of curiosity. I remember running a benchmark a long ago of different non-cryptographic hashes. The collision resistance was pretty consistent across all of them, but some were really slow and some were really fast, and that could directly affect the performance on hashtables and other structures that rely on hashes.

  • syvexOP 12 years ago

    The hash is FNV-1a, which should be faster that what you usually get in your standard library. It's also relatively easy to implement with constexpr so that you get those nice compile time hashes.

    http://www.isthe.com/chongo/tech/comp/fnv/

  • syvexOP 12 years ago

    I can't say I'm 100% happy with the slicing notation. I played around with some other wacky things, but then settled on overloading operator() to get a concise syntax.

    In principle, you use it similar to python--s(5,10) instead of s[5:10].

    I would have maybe liked to end up with s[5,10], but the only way to overload operator[] like that is with a pair. Then you'd end up with s[{5,10}]. So I figured it would be best to stick with just s(5,10).

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection