Skip to content

Commit

Permalink
Improved creation time of Bits and Integers
Browse files Browse the repository at this point in the history
  • Loading branch information
dmendel committed May 18, 2021
1 parent bb60531 commit d99f050
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.rdoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
= BinData Changelog

== Version 2.4.10 (2021-05-18)

* Improve speed of dynamic object creation. Reported by Charlie Ablett.

== Version 2.4.9 (2021-04-22)

* Change example from Fixnum to Integer. Thanks to Tim Chambers.
Expand Down
10 changes: 5 additions & 5 deletions lib/bindata/bits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ def create_fixed_clamp_code(nbits, signed)
end

if signed == :signed
max = (1 << (nbits - 1)) - 1
min = -(max + 1)
max = "max = (1 << (#{nbits} - 1)) - 1"
min = "min = -(max + 1)"
else
min = 0
max = (1 << nbits) - 1
min = "min = 0"
max = "max = (1 << #{nbits}) - 1"
end

clamp = "(val < #{min}) ? #{min} : (val > #{max}) ? #{max} : val"
clamp = "(#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val)"

if nbits == 1
# allow single bits to be used as booleans
Expand Down
12 changes: 7 additions & 5 deletions lib/bindata/int.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ def read_and_return_value(io)

def create_clamp_code(nbits, signed)
if signed == :signed
max = (1 << (nbits - 1)) - 1
min = -(max + 1)
max = "max = (1 << (#{nbits} - 1)) - 1"
min = "min = -(max + 1)"
else
max = (1 << nbits) - 1
min = 0
max = "max = (1 << #{nbits}) - 1"
min = "min = 0"
end

"val = (val < #{min}) ? #{min} : (val > #{max}) ? #{max} : val"
clamp = "(#{max}; #{min}; val = (val < min) ? min : (val > max) ? max : val)"

"val = #{clamp}"
end

def create_read_code(nbits, endian, signed)
Expand Down

0 comments on commit d99f050

Please sign in to comment.