efficient / libcuckoo

A high-performance, concurrent hash table
Other
1.61k stars 275 forks source link

How can I use operator [] ? #66

Closed Michael-Tieying-Zhang closed 7 years ago

Michael-Tieying-Zhang commented 7 years ago

It seems there is no operator [] in current libcuckoo. But our code uses [] that is supported in the old version of libcuckoo. How can I keep my code unchanged if I use current version of libcuckoo?

manugoyal commented 7 years ago

Hey so we decided to deprecate that feature, since the semantics were kind of messy, not optimally performant in general, and encouraged non-threadsafe patterns. Most usages of operator[] should map directly to existing functions. For example, if operator[] is used as an rvalue, find should suffice. If used as an lvalue, upset should suffice. Refactoring this way may actually increase the performance of your code, too.

Michael-Tieying-Zhang commented 7 years ago

If used as an lvalue, upset should suffice.

I guess you mean upsert?

For example, I have a cuckoomaptbl<int, class* pointer>, and originally I did like this: tbl[5] = pointer;

Now I should do like this:

auto fn = [pointer](class *&p) {
   p = pointer;
};
tbl.upsert(id, fn, pointer);

It's too messy for the code.

manugoyal commented 7 years ago

Well I guess it really depends on what you're trying to do. If you are sure the key isn't already in the table, you can do an insert, or if you're sure the key already is in the table, you can do an update. Both of those are one liners.

I agree that upsert in the case you're trying to do is a bit clumsy. But the bracket operator syntax does basically the same thing, but in a less flexible and performant way than upset. Perhaps you could define a helper function that takes in a table reference, key, and value, and does the upset the way you want?

Michael-Tieying-Zhang commented 7 years ago

does the upset the way you want

Upset or upsert? There is no upset API, I guess.

manugoyal commented 7 years ago

Yeah sorry i think my phone keeps autocorrecting. It's upsert

Michael-Tieying-Zhang commented 7 years ago

Alright. I think you can close this issue for now. If I have questions, I will re-open it. Thanks!

manugoyal commented 7 years ago

Cool! Seems like C++17 has an insert_or_assign function in their API that may be worth emulating in libcuckoo.

manugoyal commented 7 years ago

Just added insert_or_assign in 8d247ab97b228565c08be16b763d8cb861d147b4. This should have the functionality you described.

Michael-Tieying-Zhang commented 7 years ago

@manugoyal Thanks!