-
Notifications
You must be signed in to change notification settings - Fork 121
add get_by_view
method to Map
#2082
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
base: main
Are you sure you want to change the base?
Conversation
Potential hash collision handling issue in get_by_viewCategory Test coverage could be improvedCategory
view function called on each comparison during probingCategory |
Pull Request Test Coverage Report for Build 6660Details
💛 - Coveralls |
8b22d17
to
0a0400a
Compare
This is one of the case which show generic params of traits is useful. |
0a0400a
to
c0a033e
Compare
this may be somthing easier to understand :
Then we can build something safe on top of it, is it a premature optimization? |
Is this a user-facing API, or just an internal helper? In terms of implementation, |
This PR adds a new method
get_by_view
toMap
. The motivation is the need for querying aMap[String, V]
with@string.View
. Converting the view toString
first requires a copy, storing@string.View
directly in the map may result in memory leak. So a new API is necessary for this case.The signature of the new API is as follows:
The map will be searched with the hash code of
key
. When comparing equality, existing keys in the map will be first converted toView
usingview
.Note that for the
@string.View
case, a specialized method may be adequate. But there are other cases such as@bytes.View
. So a generic implementation is used here. Theview
function will only be called when comparing keys with identical hash code, so it will not be invoked a lot. Therefore even ifget_by_view
is not inlined, the indirect call cost ofview
should be acceptable (since usingget_by_view
implies that the key is probably a large type with non-trivial comparison, such asString
)