Skip to content

Conversation

huacnlee
Copy link
Contributor

It won't change anything after they are first invoke, so add method cache to avoid allocations and avoid GC.

Benchmarks:

Calculating -------------------------------------
project_guest_rules without method cache
                        79.352k i/100ms
project_guest_rules with method cache
                        93.634k i/100ms
-------------------------------------------------
project_guest_rules without method cache
                          2.865M (±32.5%) i/s -     11.982M
project_guest_rules with method cache
                          4.419M (± 7.4%) i/s -     22.004M

Comparison:
project_guest_rules with method cache:  4418908.0 i/s
project_guest_rules without method cache:  2864514.0 i/s - 1.54x slower

Calculating -------------------------------------
project_report_rules without method cache
                        53.126k i/100ms
project_report_rules with method cache
                        97.473k i/100ms
-------------------------------------------------
project_report_rules without method cache
                          1.093M (±36.5%) i/s -      4.675M
project_report_rules with method cache
                          4.420M (± 7.2%) i/s -     22.029M
Comparison:
project_report_rules with method cache:  4420054.3 i/s
project_report_rules without method cache:  1092509.6 i/s - 4.05x slower

Benchark case:

https://gist.github.com/huacnlee/b04788ae6df42fe769e4

It won't change anything after they are first invoke, so add method cache to avoid allocations and avoid GC.

Benchmarks:

```
Calculating -------------------------------------
project_guest_rules without method cache
                        79.352k i/100ms
project_guest_rules with method cache
                        93.634k i/100ms
-------------------------------------------------
project_guest_rules without method cache
                          2.865M (±32.5%) i/s -     11.982M
project_guest_rules with method cache
                          4.419M (± 7.4%) i/s -     22.004M

Comparison:
project_guest_rules with method cache:  4418908.0 i/s
project_guest_rules without method cache:  2864514.0 i/s - 1.54x slower

Calculating -------------------------------------
project_report_rules without method cache
                        53.126k i/100ms
project_report_rules with method cache
                        97.473k i/100ms
-------------------------------------------------
project_report_rules without method cache
                          1.093M (±36.5%) i/s -      4.675M
project_report_rules with method cache
                          4.420M (± 7.2%) i/s -     22.029M
Comparison:
project_report_rules with method cache:  4420054.3 i/s
project_report_rules without method cache:  1092509.6 i/s - 4.05x slower
```

https://gist.github.com/huacnlee/b04788ae6df42fe769e4
@Razer6
Copy link
Member

Razer6 commented Dec 16, 2015

Awesome. @yorickpeterse @randx Can you take a look?

@yorickpeterse
Copy link
Contributor

Since all these methods return constant data, can we not just use a constant instead (and return said constant from the method)?

@huacnlee
Copy link
Contributor Author

@yorickpeterse That no difference but it will make code little complex and hard to read, method cache is simple.

class Ability
  class << self
    PROJECT_GUEST_RULES = [
      :read_project,
      :read_wiki,
      :read_issue,
      :read_label,
      :read_milestone,
      :read_project_snippet,
      :read_project_member,
      :read_merge_request,
      :read_note,
      :read_build,
      :create_project,
      :create_issue,
      :create_note
    ]

    ...

    def project_guest_rules
      PROJECT_GUEST_RULES
    end
  end
end

VS

class Ability
  class << self
    def project_guest_rules
      @project_guest_rules ||= [
        :read_project,
        :read_wiki,
        :read_issue,
        :read_label,
        :read_milestone,
        :read_project_snippet,
        :read_project_member,
        :read_merge_request,
        :read_note,
        :read_build,
        :create_project,
        :create_issue,
        :create_note
      ]
    end
  end
end

@yorickpeterse
Copy link
Contributor

Fair enough, I don't see any problems with using instance variables instead.

@dzaporozhets
Copy link
Contributor

Looks good. We will merge it a bit later (after 8.3.0.rc2)

@dzaporozhets dzaporozhets self-assigned this Dec 17, 2015
@huacnlee
Copy link
Contributor Author

@randx You looks like forgot this PR, version 8.3.0 have released

@dzaporozhets
Copy link
Contributor

@huacnlee good reminder. Lets merge it

dzaporozhets added a commit that referenced this pull request Dec 24, 2015
@dzaporozhets dzaporozhets merged commit 7caaa10 into gitlabhq:master Dec 24, 2015
@huacnlee
Copy link
Contributor Author

😄

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