Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIHugging Face Arena Human Preference 55K

struct atomicunorderedmap { ~atomicunorderedmap() { } _forceinline bool contains(int…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-pro-dev-api’s answer.
  1. User

    struct atomic_unordered_map { ~atomic_unordered_map() { } __forceinline bool contains(int val) { if (table.index(val, false) != NULL) if (table.index(val, false).load(std::memory_order_relaxed) != true) return !table.index(val, false).load(std::memory_order_relaxed); return false; } __forceinline void emplace(int val) { if (table.index(val, false) != NULL) if (table.index(val, false).load(std::memory_order_relaxed) != true) table.index(val, true).store(new std::atomic_bool(false)); } __forceinline void erase(int val) { if (table.index(val, false) != NULL) { auto n = table.index(val, false); bool&& e = n.load(std::memory_order_relaxed); if (n.compare_exchange_strong(e, true, std::memory_order_acquire)) n = NULL; { } } } _declspec(noinline) void reserve(uint size) { table.reserve(size); } private: struct jump_table { std::atomic_bool index(int index, bool op) noexcept { if (op && (*data)[index] == NULL) { std::atomic_bool a = false; (*data)[index].store(a); } return &(*data)[index]; } declspec(noinline) void reserve(uint size) { size.fetch_add(size, std::memory_order_relaxed); data = std::make_uniquestd::atomic_bool*(new std::atomic_bool[size]); } private: std::unique_ptrstd::atomic_bool* data = nullptr; std::atomic_int size = 0; }; jump_table table; }; would this code waste memory for the indexes between 2 and 84 after I index 2 and 84?

  2. Assistant

    No, this code does not waste memory for the indexes between 2 and 84 after indexing 2 and 84.

    In the jump_table struct, the reserve function increases the size of the data array to accommodate the specified size. However, it does not allocate memory for each index individually. Instead, it increments the size variable and assigns a new std::atomic_bool* array of the specified size using std::make_unique.

    Therefore, if you only index 2 and 84, the memory allocated will only be for those two indexes, and the memory for the other indexes in between will not be wasted.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.