Skip to content

Commit

Permalink
Avoid unnecessary FooService allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
mdempsky authored and willnorris committed Jun 29, 2016
1 parent 087a452 commit 8808be6
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 56 deletions.
4 changes: 1 addition & 3 deletions github/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ package github
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/activity/
type ActivityService struct {
client *Client
}
type ActivityService service

// FeedLink represents a link to a related resource.
type FeedLink struct {
Expand Down
4 changes: 1 addition & 3 deletions github/authorizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ const (
// an OAuth token.
//
// GitHub API docs: https://developer.github.com/v3/oauth_authorizations/
type AuthorizationsService struct {
client *Client
}
type AuthorizationsService service

// Authorization represents an individual GitHub authorization.
type Authorization struct {
Expand Down
4 changes: 1 addition & 3 deletions github/gists.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import (
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/gists/
type GistsService struct {
client *Client
}
type GistsService service

// Gist represents a GitHub's gist.
type Gist struct {
Expand Down
4 changes: 1 addition & 3 deletions github/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ package github
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/git/
type GitService struct {
client *Client
}
type GitService service
35 changes: 21 additions & 14 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ type Client struct {
rateLimits [categories]Rate // Rate limits for the client as determined by the most recent API calls.
mostRecent rateLimitCategory

common service // Reuse a single struct instead of allocating one for each service on the heap.

// Services used for talking to different parts of the GitHub API.
Activity *ActivityService
Authorizations *AuthorizationsService
Expand All @@ -125,6 +127,10 @@ type Client struct {
Reactions *ReactionsService
}

type service struct {
client *Client
}

// ListOptions specifies the optional parameters to various List methods that
// support pagination.
type ListOptions struct {
Expand Down Expand Up @@ -174,20 +180,21 @@ func NewClient(httpClient *http.Client) *Client {
uploadURL, _ := url.Parse(uploadBaseURL)

c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent, UploadURL: uploadURL}
c.Activity = &ActivityService{client: c}
c.Authorizations = &AuthorizationsService{client: c}
c.Gists = &GistsService{client: c}
c.Git = &GitService{client: c}
c.Gitignores = &GitignoresService{client: c}
c.Issues = &IssuesService{client: c}
c.Organizations = &OrganizationsService{client: c}
c.PullRequests = &PullRequestsService{client: c}
c.Repositories = &RepositoriesService{client: c}
c.Search = &SearchService{client: c}
c.Users = &UsersService{client: c}
c.Licenses = &LicensesService{client: c}
c.Migrations = &MigrationService{client: c}
c.Reactions = &ReactionsService{client: c}
c.common.client = c
c.Activity = (*ActivityService)(&c.common)
c.Authorizations = (*AuthorizationsService)(&c.common)
c.Gists = (*GistsService)(&c.common)
c.Git = (*GitService)(&c.common)
c.Gitignores = (*GitignoresService)(&c.common)
c.Issues = (*IssuesService)(&c.common)
c.Licenses = (*LicensesService)(&c.common)
c.Migrations = (*MigrationService)(&c.common)
c.Organizations = (*OrganizationsService)(&c.common)
c.PullRequests = (*PullRequestsService)(&c.common)
c.Reactions = (*ReactionsService)(&c.common)
c.Repositories = (*RepositoriesService)(&c.common)
c.Search = (*SearchService)(&c.common)
c.Users = (*UsersService)(&c.common)
return c
}

Expand Down
4 changes: 1 addition & 3 deletions github/gitignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import "fmt"
// GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/gitignore/
type GitignoresService struct {
client *Client
}
type GitignoresService service

// Gitignore represents a .gitignore file as returned by the GitHub API.
type Gitignore struct {
Expand Down
4 changes: 1 addition & 3 deletions github/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import (
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/issues/
type IssuesService struct {
client *Client
}
type IssuesService service

// Issue represents a GitHub issue on a repository.
type Issue struct {
Expand Down
4 changes: 1 addition & 3 deletions github/licenses.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import "fmt"
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/pulls/
type LicensesService struct {
client *Client
}
type LicensesService service

// License represents an open source license.
type License struct {
Expand Down
4 changes: 1 addition & 3 deletions github/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import (
// in the GitHub API.
//
// GitHub API docs: https://developer.github.com/v3/migration/
type MigrationService struct {
client *Client
}
type MigrationService service

// Migration represents a GitHub migration (archival).
type Migration struct {
Expand Down
4 changes: 1 addition & 3 deletions github/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import (
// in the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/orgs/
type OrganizationsService struct {
client *Client
}
type OrganizationsService service

// Organization represents a GitHub organization account.
type Organization struct {
Expand Down
4 changes: 1 addition & 3 deletions github/pulls.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import (
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/pulls/
type PullRequestsService struct {
client *Client
}
type PullRequestsService service

// PullRequest represents a GitHub pull request on a repository.
type PullRequest struct {
Expand Down
4 changes: 1 addition & 3 deletions github/reactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import "fmt"
// GitHub API.
//
// GitHub API docs: https://developer.github.com/v3/reactions/
type ReactionsService struct {
client *Client
}
type ReactionsService service

// Reaction represents a GitHub reaction.
type Reaction struct {
Expand Down
4 changes: 1 addition & 3 deletions github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import "fmt"
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/repos/
type RepositoriesService struct {
client *Client
}
type RepositoriesService service

// Repository represents a GitHub repository.
type Repository struct {
Expand Down
4 changes: 1 addition & 3 deletions github/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (
// in the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/search/
type SearchService struct {
client *Client
}
type SearchService service

// SearchOptions specifies optional parameters to the SearchService methods.
type SearchOptions struct {
Expand Down
4 changes: 1 addition & 3 deletions github/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import "fmt"
// methods of the GitHub API.
//
// GitHub API docs: http://developer.github.com/v3/users/
type UsersService struct {
client *Client
}
type UsersService service

// User represents a GitHub user.
type User struct {
Expand Down

0 comments on commit 8808be6

Please sign in to comment.