Skip to content

rework hashmap_clear() - #11191

Merged
poettering merged 6 commits into
systemd:masterfrom
poettering:hashmap-clear
Dec 18, 2018
Merged

poettering merged 6 commits into
systemd:masterfrom
poettering:hashmap-clear

Conversation

@poettering

Copy link
Copy Markdown
Member

A fix for #11170, an alternative to #11163.

This includes @filbranden's test case from #11163.

@filbranden

Copy link
Copy Markdown
Contributor

I was thinking about this one earlier...

What happens if the destructor doesn't remove the entry from the hashmap? In that case, won't internal_hashmap_first_key_and_value() keep returning the same key/value repeatedly?

I think we should have hashmap testcases for this function with the destructors, both when the destructor removes the entry from the hashmap and when it doesn't... That would ensure the correctness of this case and that regressions wouldn't slip in here. (I can write the testcases, but probably would take me a couple of days to get to that...)

Cheers,
Filipe

Comment thread src/basic/hashmap.c Outdated

@keszybz keszybz Dec 18, 2018 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"If" → "In case"? It sounds like you wanted to keep the code path for the case with no destructor helpers.
nvm.

@keszybz

keszybz commented Dec 18, 2018 •

Copy link
Copy Markdown
Member

What happens if the destructor doesn't remove the entry from the hashmap? In that case, won't internal_hashmap_first_key_and_value() keep returning the same key/value repeatedly?

That would be a bug in the destructor. The destructor cannot leave a point to the object after destroying it. And leaving the reference in the hashmap would be just that.
internal_hashmap_first_key_and_value() is called with remove=true, so it always removes the key.

--

The change to require destruction helper to accept that the object is not in the hashmap is generally good, I think. Destructors have to be prepared to do that anyway, to be able to work on partially-constructed objects. So yeah, I think this patch is the way to go.

@poettering

Copy link
Copy Markdown
Member Author

What happens if the destructor doesn't remove the entry from the hashmap? In that case, won't internal_hashmap_first_key_and_value() keep returning the same key/value repeatedly?

It wouldn't. Note that we specify the second argument as 'true', which means the item is also removed when queried. Hence, it doesn't matter whether the destructors remove the item or don't, as we'll already have removed the thing by the time they get called. What is crucial though is that they don't choke if the object already got removed in the destructor.

poettering and others added 3 commits December 18, 2018 11:28
Let's first remove an item from the hashmap and only then destroy it.
This makes sure that destructor functions can mdoify the hashtables in
their own codee and we won't be confused by that.
…ghbors

In particular, check that the order of the results is consistent.

This test coverage will be useful in order to refactor the compare_func
used while sorting the results.

When introduced, this test also uncovered a memory leak in sd_lldp_stop(),
which was then fixed by a separate commit using a specialized function
as destructor of the LLDP Hashmap.

Tested:
  $ ninja -C build/ test
  $ valgrind --leak-check=full build/test-lldp
@poettering poettering added this to the v240 milestone Dec 18, 2018
This makes it slightly easier to watch for performance changes.
The point here is to compare speed of hashmap_destroy with free and a different
freeing function, to the implementation details of hashmap_clear can be
evaluated.

Results:
current code:

/* test_hashmap_free (slow, 1048576 entries) */
string_hash_ops test took 2.494499s
custom_free_hash_ops test took 2.640449s

string_hash_ops test took 2.287734s
custom_free_hash_ops test took 2.557632s

string_hash_ops test took 2.299791s
custom_free_hash_ops test took 2.586975s

string_hash_ops test took 2.314099s
custom_free_hash_ops test took 2.589327s

string_hash_ops test took 2.319137s
custom_free_hash_ops test took 2.584038s

code with a patch which restores the "fast path" using:
    for (idx = skip_free_buckets(h, 0); idx != IDX_NIL; idx = skip_free_buckets(h, idx + 1))
in the case where both free_key and free_value are either free or NULL:

/* test_hashmap_free (slow, 1048576 entries) */
string_hash_ops test took 2.347013s
custom_free_hash_ops test took 2.585104s

string_hash_ops test took 2.311583s
custom_free_hash_ops test took 2.578388s

string_hash_ops test took 2.283658s
custom_free_hash_ops test took 2.621675s

string_hash_ops test took 2.334675s
custom_free_hash_ops test took 2.601568s

So the test is noisy, but there clearly is no significant difference with the
"fast path" restored. I'm surprised by this, but it shows that the current
"safe" implementation does not cause a performance loss.

When the code is compiled with optimization, those times are significantly
lower (e.g. 1.1s and 1.4s), but again, there is no difference with the "fast
path" restored.

The difference between string_hash_ops and custom_free_hash_ops is the
additional cost of global modification and the extra function call.
@keszybz

keszybz commented Dec 18, 2018

Copy link
Copy Markdown
Member

Ah, I see that I cross-posted with @poettering.

I pushed a few commits to add an additional test/benchmark function.

@keszybz keszybz added the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label Dec 18, 2018
@poettering
poettering merged commit 6b25662 into systemd:master Dec 18, 2018
@keszybz keszybz removed the good-to-merge/waiting-for-ci 👍 PR is good to merge, but CI hasn't passed at time of review. Please merge if you see CI has passed label Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants