Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
0c14042a
Commit
0c14042a
authored
Aug 01, 2018
by
Stan Hu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rework performance section to allow for string freezing
Relates to #47424
parent
93c7b6c5
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
23 deletions
+31
-23
doc/development/performance.md
doc/development/performance.md
+31
-23
No files found.
doc/development/performance.md
View file @
0c14042a
...
...
@@ -347,13 +347,7 @@ def expire_first_branch_cache
end
```
## Anti-Patterns
This is a collection of
[
anti-patterns
][
anti-pattern
]
that should be avoided
unless these changes have a measurable, significant and positive impact on
production environments.
### String Freezing
## String Freezing
In recent Ruby versions calling
`freeze`
on a String leads to it being allocated
only once and re-used. For example, on Ruby 2.3 this will only allocate the
...
...
@@ -365,17 +359,38 @@ only once and re-used. For example, on Ruby 2.3 this will only allocate the
end
```
Blindly adding a
`.freeze`
call to every String is an anti-pattern that should
be avoided unless one can prove (using production data) the call actually has a
positive impact on performance.
Depending on the size of the String and how frequently it would be allocated
(before the
`.freeze`
call was added), this _may_ make things faster, but
there's no guarantee it will.
Strings will be frozen by default in Ruby 3.0. To prepare our code base for
this eventuality, it's a good practice to add the following header to all
Ruby files:
```
ruby
# frozen_string_literal: true
```
This may cause test failures in the code that expects to be able to manipulate
strings. Instead of using
`dup`
, use the unary plus to get an unfrozen string:
```
ruby
test
=
+
"hello"
test
+=
" world"
```
## Anti-Patterns
This feature of Ruby wasn't really meant to make things faster directly, instead
it was meant to reduce the number of allocations. Depending on the size of the
String and how frequently it would be allocated (before the
`.freeze`
call was
added), this _may_ make things faster, but there's no guarantee it will.
This is a collection of
[
anti-patterns
][
anti-pattern
]
that should be avoided
unless these changes have a measurable, significant and positive impact on
production environments.
Another common flavour of this is to not only freeze a String, but also assign
it to a constant, for example:
### Moving Allocations to Constants
Storing an object as a constant so you only allocate it once _may_ improve
performance, but there's no guarantee this will. Looking up constants has an
impact on runtime performance, and as such, using a constant instead of
referencing an object directly may even slow code down. For example:
```
ruby
SOME_CONSTANT
=
'foo'
.
freeze
...
...
@@ -393,13 +408,6 @@ there's nothing stopping somebody from doing this elsewhere in the code:
SOME_CONSTANT
=
'bar'
```
### Moving Allocations to Constants
Storing an object as a constant so you only allocate it once _may_ improve
performance, but there's no guarantee this will. Looking up constants has an
impact on runtime performance, and as such, using a constant instead of
referencing an object directly may even slow code down.
[
#15607
]:
https://gitlab.com/gitlab-org/gitlab-ce/issues/15607
[
yorickpeterse
]:
https://gitlab.com/yorickpeterse
[
anti-pattern
]:
https://en.wikipedia.org/wiki/Anti-pattern
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment