-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Require performance fix - code review #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
151224f
6f9a15d
2cb09a6
a3e3f01
86ed673
b20b293
42d8f82
c4b552f
d91179d
ac9498d
2bedcb9
301eaae
3e03f7f
5ae2a0e
e23f97e
460fb8f
88f9bab
eb8ee9a
41ed774
cb12cc2
d5567a3
da2f6b2
aba32cf
cffaa8e
d31fbb1
60aaf0b
de8b4e7
ee48900
cacd227
f19623d
d8a2bf6
e43847f
058323a
c442bce
e52a34b
752c955
3df711d
e3d72d6
8686576
8652090
0abe5a7
1c088c6
06aa81a
30b72cd
d7d6c41
4b6040b
49beb20
1b94e34
aca6251
315a20a
3304125
729caae
9158b2a
74bb0b6
b68d668
a60f97c
07fada2
9005515
5fe4248
93c4717
fcfa394
0757268
1f01734
21f7d11
58771f3
23724fc
6d6f204
6bd8bcb
11203f2
992c68c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
… allows require_2 to pass rubyspec and the case_insensitive test.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -979,6 +979,76 @@ rb_f_require_2(VALUE obj, VALUE fname) | |
| return rb_require_safe_2(fname, rb_safe_level()); | ||
| } | ||
|
|
||
| void | ||
| rb_rehash_loaded_features() | ||
| { | ||
| int i; | ||
| VALUE features; | ||
| VALUE feature; | ||
|
|
||
| st_table* new_loading_table = get_new_loading_table(); | ||
|
|
||
| if (!new_loading_table) { | ||
| printf("Can't rehash, no table loaded\n"); | ||
| return; | ||
| } | ||
|
|
||
| st_clear(new_loading_table); | ||
|
|
||
| features = get_loaded_features(); | ||
|
|
||
| for (i = 0; i < RARRAY_LEN(features); ++i) { | ||
| feature = RARRAY_PTR(features)[i]; | ||
| st_insert( | ||
| new_loading_table, | ||
| (st_data_t)ruby_strdup(RSTRING_PTR(feature)), | ||
| (st_data_t)rb_barrier_new()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| VALUE | ||
| ary_new(VALUE, long); // array.c | ||
|
|
||
| static VALUE rb_cLoadedFeaturesProxy; | ||
|
|
||
| static VALUE | ||
| rb_loaded_features_hook(int argc, VALUE *argv, VALUE self) | ||
| { | ||
| VALUE ret; | ||
| ret = rb_call_super(argc, argv); | ||
| rb_rehash_loaded_features(); | ||
| return ret; | ||
| } | ||
|
|
||
| /* | ||
| * $LOADED_FEATURES is exposed publically as an array, but under the covers | ||
| * we also store this data in a hash for fast lookups. So that we can rebuild | ||
| * the hash whenever $LOADED_FEATURES is changed, we wrap the Array class | ||
| * in a proxy that intercepts all data-modifying methods and rebuilds the | ||
| * hash. * * Note that the list of intercepted methods is currently non-comprehensive | ||
| * --- it only covers modifications made by the ruby and rubyspec test suites. | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not make it comprehensive, at least for all std array methods? But it raises the question, what do you do about mutating extensions to Array? Furthermore, you should add some tests that cover this hash invalidation on $LOADED_FEATURES change behavior.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be comprehensive, that is sloppiness on my part. After discussion on ruby-core, not sure I'll be able to proceed with this approach anyway.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also it is covered implicitly by existing tests - neither |
||
| void | ||
| define_loaded_features_proxy() | ||
| { | ||
| char* methods_to_hook[] = {"push", "clear", "replace"}; | ||
| int i; | ||
|
|
||
| rb_cLoadedFeaturesProxy = rb_define_class("LoadedFeaturesProxy", rb_cArray); | ||
| for (i = 0; i < 3; ++i) { | ||
| rb_define_method( | ||
| rb_cLoadedFeaturesProxy, | ||
| methods_to_hook[i], | ||
| rb_loaded_features_hook, | ||
| -1); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| void | ||
| Init_load() | ||
| { | ||
|
|
@@ -995,7 +1065,10 @@ Init_load() | |
|
|
||
| rb_define_virtual_variable("$\"", get_loaded_features, 0); | ||
| rb_define_virtual_variable("$LOADED_FEATURES", get_loaded_features, 0); | ||
| vm->loaded_features = rb_ary_new(); | ||
|
|
||
| define_loaded_features_proxy(); | ||
|
|
||
| vm->loaded_features = ary_new(rb_cLoadedFeaturesProxy, RARRAY_EMBED_LEN_MAX); | ||
|
|
||
| rb_define_global_function("load", rb_f_load, -1); | ||
| rb_define_global_function("require", rb_f_require, 1); | ||
|
|
||
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.
fix indent