forked from github/mime-types
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use a value pool for attributes on MIME::Type instances
- Loading branch information
Charlie Somerville
committed
Jun 5, 2013
1 parent
0d57fcc
commit ca0bce0
Showing
3 changed files
with
85 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require "mime/value_pool" | ||
|
||
module MIME | ||
module PooledAttrAccessor | ||
private | ||
def pooled_attr_accessor(sym, opts = {}) | ||
attr_reader sym | ||
ivar = :"@#{sym}" | ||
writer = :"#{sym}=" | ||
define_method writer do |val| | ||
instance_variable_set(ivar, ValuePool[val]) | ||
end | ||
private writer if opts[:private_writer] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module MIME | ||
ValuePool = Hash.new { |h,k| | ||
begin | ||
k = k.dup | ||
rescue TypeError | ||
else | ||
k.freeze | ||
end | ||
|
||
h[k] = k | ||
} | ||
end |
ca0bce0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Howdy, i've been talking on mime-types#94 about ways to decrease memory footprint in the mime/types library and this optimization came up. If you've got a second or two would you mind sharing at a high level what's going on here and why it helps?
It looks like the idea is to use references to objects rather than instantiating new objects whenever possible which seems good. Anything else i'm missing? Anything preventing this from wholesale being merged upstream?
ca0bce0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was @charliesome's work ... my idea was just to use a database like gdbm which would be so much saner
0cf4b1b