ragel-bitmap 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aceb3e68e482270337fa3a13a01c176df168731cbb129f7dca0d42a7a999fcb8
4
- data.tar.gz: 9891faf2710a410ba99a2a9f40ed8aa7fd06f239a77312b1a76199d03236d653
3
+ metadata.gz: '092e46dd50337f51b96bc571bcabb3cb157c903a65469256f923df24006dcca4'
4
+ data.tar.gz: 965528c96ccd950bbd515dd57cf7213debeacb1a1f2531b22fc503e4c991401d
5
5
  SHA512:
6
- metadata.gz: 2f92c6dee162edfeb0a2e5c09745e08c03797ced2f704fefbd1b89d1aaa5fd1ee8f8252cc8dbc4cfb5c8a4f5d38dbfa00be7579f87e704a71c765637422065ba
7
- data.tar.gz: 5e4f050c199ae16bd4008bfb4ff06fb0a515109a8ceb8832195b3117c6737cd9d90b7b14912fc2bc223630ae53503f5608162cb10262d472a7053adfd85a1f84
6
+ metadata.gz: 0b27d046b012d2305cf884abc5c012724b40aaec5095c41aed8f19cd3ed2536cb23c5e878d79cdcd8c55f51d137adc5a481014e139cb76b2dcca3fb5535d6a49
7
+ data.tar.gz: e693f71d64e87a3c7cf25d30c554afc059ca288911feb7e3c3ad0835dc641f800c19d50df24eec78b8a4aa996a9d4e076981f7ec64adc589fd84a82ba07ed1e0
data/.rubocop.yml CHANGED
@@ -5,3 +5,6 @@ AllCops:
5
5
  Exclude:
6
6
  - 'test/ragel/fixtures/*'
7
7
  - 'vendor/**/*'
8
+
9
+ Style/Documentation:
10
+ Enabled: false
@@ -0,0 +1,76 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ - Using welcoming and inclusive language
18
+ - Being respectful of differing viewpoints and experiences
19
+ - Gracefully accepting constructive criticism
20
+ - Focusing on what is best for the community
21
+ - Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ - The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ - Trolling, insulting/derogatory comments, and personal or political attacks
28
+ - Public or private harassment
29
+ - Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ - Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at kevin.deisz@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ https://www.contributor-covenant.org/faq
data/Gemfile.lock CHANGED
@@ -1,20 +1,20 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ragel-bitmap (0.1.0)
4
+ ragel-bitmap (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.0)
10
- jaro_winkler (1.5.2)
10
+ jaro_winkler (1.5.3)
11
11
  minitest (5.11.3)
12
12
  parallel (1.17.0)
13
13
  parser (2.6.3.0)
14
14
  ast (~> 2.4.0)
15
15
  rainbow (3.0.0)
16
16
  rake (12.3.2)
17
- rubocop (0.70.0)
17
+ rubocop (0.72.0)
18
18
  jaro_winkler (~> 1.5.1)
19
19
  parallel (~> 1.10)
20
20
  parser (>= 2.6)
@@ -32,7 +32,7 @@ DEPENDENCIES
32
32
  minitest (~> 5.11)
33
33
  ragel-bitmap!
34
34
  rake (~> 12.3)
35
- rubocop (~> 0.70)
35
+ rubocop (~> 0.72)
36
36
 
37
37
  BUNDLED WITH
