-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Eliminate ~540MB of allocations from benchmark
- Don't use def_delegators on methods called frequently, it allocates *args - Build native extension String.strncmp to compare two substrings, which results in faster StringPtr#== and StringPtr#start_with?
- Loading branch information
Showing
24 changed files
with
326 additions
and
561 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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
build/generated | ||
.bundle | ||
.ruby-version | ||
lib/strncmp | ||
ext/strncmp/Makefile | ||
ext/strncmp/strncmp.h | ||
ext/strncmp/strncmp.o |
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
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
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,6 @@ | ||
require "mkmf" | ||
extension_name = "strncmp/strncmp" | ||
|
||
create_header | ||
dir_config extension_name | ||
create_makefile extension_name |
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,34 @@ | ||
#include "ruby.h" | ||
#include "extconf.h" | ||
|
||
VALUE rb_strncmp(VALUE self, VALUE str1, VALUE offset1, VALUE str2, VALUE offset2, VALUE length) { | ||
// rb_raise(rb_eArgError, "message %s", "argument"); | ||
|
||
if (!FIXNUM_P(length)) rb_raise(rb_eTypeError, "length must be an Integer"); | ||
if (!FIXNUM_P(offset1)) rb_raise(rb_eTypeError, "offset1 must be an Integer"); | ||
if (!FIXNUM_P(offset2)) rb_raise(rb_eTypeError, "offset1 must be an Integer"); | ||
|
||
long len = NUM2LONG(length); | ||
long beg1 = NUM2LONG(offset1); | ||
long beg2 = NUM2LONG(offset2); | ||
|
||
if (len < 0) rb_raise(rb_eArgError, "length cannot be negative"); | ||
if (beg1 < 0) rb_raise(rb_eArgError, "offset1 cannot be negative"); | ||
if (beg2 < 0) rb_raise(rb_eArgError, "offset2 cannot be negative"); | ||
|
||
if (beg1 + len > rb_str_strlen(str1)) return Qnil; | ||
if (beg2 + len > rb_str_strlen(str2)) return Qnil; | ||
|
||
char *ptr1 = rb_str_subpos(str1, beg1, &len); | ||
char *ptr2 = rb_str_subpos(str2, beg2, &len); | ||
|
||
if (ptr1 == 0 || ptr2 == 0) return Qnil; | ||
|
||
return (memcmp(ptr1, ptr2, len) == 0) ? Qtrue : Qfalse; | ||
} | ||
|
||
void Init_strncmp() | ||
{ | ||
// VALUE rb_cString = rb_define_class("String", rb_cObject); | ||
rb_define_singleton_method(rb_cString, "strncmp", rb_strncmp, 5); | ||
} |
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 @@ | ||
require "strncmp/strncmp" |
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
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
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
Oops, something went wrong.