Skip to content

Use sync.Pool for reclaiming unused posting.List #223

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

Closed
wants to merge 8 commits into from

Conversation

manishrjain
Copy link
Contributor

@manishrjain manishrjain commented Sep 19, 2016

We have to do this via reference counting because multiple goroutines might be holding the same pointer.


This change is Reviewable

}
if lp := getFromMap(gotomicKey); lp != nil {
return lp, lp.decr
} else {

Choose a reason for hiding this comment

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

if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)

l.init(key, pstore)
return l, l.decr

} else {

Choose a reason for hiding this comment

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

if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.4%) to 54.162% when pulling ff44edf on feature/plist into 1f751a1 on master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 54.183% when pulling ef80525 on feature/plist into 1f751a1 on master.

@manishrjain
Copy link
Contributor Author

Review status: 0 of 12 files reviewed at latest revision, 2 unresolved discussions.


posting/lists.go, line 300 at r1 (raw file):

Previously, houndci-bot (Hound) wrote…

if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)

Done.

posting/lists.go, line 307 at r1 (raw file):

Previously, houndci-bot (Hound) wrote…

if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)

Done.

Comments from Reviewable

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 54.196% when pulling 6799fd8 on feature/plist into 1f751a1 on master.

@jchiu0
Copy link
Contributor

jchiu0 commented Sep 20, 2016

:lgtm: (just some minor remarks. didn't find anything very wrong. Good that decr is internal and you return it.)


Review status: 0 of 12 files reviewed at latest revision, 10 unresolved discussions, some commit checks broke.


posting/list.go, line 85 at r2 (raw file):

  return atomic.AddInt32(&l.refcount, 1)
}
func (l *List) decr() {

Nit: Missing empty line


posting/list.go, line 90 at r2 (raw file):

      return
  }
  if val < 0 {

x.Assertf(val < 0, "List reference ....: %v", val)


posting/lists.go, line 277 at r2 (raw file):

}

// GetOrCreate stores the List corresponding to key(if its not there already)

nit: (if it's not there already)


posting/lists.go, line 294 at r2 (raw file):

  {
      l := getNew() // This retrieves a new *List and increments it's ref count.

spelling: "its"


posting/lists.go, line 296 at r2 (raw file):

      l := getNew() // This retrieves a new *List and increments it's ref count.
      if inserted := lhmap.PutIfMissing(gotomicKey, l); inserted {
          l.incr() // Prepare this for return to caller.

Maybe add comment: Increment counter due to reference from lhmap?


posting/lists.go, line 301 at r2 (raw file):

      }
      // If we're unable to insert this, decrement the reference count.

Maybe add comment: decr is to undo the incr in newList call?


posting/lists.go, line 307 at r2 (raw file):

errors
Can avoid import of "errors" with x.Errorf. Or maybe x.Check(x.Errorf("Key should be present %v", key))


posting/lists.go, line 326 at r2 (raw file):

func processOne(k gotomic.Hashable, c *counters) {
  ret, _ := lhmap.Delete(k)

Maybe add comment at Delete or here saying that Delete returns a reference to the deleted value, so it doesn't call l.decr?


Comments from Reviewable

@manishrjain
Copy link
Contributor Author

Review status: 0 of 12 files reviewed at latest revision, 10 unresolved discussions, some commit checks broke.


posting/list.go, line 85 at r2 (raw file):

Previously, jchiu0 (Jay Chiu) wrote…

Nit: Missing empty line

Actually grouping them together a bunch of small connected functions.

posting/list.go, line 90 at r2 (raw file):

Previously, jchiu0 (Jay Chiu) wrote…

x.Assertf(val < 0, "List reference ....: %v", val)

Done.

posting/lists.go, line 277 at r2 (raw file):

Previously, jchiu0 (Jay Chiu) wrote…

nit: (if it's not there already)

Done.

posting/lists.go, line 296 at r2 (raw file):

Previously, jchiu0 (Jay Chiu) wrote…

Maybe add comment: Increment counter due to reference from lhmap?

Done. It's the increment for the caller, because we already did the increment for the map in getNew().

posting/lists.go, line 301 at r2 (raw file):

Previously, jchiu0 (Jay Chiu) wrote…

Maybe add comment: decr is to undo the incr in newList call?

Done.

posting/lists.go, line 307 at r2 (raw file):

Previously, jchiu0 (Jay Chiu) wrote…

errors
Can avoid import of "errors" with x.Errorf. Or maybe x.Check(x.Errorf("Key should be present %v", key))

Done. Used x.Assertf(..) which gives what I needed.

posting/lists.go, line 326 at r2 (raw file):

Previously, jchiu0 (Jay Chiu) wrote…

Maybe add comment at Delete or here saying that Delete returns a reference to the deleted value, so it doesn't call l.decr?

It does call l.decr() below. The value is just removed from the map, not really deleted. Every value in map has a ref count of at least 1. So, when we remove from map, we decrement that ref count, to allow it to be put back into pool.

Comments from Reviewable

@coveralls
Copy link

Coverage Status

Coverage decreased (-24.7%) to 29.801% when pulling 4a4061b on feature/plist into 1f751a1 on master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.2%) to 54.342% when pulling 6ea5cc3 on feature/plist into 1f751a1 on master.

@manishrjain manishrjain deleted the feature/plist branch September 20, 2016 04:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants