Skip to content

Conversation

@laines-it
Copy link
Collaborator

@laines-it laines-it commented Dec 2, 2025

This commit reduces allocations.

Future.done allocation replaced with

  • Future.cond (sync.Cond)
  • Future.finished (atomic.Bool)

Other code use Future.isDone() instead Future.done == nil check.

Added Future.finish() marks Future as done.
Future.WaitChan() now creates channel on demand.

Closes #496

@laines-it laines-it force-pushed the laines-it/gh-496-replace-Future-chan-cond-Sync branch from dd9e78b to f5ce75f Compare December 2, 2025 14:34
@laines-it laines-it marked this pull request as draft December 2, 2025 14:35
@laines-it laines-it marked this pull request as ready for review December 2, 2025 14:41
Copy link
Collaborator

@oleg-jukovec oleg-jukovec left a comment

Choose a reason for hiding this comment

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

Thanks for the patch. Overall, everything's fine.

I've added some implementation notes, but you need to do a proper rebase first.

@oleg-jukovec
Copy link
Collaborator

Please, rebase on the master branch.

@laines-it laines-it force-pushed the laines-it/gh-496-replace-Future-chan-cond-Sync branch from f5ce75f to d238853 Compare December 6, 2025 07:52
Copy link
Collaborator

@oleg-jukovec oleg-jukovec left a comment

Choose a reason for hiding this comment

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

Thank you for the patch! I have just a one mirror comment:

@laines-it laines-it force-pushed the laines-it/gh-496-replace-Future-chan-cond-Sync branch from d238853 to 948fcee Compare December 7, 2025 08:16
Copy link
Collaborator

@oleg-jukovec oleg-jukovec left a comment

Choose a reason for hiding this comment

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

Thank you for the patch!

future.go Outdated
resp Response
err error
cond sync.Cond
finished atomic.Bool
Copy link
Collaborator

Choose a reason for hiding this comment

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

Right now atomic.finished is called under lock everywhere (except isDone), that is once or twice called without it. It's better to replace with simple bool calling it with lock, than making it atomic (that will invalidate cache record on every access to this variable).

Copy link
Collaborator Author

@laines-it laines-it Dec 9, 2025

Choose a reason for hiding this comment

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

Done. Thanks for recommendations! I wrapped finished in connection.go like:

fut.mutex.Lock()
is_done := fut.finished
fut.mutex.Unlock()

if is_done {
   ...
   return
}

future.go Outdated
Comment on lines 53 to 55
fut.done = nil
fut.finished.Store(false)
fut.cond = *sync.NewCond(&fut.mutex)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This kind of initialization is allowed (and preferred) when sync.Cond is stored by value rather than as a pointer:

Suggested change
fut.done = nil
fut.finished.Store(false)
fut.cond = *sync.NewCond(&fut.mutex)
fut.done = nil
fut.finished.Store(false)
fut.cond.L = &fut.Mutex

Moreover, it generates less code, is more performant, and helps the Go escape analyzer understand that the allocated memory does not escape (not that the analyzer couldn’t deduce this on its own, but the hint certainly doesn’t hurt).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

@laines-it laines-it force-pushed the laines-it/gh-496-replace-Future-chan-cond-Sync branch from 948fcee to da9ec89 Compare December 9, 2025 20:18
This commit reduces allocations.

Future.done allocation replaced with
- Future.cond (sync.Cond)
- Future.finished (bool)

Other code use `Future.finished` instead `Future.done == nil` check.

Added Future.finish() marks Future as done.
Future.WaitChan() now creates channel on demand.

Closes #496
@laines-it laines-it force-pushed the laines-it/gh-496-replace-Future-chan-cond-Sync branch from da9ec89 to 364deec Compare December 9, 2025 20:53
@oleg-jukovec oleg-jukovec requested a review from bigbes December 9, 2025 23:13
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.

v3: replace channel in Future with a cond.Sync

4 participants