From 8a4141411480a4b27168d7645ebcc1a207cada00 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Mon, 10 Mar 2014 16:47:52 -0400 Subject: [PATCH 1/5] add caching feature for bitcore.X requires Normally when one loads the bitcore object, all classes are loaded into memory, even if they are not used. This comment updates this so that the classes are only required when they are actually used, preserving memory and making bitcore as small and light for each project. This code is only relevant for node, since the browser will have the ability to compile only the selected components. Also, the Key class does not yet work with this, but everything else does. --- bitcore.js | 75 +++++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 37 deletions(-) diff --git a/bitcore.js b/bitcore.js index b55dc7b74..9e755a3e3 100644 --- a/bitcore.js +++ b/bitcore.js @@ -1,49 +1,50 @@ -/* - * Bitcore bindings for the browser - */ - - - -module.exports.bignum = require('bignum'); -module.exports.base58 = require('base58-native'); -module.exports.buffertools = require('buffertools'); - -module.exports.config = require('./config'); -module.exports.const = require('./const'); -module.exports.Deserialize = require('./Deserialize'); -module.exports.log = require('./util/log'); -module.exports.networks = require('./networks'); -module.exports.util = require('./util/util'); - -module.exports.EncodedData = require('./util/EncodedData'); -module.exports.VersionedData = require('./util/VersionedData'); +/* +One way to require files is this simple way: module.exports.Address = require('./Address'); -module.exports.Opcode = require('./Opcode'); -module.exports.Script = require('./Script'); -module.exports.Transaction = require('./Transaction'); -module.exports.Connection = require('./Connection'); -module.exports.Peer = require('./Peer'); -module.exports.Block = require('./Block'); -module.exports.ScriptInterpreter = require('./ScriptInterpreter'); -module.exports.Bloom = require('./Bloom'); + +However, that will load all classes in memory even if they are not used. +Instead, we can set the 'get' property of each class to only require them when +they are accessed, saving memory if they are not used in a given project. +*/ +var requireWhenAccessed = function(name, file) { + Object.defineProperty(module.exports, name, {get: function() {return require(file)}}); +}; + +requireWhenAccessed('bignum', 'bignum'); +requireWhenAccessed('base58', 'base58-native'); +requireWhenAccessed('buffertools', 'buffertools'); +requireWhenAccessed('config', './config'); +requireWhenAccessed('const', './const'); +requireWhenAccessed('Deserialize', './Deserialize'); +requireWhenAccessed('log', './util/log'); +requireWhenAccessed('networks', './networks'); +requireWhenAccessed('util', './util/util'); +requireWhenAccessed('EncodedData', './util/EncodedData'); +requireWhenAccessed('VersionedData', './util/VersionedData'); +requireWhenAccessed('Address', './Address'); +requireWhenAccessed('Opcode', './Opcode'); +requireWhenAccessed('Script', './Script'); +requireWhenAccessed('Transaction', './Transaction'); +requireWhenAccessed('Connection', './Connection'); +requireWhenAccessed('Peer', './Peer'); +requireWhenAccessed('Block', './Block'); +requireWhenAccessed('ScriptInterpreter', './ScriptInterpreter'); +requireWhenAccessed('Bloom', './Bloom'); module.exports.KeyModule = require('./Key'); -module.exports.SINKey = require('./SINKey'); -module.exports.SIN = require('./SIN'); -module.exports.PrivateKey = require('./PrivateKey'); -module.exports.RpcClient = require('./RpcClient'); -module.exports.Wallet = require('./Wallet'); -module.exports.WalletKey = require('./WalletKey'); +requireWhenAccessed('SINKey', './SINKey'); +requireWhenAccessed('SIN', './SIN'); +requireWhenAccessed('PrivateKey', './PrivateKey'); +requireWhenAccessed('RpcClient', './RpcClient'); +requireWhenAccessed('Wallet', './Wallet'); +requireWhenAccessed('WalletKey', './WalletKey'); module.exports.Buffer = Buffer; if (typeof process.versions === 'undefined') { // Browser specific module.exports.bignum.config({EXPONENTIAL_AT: 9999999, DECIMAL_PLACES: 0, ROUNDING_MODE: 1}); - // module.exports.PeerManager = function () { - // throw new Error('PeerManager not availabe in browser Bitcore, under .bitcore. Use it with: require(\'PeerManager\');'); - // }; } else { // Nodejs specific - module.exports.PeerManager = require('./PeerManager'); + requireWhenAccessed('PeerManager', './PeerManager'); } From 5b67d91f3518907065027a94cf4d13a336602317 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Mon, 10 Mar 2014 17:04:23 -0400 Subject: [PATCH 2/5] fix the loading for Key.js Now, like all other modules loaded in the bitcore object, Key is only required when it is actually used. All tests pass in node and the browser. --- bitcore.js | 3 ++- browser/browserify.js | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bitcore.js b/bitcore.js index 9e755a3e3..c6db1fb2f 100644 --- a/bitcore.js +++ b/bitcore.js @@ -30,7 +30,8 @@ requireWhenAccessed('Peer', './Peer'); requireWhenAccessed('Block', './Block'); requireWhenAccessed('ScriptInterpreter', './ScriptInterpreter'); requireWhenAccessed('Bloom', './Bloom'); -module.exports.KeyModule = require('./Key'); +//module.exports.KeyModule = require('./Key'); +requireWhenAccessed('KeyModule', './Key'); requireWhenAccessed('SINKey', './SINKey'); requireWhenAccessed('SIN', './SIN'); requireWhenAccessed('PrivateKey', './PrivateKey'); diff --git a/browser/browserify.js b/browser/browserify.js index d1b627f59..87b341504 100644 --- a/browser/browserify.js +++ b/browser/browserify.js @@ -35,6 +35,7 @@ var modules = [ 'PeerManager', 'PrivateKey', 'RpcClient', + 'Key', 'SIN', 'SINKey', 'Script', @@ -60,7 +61,6 @@ b.require('browserify-buffertools/buffertools.js', {expose:'buffertools'}); b.require('./bitcore', {expose: 'bitcore'}); b.require('buffer', {expose: 'buffer'}); b.require('base58-native'); -b.require('./Key.js', {expose: 'KeyModule'}); b.require('./util/log'); b.require('./util/util'); b.require('./util/EncodedData'); From 82d9f44b6ba8d209c730657da7689ef3163ea4ec Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Mon, 10 Mar 2014 17:06:11 -0400 Subject: [PATCH 3/5] remove unnecessary "buffer" from browserify.js --- browser/browserify.js | 1 - 1 file changed, 1 deletion(-) diff --git a/browser/browserify.js b/browser/browserify.js index 87b341504..21fa085c0 100644 --- a/browser/browserify.js +++ b/browser/browserify.js @@ -59,7 +59,6 @@ var b = browserify(opts); b.require('browserify-bignum/bignumber.js', {expose: 'bignum'} ); b.require('browserify-buffertools/buffertools.js', {expose:'buffertools'}); b.require('./bitcore', {expose: 'bitcore'}); -b.require('buffer', {expose: 'buffer'}); b.require('base58-native'); b.require('./util/log'); b.require('./util/util'); From acd42bf459ab69de1675e22611d96d333bd7afed Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Mon, 10 Mar 2014 17:07:13 -0400 Subject: [PATCH 4/5] remove comment --- bitcore.js | 1 - 1 file changed, 1 deletion(-) diff --git a/bitcore.js b/bitcore.js index c6db1fb2f..c66b5c5df 100644 --- a/bitcore.js +++ b/bitcore.js @@ -30,7 +30,6 @@ requireWhenAccessed('Peer', './Peer'); requireWhenAccessed('Block', './Block'); requireWhenAccessed('ScriptInterpreter', './ScriptInterpreter'); requireWhenAccessed('Bloom', './Bloom'); -//module.exports.KeyModule = require('./Key'); requireWhenAccessed('KeyModule', './Key'); requireWhenAccessed('SINKey', './SINKey'); requireWhenAccessed('SIN', './SIN'); From 8e430df82ccd04edea5762c00101ac67010ce535 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Mon, 10 Mar 2014 17:50:52 -0400 Subject: [PATCH 5/5] fix address example by using bitcore.Address --- examples/example.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/example.html b/examples/example.html index 40a993cc7..951ef1e72 100644 --- a/examples/example.html +++ b/examples/example.html @@ -12,7 +12,7 @@