mirror of
https://github.com/R0m1k3/Loki.git
synced 2026-10-15 11:19:07 +02:00
Trois défauts sans rapport entre eux, tous visibles à l'écran. Jauge de contexte bloquée à « 0 / N (0 %) » : le comptage exact vient de usage.prompt_tokens (include_usage), qu'un llama-server récent a cessé de renvoyer. On note désormais si l'usage est arrivé ; sinon la fin de tour publie l'estimation qui pilote déjà la compaction — approximative, mais jamais absente. Et la souscription envoie la valeur courante juste après le rattrapage : une page rechargée affichait zéro sur une discussion pourtant pleine, faute d'événement ctx_used dans la fenêtre rejouée. Espace disque : « il ne reste que 11,9 Go » sur un partage Unraid qui en a des centaines. /mnt/user est servi par shfs, un FUSE qui agrège plusieurs disques — statfs y renvoie l'espace d'un seul, et ce chiffre REFUSAIT l'installation du modèle. Les systèmes de fichiers qui approximent (FUSE, overlay, NFS, CIFS, Ceph, Gluster) sont reconnus par leur magie : leur chiffre reste affiché, précédé d'un « ~ », mais ne bloque plus rien. Panneau Fichiers : le panneau entier défilait, emportant vers le haut l'en-tête, le fil d'Ariane et le pied (occupation disque, bouton .zip) dès qu'il y avait quelques fichiers. Seule la liste défile maintenant ; le pied reste en bas et l'état vide se centre. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
140 lines
3.2 KiB
Markdown
140 lines
3.2 KiB
Markdown
# reusify
|
|
|
|
[![npm version][npm-badge]][npm-url]
|
|
|
|
Reuse your objects and functions for maximum speed. This technique will
|
|
make any function run ~10% faster. You call your functions a
|
|
lot, and it adds up quickly in hot code paths.
|
|
|
|
```
|
|
$ node benchmarks/createNoCodeFunction.js
|
|
Total time 53133
|
|
Total iterations 100000000
|
|
Iteration/s 1882069.5236482036
|
|
|
|
$ node benchmarks/reuseNoCodeFunction.js
|
|
Total time 50617
|
|
Total iterations 100000000
|
|
Iteration/s 1975620.838848608
|
|
```
|
|
|
|
The above benchmark uses fibonacci to simulate a real high-cpu load.
|
|
The actual numbers might differ for your use case, but the difference
|
|
should not.
|
|
|
|
The benchmark was taken using Node v6.10.0.
|
|
|
|
This library was extracted from
|
|
[fastparallel](http://npm.im/fastparallel).
|
|
|
|
## Example
|
|
|
|
```js
|
|
var reusify = require('reusify')
|
|
var fib = require('reusify/benchmarks/fib')
|
|
var instance = reusify(MyObject)
|
|
|
|
// get an object from the cache,
|
|
// or creates a new one when cache is empty
|
|
var obj = instance.get()
|
|
|
|
// set the state
|
|
obj.num = 100
|
|
obj.func()
|
|
|
|
// reset the state.
|
|
// if the state contains any external object
|
|
// do not use delete operator (it is slow)
|
|
// prefer set them to null
|
|
obj.num = 0
|
|
|
|
// store an object in the cache
|
|
instance.release(obj)
|
|
|
|
function MyObject () {
|
|
// you need to define this property
|
|
// so V8 can compile MyObject into an
|
|
// hidden class
|
|
this.next = null
|
|
this.num = 0
|
|
|
|
var that = this
|
|
|
|
// this function is never reallocated,
|
|
// so it can be optimized by V8
|
|
this.func = function () {
|
|
if (null) {
|
|
// do nothing
|
|
} else {
|
|
// calculates fibonacci
|
|
fib(that.num)
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
The above example was intended for synchronous code, let's see async:
|
|
```js
|
|
var reusify = require('reusify')
|
|
var instance = reusify(MyObject)
|
|
|
|
for (var i = 0; i < 100; i++) {
|
|
getData(i, console.log)
|
|
}
|
|
|
|
function getData (value, cb) {
|
|
var obj = instance.get()
|
|
|
|
obj.value = value
|
|
obj.cb = cb
|
|
obj.run()
|
|
}
|
|
|
|
function MyObject () {
|
|
this.next = null
|
|
this.value = null
|
|
|
|
var that = this
|
|
|
|
this.run = function () {
|
|
asyncOperation(that.value, that.handle)
|
|
}
|
|
|
|
this.handle = function (err, result) {
|
|
that.cb(err, result)
|
|
that.value = null
|
|
that.cb = null
|
|
instance.release(that)
|
|
}
|
|
}
|
|
```
|
|
|
|
Also note how in the above examples, the code, that consumes an instance of `MyObject`,
|
|
reset the state to initial condition, just before storing it in the cache.
|
|
That's needed so that every subsequent request for an instance from the cache,
|
|
could get a clean instance.
|
|
|
|
## Why
|
|
|
|
It is faster because V8 doesn't have to collect all the functions you
|
|
create. On a short-lived benchmark, it is as fast as creating the
|
|
nested function, but on a longer time frame it creates less
|
|
pressure on the garbage collector.
|
|
|
|
## Other examples
|
|
If you want to see some complex example, checkout [middie](https://github.com/fastify/middie) and [steed](https://github.com/mcollina/steed).
|
|
|
|
## Acknowledgements
|
|
|
|
Thanks to [Trevor Norris](https://github.com/trevnorris) for
|
|
getting me down the rabbit hole of performance, and thanks to [Mathias
|
|
Buss](http://github.com/mafintosh) for suggesting me to share this
|
|
trick.
|
|
|
|
## License
|
|
|
|
MIT
|
|
|
|
[npm-badge]: https://badge.fury.io/js/reusify.svg
|
|
[npm-url]: https://badge.fury.io/js/reusify
|