Skip to content

Code and style improvements #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 5, 2023

Conversation

flrdv
Copy link
Contributor

@flrdv flrdv commented Nov 8, 2022

  • renamed docstrings describing Set[T] interface methods to be compatible with a community styleguide - to start with a name of actual method. This also allows us to generate documentation more effectively (because go doc by default ignores docstrings that are not starting with an object name)
  • in unsafe implementation of set, removed pointer receivers, because unsafe set is already a pointer - because it is a map, and map is a pointer-type, and passing it by value anyway allows us to mutate the origin destination
    • also updated Clear() method. It now REALLY clears the set instead of allocating a new one. All the details are provided in the description of the last one commit

flrdv added 4 commits November 8, 2022 21:55
updated docstrings describing Set interface methods to start with a method name for a correct documentation generation (and avoiding tons of warnings in goland)
now constructors returns by value instead of by reference a newly constructed sets
renamed method receivers to be a single-letter equal to a first letter in object's name, as community recommends
`threadUnsafeSet` is already a map that is a reference-type, so passing it by-value still lets us mutate an origin object, but decreases a number of pointers, which in turn, decreases a GC-pressure, (potentially) makes less useless operations and (definitely) makes code better to read/understand.

Another point of the change is to a bit "upgrade" a `Clear()` method. In the old implementation, it simply constructed a new `threadUnsafeSet` and did mutate an origin pointer to point at our new set. This commits an extra allocation (I would expect a `Clear()` method to be allocations-free), and fallbacks already allocated map's size to the default one. So I replaced this with clearing itself using `mapclear()` function (that implicitly replaces `for key := range d { delete(d, key) }`). This is actually pretty expensive operation, but does no allocations, that is maybe even cheaper than vice-versa. Anyway user always can construct a new instance of the set by itself, but cannot clear underlying map manually in case he really needs it - to decrease amount of memory used in average, number of which is actually affected a lot by internally allocating a new map
@deckarep
Copy link
Owner

@fakefloordiv - thanks for the changes I’ll be reviewing this in the coming days.

Overall looks like some nice cleanup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants