Skip to content
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

Add intern.Table.Query #385

Merged
merged 2 commits into from
Dec 10, 2024
Merged

Add intern.Table.Query #385

merged 2 commits into from
Dec 10, 2024

Conversation

mcy
Copy link
Member

@mcy mcy commented Dec 6, 2024

Suppose we have m map[intern.ID]T and s string. We want to query if this string is in the map. We could write m[table.Intern(s)], but if s is not already interned, this is quite wasteful! Interning a never-before-seen string costs three map hits, plus the hit to m. However, if s is not interned, it cannot possibly be in m, so we can cut the cost of every lookup to one hit in Query and one in m[id].

In other words, this lets us write this function:

func get(tab intern.Table, m map[intern.ID]T, s string) (T, bool) {
  id, ok := tab.Query(s)
  if !ok { return T{}, false }
  
  v, ok := m[id]
  return v, ok
}

char6-inlined IDs are treated as always being interned, which means that for very small IDs, we only have to hit one map: the lookup in m[id].

@mcy mcy changed the title Add intern.Table.TryIntern Add intern.Table.TryIntern Dec 6, 2024
@mcy mcy requested a review from jhump December 6, 2024 21:44
internal/intern/intern.go Outdated Show resolved Hide resolved
@mcy mcy requested a review from jhump December 9, 2024 22:04
@mcy mcy changed the title Add intern.Table.TryIntern Add intern.Table.Query Dec 9, 2024
@mcy mcy merged commit 7a4632c into main Dec 10, 2024
10 checks passed
@mcy mcy deleted the mcy/try-intern branch December 10, 2024 19:09
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