Skip to content

Commit 8872ce4

Browse files
committed
doc: readme.md: documented the new CRC32 and string hashing APIs and macros.
1 parent 6481f54 commit 8872ce4

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

README.md

+64-6
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ openresty/luajit2 - OpenResty's maintained branch of LuaJIT.
1313
* [table.nkeys](#tablenkeys)
1414
* [table.clone](#tableclone)
1515
* [jit.prngstate](#jitprngstate)
16+
* [jit.crc32](#jitcrc32)
17+
* [jit.strhashcrc32](#jitstrhashcrc32)
1618
* [thread.exdata](#threadexdata)
1719
* [New macros](#new-macros)
1820
* [`OPENRESTY_LUAJIT`](#openresty-luajit)
21+
* [`LJ_OR_DISABLE_STRHASHCRC32`](#lj-or-disable-strhashcrc32)
1922
* [Optimizations](#optimizations)
2023
* [Updated JIT default parameters](#updated-jit-default-parameters)
2124
* [String hashing](#string-hashing)
@@ -155,6 +158,47 @@ local newstate = jit.prngstate(123456)
155158

156159
[Back to TOC](#table-of-contents)
157160

161+
### jit.crc32
162+
163+
**syntax:** *ok = jit.crc32()*
164+
165+
Returns a boolean value indicating if the current architecture supports a CRC32
166+
instruction set.
167+
168+
CRC32 support will be checked at runtime if LuaJIT was compiled with `-msse4.2`
169+
on x64 architectures, or `-march=armv8-a+crc` on ARM64.
170+
171+
CRC32 support allows for this branch to use an optimized string hashing
172+
algorithm. See the [String hashing](#string-hashing) section for details on
173+
how to enable this optimization.
174+
175+
Usage:
176+
177+
```lua
178+
local ok = jit.crc32()
179+
```
180+
181+
[Back to TOC](#table-of-contents)
182+
183+
### jit.strhashcrc32
184+
185+
**syntax:** *ok = jit.strhashcrc32()*
186+
187+
Returns a boolean value indicating if the optimized string hashing algorithm
188+
implemented by this branch is enabled. The `ok` return value will be `true` if
189+
it is enabled, or `false` otherwise.
190+
191+
See the [String hashing](#string-hashing) section for details on
192+
how to enable this optimization.
193+
194+
Usage:
195+
196+
```lua
197+
local ok = jit.strhashcrc32()
198+
```
199+
200+
[Back to TOC](#table-of-contents)
201+
158202
### thread.exdata
159203

160204
**syntax:** *exdata = th_exdata(data?)*
@@ -207,6 +251,14 @@ help distinguishing this OpenResty-specific branch of LuaJIT.
207251
208252
[Back to TOC](#table-of-contents)
209253
254+
### `LJ_OR_DISABLE_STRHASHCRC32`
255+
256+
When specified at compilation (`-DLJ_OR_DISABLE_STRHASHCRC32`), this flag will
257+
disable the string hashing optimization described in the [String
258+
hashing](#string-hashing) section.
259+
260+
[Back to TOC](#table-of-contents)
261+
210262
## Optimizations
211263
212264
### Updated JIT default parameters
@@ -227,13 +279,19 @@ maxmcode=40960 -- in KB
227279

228280
### String hashing
229281

230-
This optimization only applies to Intel CPUs supporting the SSE 4.2 instruction
231-
sets. For such CPUs, and when this branch is compiled with `-msse4.2`, the
232-
string hashing function used for strings interning will be based on an
233-
optimized crc32 implementation (see `lj_str_new()`).
282+
This optimizations modifies the string hashing algorithm to use a CRC32-based
283+
variant. This variant still provides constant-time hashing complexity (`O(n)`)
284+
but makes hash collision attacks harder for strings up to 127 bytes of size
285+
(see `lj_str_new()`).
286+
287+
This optimization is only available for x64 and ARM64 architectures, and will
288+
be enabled if:
289+
290+
1. LuaJIT is compiled with `-msse4.2` on x64, or `-march=armv8-a+crc` on ARM64.
291+
2. A CRC32 instruction set is detected at runtime (see [jit.crc32](#jitcrc32)).
234292

235-
This optimization still provides constant-time hashing complexity (`O(n)`), but
236-
makes hash collision attacks harder for strings up to 127 bytes of size.
293+
**Note:** This optimization can be disabled by compiling LuaJIT with
294+
`-DLJ_OR_DISABLE_STRHASHCRC32`.
237295

238296
[Back to TOC](#table-of-contents)
239297

0 commit comments

Comments
 (0)