slim-attributes 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT_LICENCE +20 -0
- data/README +47 -0
- data/ext/extconf.rb +26 -0
- data/ext/slim_attrib_ext.c +141 -0
- data/lib/slim_attributes.rb +40 -0
- metadata +50 -0
data/MIT_LICENCE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Stephen Sykes
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
SlimAttributes
|
2
|
+
==============
|
3
|
+
|
4
|
+
This is a small patch to the ActiveRecord Mysql adaptor that stops rails from using the all_hashes / each_hash mechanism - which is what is called when you do a find.
|
5
|
+
|
6
|
+
It is faster, and uses less memory.
|
7
|
+
|
8
|
+
Measuring with just ActiveRecord code - fetching stuff from the database - we see anything up to a 50% (or more) speed increase, but I suppose it really depends on your system and environment, and what you are doing with the results from the database. Measure your own system and send me the results!
|
9
|
+
|
10
|
+
|
11
|
+
Installation
|
12
|
+
============
|
13
|
+
|
14
|
+
You're going to need the mysql headers for this to work.
|
15
|
+
|
16
|
+
cd vendor/plugins/slim_attributes
|
17
|
+
ruby extconf.rb --with-mysql-config
|
18
|
+
make
|
19
|
+
sudo make install
|
20
|
+
|
21
|
+
|
22
|
+
Description
|
23
|
+
===========
|
24
|
+
|
25
|
+
The reason for overriding all_hashes is threefold:
|
26
|
+
|
27
|
+
* making a hash of each and every row returned from the database is slow
|
28
|
+
* ruby makes frozen copies of each column name string (for the keys) which results in a great many strings which are not really needed
|
29
|
+
* we observe that it's not often that all the fields of rows fetched from the database are actually used
|
30
|
+
|
31
|
+
So this is an alternative implementation of all_hashes that returns a 'fake hash' which contains a hash of the column names (the same hash of names is used for every row), and also contains the row data in an area memcpy'd directly from the mysql API.
|
32
|
+
|
33
|
+
The field contents are then instantiated into Ruby strings on demand - ruby strings are only made if you need them. Note that if you always look at all the columns when you fetch data from the database then this won't necessarily be faster that the unpatched mysql adapter. But it won't be much slower either, and we do expect that most times not all the columns from a result set are accessed.
|
34
|
+
|
35
|
+
Note that the 'fake hash' quacks like a hash in many ways, but not all ways. So @attributes in an ActiveRecord object may not behave as you are expecting it to, and it particularly won't work if you try to add a key to it that is not a column name in the result set.
|
36
|
+
|
37
|
+
@attributes["not a column name"] = "something"
|
38
|
+
=> RuntimeError: Key was not a column name from the result set
|
39
|
+
|
40
|
+
Hash has many methods that are not supported by the fake hash, but I found that the ones I have implemented have been sufficient for use in our Rails app. It should be fairly easy to implement most of the missing methods if needed, but I did not wish this patch to be larger than necessary.
|
41
|
+
|
42
|
+
===========
|
43
|
+
|
44
|
+
No warranty - this plugin likely needs some more work if you want it to be foolproof. However, that said, we are using it in our production environment with good results.
|
45
|
+
|
46
|
+
|
47
|
+
Copyright (c) 2008 Stephen Sykes, released under the MIT license
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# this code is borrowed from Mysql/Ruby, credit to TOMITA Masahiro
|
2
|
+
# it works for me under OS X and Fedora, hope it works for you also
|
3
|
+
|
4
|
+
require 'mkmf'
|
5
|
+
|
6
|
+
if /mswin32/ =~ RUBY_PLATFORM
|
7
|
+
inc, lib = dir_config('mysql')
|
8
|
+
exit 1 unless have_library("libmysql")
|
9
|
+
elsif mc = with_config('mysql-config') then
|
10
|
+
mc = 'mysql_config' if mc == true
|
11
|
+
cflags = `#{mc} --cflags`.chomp
|
12
|
+
exit 1 if $? != 0
|
13
|
+
libs = `#{mc} --libs`.chomp
|
14
|
+
exit 1 if $? != 0
|
15
|
+
$CPPFLAGS += ' ' + cflags
|
16
|
+
$libs = libs + " " + $libs
|
17
|
+
else
|
18
|
+
inc, lib = dir_config('mysql', '/usr/local')
|
19
|
+
libs = ['m', 'z', 'socket', 'nsl', 'mygcc']
|
20
|
+
while not find_library('mysqlclient', 'mysql_query', lib, "#{lib}/mysql") do
|
21
|
+
exit 1 if libs.empty?
|
22
|
+
have_library(libs.shift)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
create_makefile("slim_attrib_ext")
|
@@ -0,0 +1,141 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
|
3
|
+
#include <mysql.h>
|
4
|
+
#include <errmsg.h>
|
5
|
+
#include <mysqld_error.h>
|
6
|
+
|
7
|
+
#define GetMysqlRes(obj) (Check_Type(obj, T_DATA), ((struct mysql_res*)DATA_PTR(obj))->res)
|
8
|
+
#define GetLongPtr(obj) (Check_Type(obj, T_DATA), (long*)DATA_PTR(obj))
|
9
|
+
#define GetCharPtr(obj) (Check_Type(obj, T_DATA), (char*)DATA_PTR(obj))
|
10
|
+
#define GetCharStarPtr(obj) (Check_Type(obj, T_DATA), (char**)DATA_PTR(obj))
|
11
|
+
|
12
|
+
VALUE cRowHash, cClass;
|
13
|
+
|
14
|
+
// from mysql/ruby
|
15
|
+
struct mysql_res {
|
16
|
+
MYSQL_RES* res;
|
17
|
+
char freed;
|
18
|
+
};
|
19
|
+
|
20
|
+
// row info
|
21
|
+
#define SLIM_IS_NULL (char)1
|
22
|
+
#define SLIM_IS_SET (char)2
|
23
|
+
|
24
|
+
static VALUE all_hashes(VALUE obj) {
|
25
|
+
MYSQL_RES *res = GetMysqlRes(obj);
|
26
|
+
MYSQL_FIELD *fields = mysql_fetch_fields(res);
|
27
|
+
MYSQL_ROW row;
|
28
|
+
VALUE all_hashes_ary, col_names_hsh, row_ary;
|
29
|
+
my_ulonglong nr = mysql_num_rows(res);
|
30
|
+
unsigned int nf = mysql_num_fields(res);
|
31
|
+
unsigned int i, j, s;
|
32
|
+
unsigned long *lengths;
|
33
|
+
char *row_space, *row_info_space, **pointers_space;
|
34
|
+
|
35
|
+
/* hash of column names */
|
36
|
+
col_names_hsh = rb_hash_new();
|
37
|
+
for (i=0; i<nf; i++) {
|
38
|
+
rb_hash_aset(col_names_hsh, rb_str_new2(fields[i].name), INT2FIX(i));
|
39
|
+
}
|
40
|
+
|
41
|
+
/* array of result rows */
|
42
|
+
all_hashes_ary = rb_ary_new2(nr);
|
43
|
+
for (i=0; i<nr; i++) {
|
44
|
+
VALUE frh;
|
45
|
+
frh = rb_class_new_instance(0, NULL, cRowHash);
|
46
|
+
rb_iv_set(frh, "@field_indexes", col_names_hsh);
|
47
|
+
row = mysql_fetch_row(res); // get the row
|
48
|
+
|
49
|
+
row_ary = rb_ary_new();
|
50
|
+
rb_iv_set(frh, "@row", row_ary); // ready to hold fetched fields
|
51
|
+
|
52
|
+
lengths = mysql_fetch_lengths(res); // get lengths
|
53
|
+
for (s=j=0; j < nf; j++) s += lengths[j]; // s = total of lengths
|
54
|
+
row_space = malloc(s);
|
55
|
+
pointers_space = malloc((nf + 1) * sizeof(char *));
|
56
|
+
row_info_space = calloc(nf, 1);
|
57
|
+
for (s=j=0; j < nf; j++) {
|
58
|
+
int len = lengths[j];
|
59
|
+
char *p = row_space + s;
|
60
|
+
s += len;
|
61
|
+
pointers_space[j] = p;
|
62
|
+
if (!row[j]) row_info_space[j] = SLIM_IS_NULL;
|
63
|
+
else memcpy(p, row[j], len); // copy row data in
|
64
|
+
}
|
65
|
+
pointers_space[nf] = row_space + s;
|
66
|
+
rb_iv_set(frh, "@pointers", Data_Wrap_Struct(cClass, 0, free, pointers_space));
|
67
|
+
rb_iv_set(frh, "@row_info", Data_Wrap_Struct(cClass, 0, free, row_info_space));
|
68
|
+
rb_iv_set(frh, "@raw_row", Data_Wrap_Struct(cClass, 0, free, row_space)); // @raw_row contains pointer to the the row data
|
69
|
+
rb_ary_store(all_hashes_ary, i, frh);
|
70
|
+
}
|
71
|
+
return all_hashes_ary;
|
72
|
+
}
|
73
|
+
|
74
|
+
static VALUE fetch_by_index(VALUE obj, VALUE index) {
|
75
|
+
VALUE row_ary, row_ary_contents, contents, row_info_obj;
|
76
|
+
char *raw_row, *row_info, **pointers, *start;
|
77
|
+
long col_number;
|
78
|
+
unsigned int length;
|
79
|
+
|
80
|
+
col_number = FIX2LONG(index);
|
81
|
+
row_info_obj = rb_iv_get(obj, "@row_info");
|
82
|
+
row_ary = rb_iv_get(obj, "@row");
|
83
|
+
if (NIL_P(row_info_obj)) return rb_ary_entry(row_ary, col_number); // was marshalled
|
84
|
+
else {
|
85
|
+
row_info = GetCharPtr(row_info_obj);
|
86
|
+
if (row_info[col_number] == SLIM_IS_SET) return rb_ary_entry(row_ary, col_number); // was set already, return array entry
|
87
|
+
}
|
88
|
+
|
89
|
+
if (row_info[col_number] == SLIM_IS_NULL) { // return nil if null from db
|
90
|
+
rb_ary_store(row_ary, col_number, Qnil);
|
91
|
+
row_info[col_number] = SLIM_IS_SET;
|
92
|
+
return Qnil;
|
93
|
+
}
|
94
|
+
|
95
|
+
pointers = GetCharStarPtr(rb_iv_get(obj, "@pointers"));
|
96
|
+
start = pointers[col_number];
|
97
|
+
length = pointers[col_number + 1] - start;
|
98
|
+
contents = rb_tainted_str_new(start, length);
|
99
|
+
rb_ary_store(row_ary, col_number, contents);
|
100
|
+
row_info[col_number] = SLIM_IS_SET;
|
101
|
+
return contents;
|
102
|
+
}
|
103
|
+
|
104
|
+
static VALUE slim_fetch(VALUE obj, VALUE name) {
|
105
|
+
VALUE real_hash, hash_lookup;
|
106
|
+
real_hash = rb_iv_get(obj, "@real_hash");
|
107
|
+
if (!NIL_P(real_hash)) return rb_hash_aref(real_hash, name);
|
108
|
+
hash_lookup = rb_hash_aref(rb_iv_get(obj, "@field_indexes"), name);
|
109
|
+
if (NIL_P(hash_lookup)) return Qnil;
|
110
|
+
return fetch_by_index(obj, hash_lookup);
|
111
|
+
}
|
112
|
+
|
113
|
+
static VALUE set_element(VALUE obj, VALUE name, VALUE val) {
|
114
|
+
VALUE real_hash, hash_lookup;
|
115
|
+
VALUE row_ary, row_info_obj;
|
116
|
+
long col_number;
|
117
|
+
|
118
|
+
real_hash = rb_iv_get(obj, "@real_hash");
|
119
|
+
if (!NIL_P(real_hash)) return rb_hash_aset(real_hash, name, val);
|
120
|
+
|
121
|
+
hash_lookup = rb_hash_aref(rb_iv_get(obj, "@field_indexes"), name);
|
122
|
+
if (NIL_P(hash_lookup)) return rb_funcall(rb_funcall(obj, rb_intern("to_hash"), 0), rb_intern("[]="), 2, name, val);
|
123
|
+
row_ary = rb_iv_get(obj, "@row");
|
124
|
+
col_number = FIX2LONG(hash_lookup);
|
125
|
+
rb_ary_store(row_ary, col_number, val);
|
126
|
+
row_info_obj = rb_iv_get(obj, "@row_info");
|
127
|
+
if (!NIL_P(row_info_obj)) GetCharPtr(row_info_obj)[col_number] = SLIM_IS_SET;
|
128
|
+
return val;
|
129
|
+
}
|
130
|
+
|
131
|
+
void Init_slim_attrib_ext() {
|
132
|
+
VALUE c = rb_cObject;
|
133
|
+
c = rb_const_get_at(c, rb_intern("Mysql"));
|
134
|
+
c = rb_const_get_at(c, rb_intern("Result"));
|
135
|
+
rb_define_method(c, "all_hashes", (VALUE(*)(ANYARGS))all_hashes, 0);
|
136
|
+
cRowHash = rb_const_get_at(c, rb_intern("RowHash"));
|
137
|
+
cClass = rb_define_class("CObjects", cRowHash);
|
138
|
+
rb_define_method(cRowHash, "fetch_by_index", (VALUE(*)(ANYARGS))fetch_by_index, 1);
|
139
|
+
rb_define_method(cRowHash, "[]", (VALUE(*)(ANYARGS))slim_fetch, 1);
|
140
|
+
rb_define_method(cRowHash, "[]=", (VALUE(*)(ANYARGS))set_element, 2);
|
141
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'mysql'
|
2
|
+
|
3
|
+
class Mysql::Result
|
4
|
+
class RowHash
|
5
|
+
def marshal_dump
|
6
|
+
to_hash
|
7
|
+
end
|
8
|
+
|
9
|
+
def marshal_load(hash)
|
10
|
+
@real_hash = hash
|
11
|
+
end
|
12
|
+
|
13
|
+
def has_key?(name)
|
14
|
+
@real_hash ? @real_hash.has_key?(name) : @field_indexes[name]
|
15
|
+
end
|
16
|
+
|
17
|
+
alias_method :include?, :has_key?
|
18
|
+
|
19
|
+
def keys
|
20
|
+
@real_hash ? @real_hash.keys : @field_indexes.keys
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_hash
|
24
|
+
@real_hash ||= begin
|
25
|
+
@field_indexes.each_value {|v| fetch_by_index(v)}
|
26
|
+
@field_indexes.inject({}) {|memo, fi| memo[fi[0]] = @row[fi[1]]; memo}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def to_a
|
31
|
+
to_hash.to_a
|
32
|
+
end
|
33
|
+
|
34
|
+
def method_missing(name, *args, &block)
|
35
|
+
to_hash.send(name, *args, &block)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
require 'slim_attrib_ext'
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.4
|
3
|
+
specification_version: 1
|
4
|
+
name: slim-attributes
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: "0.1"
|
7
|
+
date: 2008-04-06 00:00:00 +03:00
|
8
|
+
summary: Slim attributes boosts speed in Rails/Mysql ActiveRecord Models by avoiding instantiating Hashes for each result row, and lazily instantiating attributes as needed
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: sdsykes@gmail.com
|
12
|
+
homepage: http://slim-attributes.rubyforge.org/
|
13
|
+
rubyforge_project: slim-attributes
|
14
|
+
description:
|
15
|
+
autorequire: slim
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors:
|
30
|
+
- Stephen Sykes
|
31
|
+
files:
|
32
|
+
- lib/slim_attributes.rb
|
33
|
+
- ext/extconf.rb
|
34
|
+
- ext/slim_attrib_ext.c
|
35
|
+
- README
|
36
|
+
- MIT_LICENCE
|
37
|
+
test_files: []
|
38
|
+
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
executables: []
|
44
|
+
|
45
|
+
extensions:
|
46
|
+
- ext/extconf.rb
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
dependencies: []
|
50
|
+
|