38
38
  2.0.1
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2019 Kevin Deisz
3
+ Copyright (c) 2019-present Kevin Deisz
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.com/kddeisz/ragel-bitmap.svg?branch=master)](https://travis-ci.com/kddeisz/ragel-bitmap)
4
4
 
5
- Use bitmaps for ragel-generated code instead of arrays.
5
+ [Ragel](https://www.colm.net/open-source/ragel/) generates ruby code with very large arrays of integers that allocate a lot of memory when required. To reduce memory consumption, this gem replaces those arrays with packed strings.
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,7 +22,17 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- Use `Ragel::Bitmap.replace(filepath)` where `filepath` is a path to a ragel-generated file. Require `ragel/bitmap` in your code. Profit!
25
+ After you've run `ragel` to generate your parser, you should then run this gem over the resulting source file and it will replace the integer arrays inline. For example, the following code adds a rake rule to generate the ragel parser from the grammar file and then run `Ragel::Bitmap` over it:
26
+
27
+ ```ruby
28
+ rule %r|_parser\.rb\z| => '%X.rl' do |t|
29
+ sh "ragel -s -R -L -F1 -o #{t.name} #{t.source}"
30
+ require 'ragel/bitmap'
31
+ Ragel::Bitmap.replace(t.name)
32
+ end
33
+ ```
34
+
35
+ Then, in your application, add `require 'ragel/bitmap'` before you require your parser. Now you should be off and running!
26
36
 
27
37
  ## Development
28
38
 
@@ -3,10 +3,36 @@
3
3
  require 'ripper'
4
4
 
5
5
  module Ragel
6
- class Bitmap
6
+ module Bitmap
7
7
  # A ruby parser that finds instances of table declarations in a
8
8
  # ragel-outputted file.
9
9
  class Replace < Ripper::SexpBuilderPP
10
+ class << self
11
+ # Get the required args for a bitmap from a set of numbers
12
+ def bitmap_args_from(numbers)
13
+ size = (Math.log2(numbers.max) / 8).ceil
14
+ strings =
15
+ size.downto(1).map do |index|
16
+ shift = (index - 1) * 8
17
+ numbers.map { |number| (number >> shift) & 0xff }.pack('C*')
18
+ end
19
+
20
+ [class_from(size), strings]
21
+ end
22
+
23
+ private
24
+
25
+ def class_from(size)
26
+ case size
27
+ when 1 then :Array8
28
+ when 2 then :Array16
29
+ when 3 then :Array24
30
+ else
31
+ :ArrayGeneric
32
+ end
33
+ end
34
+ end
35
+
10
36
  # Represents a table declaration in the source
11
37
  class Table
12
38
  attr_reader :source, :start_line, :end_line
@@ -17,14 +43,13 @@ module Ragel
17
43
  @end_line = lineno
18
44
  end
19
45
 
46
+ private
47
+
20
48
  def source_from(name, numbers)
21
- width = Math.log2(numbers.max).ceil
22
- bitmap =
23
- numbers.each_with_index.inject(0) do |accum, (number, index)|
24
- accum | (number << (width * index))
25
- end
49
+ clazz, strings = Replace.bitmap_args_from(numbers)
50
+ arguments = strings.map(&:inspect).join(', ')
26
51
 
27
- "self.#{name} = ::Ragel::Bitmap.new(#{width}, #{bitmap})"
52
+ "self.#{name} = ::Ragel::Bitmap::#{clazz}.new(#{arguments})"
28
53
  end
29
54
  end
30
55
 
@@ -56,26 +81,21 @@ module Ragel
56
81
  @tables = []
57
82
  end
58
83
 
59
- class << self
60
- def replace(source)
61
- buffer = Buffer.new(source)
62
- tables_from(source).reverse_each { |table| buffer.replace(table) }
63
- buffer.to_source
64
- end
84
+ def each_table(&block)
85
+ parse
65
86
 
66
- private
67
-
68
- def tables_from(source)
69
- replace = new(source)
70
- replace.parse
87
+ if error?
88
+ warn 'Invalid ruby'
89
+ exit 1
90
+ end
71
91
 
72
- if replace.error?
73
- warn 'Invalid ruby'
74
- exit 1
75
- end
92
+ tables.reverse_each(&block)
93
+ end
76
94
 
77
- replace.tables
78
- end
95
+ def self.replace(source)
96
+ buffer = Buffer.new(source)
97
+ new(source).each_table { |table| buffer.replace(table) }
98
+ buffer.to_source
79
99
  end
80
100
 
81
101
  private
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ragel
4
- class Bitmap
5
- VERSION = '0.1.0'
4
+ module Bitmap
5
+ VERSION = '0.2.0'
6
6
  end
7
7
  end
data/lib/ragel/bitmap.rb CHANGED
@@ -3,19 +3,54 @@
3
3
  require 'ragel/bitmap/version'
4
4
 
5
5
  module Ragel
6
- # An integer bitmap that contains a width (the number of bytes that the
7
- # largest integer requires) and a bitmap (an integer that is the combination
8
- # of the element integers)
9
- class Bitmap
10
- attr_reader :width, :bitmap
11
-
12
- def initialize(width, bitmap)
13
- @width = width
14
- @bitmap = bitmap
6
+ module Bitmap
7
+ class Array8
8
+ def initialize(string)
9
+ @string = string
10
+ end
11
+
12
+ def [](idx)
13
+ @string.getbyte(idx)
14
+ end
15
+ end
16
+
17
+ class Array16
18
+ def initialize(highstring, lowstring)
19
+ @highstring = highstring
20
+ @lowstring = lowstring
21
+ end
22
+
23
+ def [](idx)
24
+ (@highstring.getbyte(idx) << 8) | @lowstring.getbyte(idx)
25
+ end
15
26
  end
16
27
 
17
- def [](index)
18
- (bitmap >> (width * index)) & (2**width - 1)
28
+ class Array24
29
+ def initialize(highstring, middlestring, lowstring)
30
+ @highstring = highstring
31
+ @middlestring = middlestring
32
+ @lowstring = lowstring
33
+ end
34
+
35
+ def [](idx)
36
+ (@highstring.getbyte(idx) << 16) |
37
+ (@middlestring.getbyte(idx) << 8) |
38
+ @lowstring.getbyte(idx)
39
+ end
40
+ end
41
+
42
+ class ArrayGeneric
43
+ def initialize(*strings)
44
+ @strings = strings
45
+ end
46
+
47
+ def [](idx)
48
+ shift = @strings.length * 8
49
+ @strings.inject(0) do |product, bitmap|
50
+ shift -= 8
51
+ product | (bitmap.getbyte(idx) << shift)
52
+ end
53
+ end
19
54
  end
20
55
 
21
56
  def self.replace(filepath)
data/ragel-bitmap.gemspec CHANGED
@@ -28,5 +28,5 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'bundler', '~> 2.0'
29
29
  spec.add_development_dependency 'minitest', '~> 5.11'
30
30
  spec.add_development_dependency 'rake', '~> 12.3'
31
- spec.add_development_dependency 'rubocop', '~> 0.70'
31
+ spec.add_development_dependency 'rubocop', '~> 0.72'
32
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ragel-bitmap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Deisz
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-05-29 00:00:00.000000000 Z
11
+ date: 2019-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.70'
61
+ version: '0.72'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0.70'
68
+ version: '0.72'
69
69
  description:
70
70
  email:
71
71
  - kevin.deisz@gmail.com
@@ -76,6 +76,7 @@ files:
76
76
  - ".gitignore"
77
77
  - ".rubocop.yml"
78
78
  - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
79
80
  - Gemfile
80
81
  - Gemfile.lock
81
82
  - LICENSE