Skip to content

fix: optimize Set by using struct{} instead of bool for memory efficiency #782

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

WolfieLeader
Copy link

Description

This PR optimizes the Set implementation by replacing the bool value with an empty struct (struct{}). This significantly reduces memory usage, especially for large data sets.

Motivation

The Set previously used a map[T]bool, where each value occupied at least one byte. Since the actual value in a Set is irrelevant (presence of a key implies membership), this value can be replaced with an empty struct (struct{}), which has zero memory allocation in Go.

This change enhances memory efficiency without affecting functionality or performance negatively.

Changes

  • Replaced map[T]bool with map[T]struct{} for Set implementation.

Checklist

  • Code compiles and passes tests.
  • Implementation is idiomatic and conforms to Go best practices.
  • Changes are isolated to a single logical unit.
  • File naming and formatting follow repository guidelines.
  • Added or updated tests as necessary.

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.

1 participant