I have a text contains a hexadecimal bytes as follows:
01 1b 42 61 6b 68 73 68 5f 48 6f 73 70 69 74 61 6c 5f 41 6c 2d 53 68 61 72 61 66 69 61 02 0f 33 30 30 31 37 34 37 36 39 35 31 30 30 30 33 03 10 30 32 2f 30 31 2f 31 37 5f 30 35 3a 31 32 5f 50 04 05 31 34 2e 30 30 05 01 30
, when I am encrypting it to BASE64 using the of_encode64() function in powerbuilder winsock user object, I got the below result:
MDExQjQyNjE2QjY4NzM2ODVGNDg2RjczNzA2OTc0NjE2QzVGNDE2QzJENTM2ODYx
NzI2MTY2Njk2MTAyMEYzMzMwMzAzMTM3MzQzNzM2MzkzNTMxMzAzMDMwMzMwMzEw
MzAzMjJGMzAzMTJGMzEzNzVGMzAzNTNBMzEzMjVGNTAwNDA1MzEzNDJFMzAzMDA1
MDEzMA==
, but when my friend encodes it using the Convert.ToBase64String() in Dot Net, he got a totally different result as below:
ARtCYWtoc2hfSG9zcGl0YWxfQWwtU2hhcmFmaWECDzMwMDE3NDc2OTUxMDAwMwMQMDIvMDEvMTdfMDU6MTJfUAQFMTQuMDAFATA=
, I played with all possible parameters: CRYPT_STRING_BASE64HEADER, CRYPT_STRING_BASE64REQUESTHEADER, CRYPT_STRING_BASE64X509CRLHEADER but not help!
The of_encode64() function code as below:
[local function declaration]
Function boolean CryptBinaryToString ( &
Blob pbBinary, &
ulong cbBinary, &
ulong dwFlags, &
Ref string pszString, &
Ref ulong pcchString &
) Library "crypt32.dll" Alias For "CryptBinaryToStringW"
[instance variables]
// Base64, with certificate beginning and ending headers
CONSTANT Ulong CRYPT_STRING_BASE64HEADER = 0
// Base64, without headers
CONSTANT Ulong CRYPT_STRING_BASE64 = 1
// Base64, with request beginning and ending headers
CONSTANT Ulong CRYPT_STRING_BASE64REQUESTHEADER = 3
// Base64, with X.509 CRL beginning and ending headers
CONSTANT Ulong CRYPT_STRING_BASE64X509CRLHEADER = 9
Encode a String to Base64:
// -----------------------------------------------------------------------------
// SCRIPT: n_winsock.of_Encode64
//
// PURPOSE: This function converts binary data to a Base64 encoded string.
//
// Note: Requires Windows XP or Server 2003
//
// ARGUMENTS: ablob_data - Blob containing data
//
// RETURN: String containing encoded data
//
// DATE PROG/ID DESCRIPTION OF CHANGE / REASON
// ---------- -------- -----------------------------------------------------
// 08/20/2010 RolandS Initial coding
// 09/27/2010 RolandS Changed to remove trailing CRLF characters
// 03/17/2012 RolandS Changed argument to read-only
// -----------------------------------------------------------------------------
String ls_encoded
ULong lul_len, lul_buflen
Boolean lb_rtn
lul_len = Len(ablob_data)
lul_buflen = lul_len * 3
//lul_buflen = lul_len * 2
ls_encoded = Space(lul_buflen)
lb_rtn = CryptBinaryToString(ablob_data, lul_len, &
CRYPT_STRING_BASE64, ls_encoded, lul_buflen)
If lb_rtn Then
ls_encoded = Left(ls_encoded, lul_buflen - 2)
Else
SetNull(ls_encoded)
End If
Return ls_encoded
I am calling it using this code: Where as_data is the Hexadecimal Bytes String above
Return of_Encode64(Blob(as_data, EncodingAnsi!))
The full code for Encode/decode a string in Base64 in PowerBuilder using the winsock user object in this below URL:
https://www.rgagnon.com/pbdetails/pb-0258.html
Thank you in advance. Keep Safe.
I read about the problem a day ago where the problem occurs with people but when it comes to the final second. I kept noticing in my timer that the as it counts down it sometimes skips a second, some suggested changing the interval from 1000ms to 500ms others said make it 900. And there are those who suggested making your own Timer.
My timer is a bit big as it contains two smaller timers within it, that count down individual minutes within the total time and I activate an animation during those times. So basically I am asking what approach should I take to for a time critical counter that is 100% accurate?
This is my code:
private void startTimer() {
Log.println(Log.ASSERT, "CHECK","Entered startTimer() method");
millisInFuture = mTimeLeftInMillis;
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 900) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
millisPassed = millisInFuture - mTimeLeftInMillis;
progress = (int) (millisPassed * 100 / millisInFuture);
pb.setProgress(progress);
pb2.setProgress(0);
pb3.setProgress(0);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Key: 60 sec
if (millisInFuture == 480000) {
if (millisPassed <= 60000 || (millisPassed > 180000 && millisPassed <= 240000) || (millisPassed > 300000 && millisPassed <= 360000 || (millisPassed > 420000 && millisPassed <= 480000))) {
// Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.fade_in);
// stepupimage.setAnimation(animation);
Log.println(Log.ASSERT,"CHECK","Check that the first if statement of key 60 is entered");
statusIfUp();
time_of_stage = (millisInFuture - millisPassed) % 60000;
progress2 = (int) (time_of_stage*100 / 60000);
Log.println(Log.VERBOSE,"CHECK","TIME OF STAGE = "+time_of_stage);
Log.println(Log.VERBOSE,"CHECK","progress2= "+progress2);
pb2.setProgress(progress2);
updateStageUpCount();
upArrowAnimation();
setflowrate();
}
}
#Override
public void onFinish() {
Toast.makeText(tool1mode1.this, "Done", Toast.LENGTH_SHORT).show();
avd2.stop(); avd3.stop();
try {
pb.setProgress(100); pb2.setProgress(0); pb3.setProgress(0);
stage_timer.setVisibility(View.INVISIBLE);
stage_timer.setVisibility(View.INVISIBLE);
progressBar.setVisibility(View.INVISIBLE);
progressBar2.setVisibility(View.INVISIBLE);
progressBar.setProgress(0);
progressBar2.setProgress(0);
progressBar.setTranslationY(60);
progressBar2.setTranslationY(60);
flow.setTranslationY(60);
animation1.cancel(); animation2.cancel();
//Vibration
if (Build.VERSION.SDK_INT >= 26) {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneShot(150, VibrationEffect.DEFAULT_AMPLITUDE));
} else {
((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createWaveform(new long[]{150}, new int[]{VibrationEffect.EFFECT_CLICK}, -1));
}
}
catch (NullPointerException e) {
e.printStackTrace();
}
}
}.start();
}
So to summarize: I have a big timer and two small timers that count certain intervals within the larger timer. Now the CountDownTimer class that is provided by Android skips a second or two at times during the count down. I want to fix this problem and don't know how, I would have just rolled with it but unfortunately the one I'm making the app for requires it to be 100% accurate as the application is time critical.
Note: pb, pb2, pb3, are progress bars of the timers (1st is the big one, 2nd and 3rd alternate for the second timer)
I'm trying to make a division of two variables, but when I want to print the result, the program prints out 0
the leadTime and endAmount are being correctly printed by te program, but the monthlyAmount is being printed as 0. Also if I remove the float64() around endAmount and leadTime it is being printed as 0
var leadTime int
if currentAge < 45 {
leadTime = 120
} else if currentAge > 45 && currentAge < 55 {
leadTime = 90
} else if currentAge > 55 {
leadTime = 60
}
endAmount, _ := strconv.Atoi(amountAsString)
monthlyAmount:= (float64(endAmount) / float64(leadTime)
fmt.Println("leadTime :", leadTime )
fmt.Println("Total amount:", endAmount)
fmt.Println("monthlyAmount:", monthlyAmount)
with the standardinput I'm testing with, the leadTime = 120 and the endAmount = 93735.00 so the monthlyAmount should be: 781.13
The problem is your trying to convert floating string an int in
endAmount, _ := strconv.Atoi(amountAsString)
This will give an error, but you're just neglecting it. Use strconv.ParseFloat(amountAsString, 64) to solve your problem.
Check the playground example to see the error and the working code
After a big battle I was finally able to get Theano to use the GPU in OSX.
But now, I've tried everything I can remember and Theano still can't use CuDNN.
I installed CUDA version 7 and CUDNN version 3.
I tried copying the libraries to /usr/local/cuda/lib and also to /usr/local/cuda/lib64, the include file was copied to /usr/local/cuda/include
My .theanorc is
[global]
floatX = float32
device = gpu
force_device = True
allow_gc = False
optimizer_including=cudnn
# warn_float64 = warn
# openmp = True
[nvcc]
fastmath = True
flags = -L/Users/morgado/anaconda/lib/
[cuda]
root = /usr/local/cuda
[gcc]
cxxflags = -L/usr/local/cuda/lib64
And my .profile has the relevant parts:
# CUDA
CUDA_ROOT=/usr/local/cuda
export PATH=$CUDA_ROOT/bin:$PATH
export DYLD_LIBRARY_PATH=$CUDA_ROOT/lib:$DYLD_LIBRARY_PATH
export LD_LIBRARY_PATH=$CUDA_ROOT/lib:$CUDA_ROOT/lib64:$LD_LIBRARY_PATH
But still when I try to get Theano to use CUDNN the further I get (with the files in lib64) gives me the error:
Using gpu device 0: GeForce GT 750M
1 #include <Python.h>
2 #include <iostream>
3 #include "cudnn.h"
4 //////////////////////
5 //// Support Code
6 //////////////////////
7
8 #if PY_MAJOR_VERSION >= 3
9 #define PyInt_FromLong PyLong_FromLong
10 #endif
11
12
13 namespace {
14 struct __struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715 {
15 PyObject* __ERROR;
16
17 PyObject* storage_V1;
18
19
20 __struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715() {}
21 ~__struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715(void) {
22 cleanup();
23 }
24
25 int init(PyObject* __ERROR, PyObject* storage_V1) {
26 Py_XINCREF(storage_V1);
27 this->storage_V1 = storage_V1;
28
29
30
31 this->__ERROR = __ERROR;
32 return 0;
33 }
34 void cleanup(void) {
35 __label_1:
36
37 double __DUMMY_1;
38 __label_4:
39
40 double __DUMMY_4;
41
42 Py_XDECREF(this->storage_V1);
43 }
44 int run(void) {
45 int __failure = 0;
46
47 PyObject* py_V1;
48
49 PyObject* V1;
50
51 {
52
53 py_V1 = Py_None;
54 {Py_XINCREF(py_V1);}
55
56 V1 = NULL;
57
58 {
59 // Op class DnnVersion
60
61 #if defined(CUDNN_VERSION)
62 V1 = PyTuple_Pack(2, PyInt_FromLong(CUDNN_VERSION), PyInt_FromLong(cudnnGetVersion()));
63 #else
64 V1 = PyInt_FromLong(-1);
65 #endif
66 __label_3:
67
68 double __DUMMY_3;
69
70 }
71 __label_2:
72
73 if (!__failure) {
74
75 assert(py_V1->ob_refcnt > 1);
76 Py_DECREF(py_V1);
77 py_V1 = V1 ? V1 : Py_None;
78 Py_INCREF(py_V1);
79
80 PyObject* old = PyList_GET_ITEM(storage_V1, 0);
81 {Py_XINCREF(py_V1);}
82 PyList_SET_ITEM(storage_V1, 0, py_V1);
83 {Py_XDECREF(old);}
84 }
85
86 Py_XDECREF(V1);
87
88 {Py_XDECREF(py_V1);}
89
90 double __DUMMY_2;
91
92 }
93
94
95 if (__failure) {
96 // When there is a failure, this code puts the exception
97 // in __ERROR.
98 PyObject* err_type = NULL;
99 PyObject* err_msg = NULL;
100 PyObject* err_traceback = NULL;
101 PyErr_Fetch(&err_type, &err_msg, &err_traceback);
102 if (!err_type) {err_type = Py_None;Py_INCREF(Py_None);}
103 if (!err_msg) {err_msg = Py_None; Py_INCREF(Py_None);}
104 if (!err_traceback) {err_traceback = Py_None; Py_INCREF(Py_None);}
105 PyObject* old_err_type = PyList_GET_ITEM(__ERROR, 0);
106 PyObject* old_err_msg = PyList_GET_ITEM(__ERROR, 1);
107 PyObject* old_err_traceback = PyList_GET_ITEM(__ERROR, 2);
108 PyList_SET_ITEM(__ERROR, 0, err_type);
109 PyList_SET_ITEM(__ERROR, 1, err_msg);
110 PyList_SET_ITEM(__ERROR, 2, err_traceback);
111 {Py_XDECREF(old_err_type);}
112 {Py_XDECREF(old_err_msg);}
113 {Py_XDECREF(old_err_traceback);}
114 }
115 // The failure code is returned to index what code block failed.
116 return __failure;
117
118 }
119 };
120 }
121
122
123 static int __struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715_executor(__struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715 *self) {
124 return self->run();
125 }
126
127 static void __struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715_destructor(PyObject *capsule) {
128 __struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715 *self = (__struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715 *)PyCapsule_GetContext(capsule);
129 delete self;
130 }
131
132 //////////////////////
133 //// Functions
134 //////////////////////
135 static PyObject * instantiate(PyObject * self, PyObject *argtuple) {
136 assert(PyTuple_Check(argtuple));
137 if (2 != PyTuple_Size(argtuple)){
138 PyErr_Format(PyExc_TypeError, "Wrong number of arguments, expected 2, got %i", (int)PyTuple_Size(argtuple));
139 return NULL;
140 }
141 __struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715* struct_ptr = new __struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715();
142 if (struct_ptr->init( PyTuple_GET_ITEM(argtuple, 0),PyTuple_GET_ITEM(argtuple, 1) ) != 0) {
143 delete struct_ptr;
144 return NULL;
145 }
146 PyObject* thunk = PyCapsule_New((void*)(&__struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715_executor), NULL, __struct_compiled_op_m086964a2c5561b842ea4c5d4a203a715_destructor);
147 if (thunk != NULL && PyCapsule_SetContext(thunk, struct_ptr) != 0) {
148 PyErr_Clear();
149 Py_DECREF(thunk);
150 thunk = NULL;
151 }
152
153 return thunk; }
154
155 //////////////////////
156 //// Module init
157 //////////////////////
158 static PyMethodDef MyMethods[] = {
159 {"instantiate", instantiate, METH_VARARGS, "undocumented"} ,
160 {NULL, NULL, 0, NULL}
161 };
162 static struct PyModuleDef moduledef = {
163 PyModuleDef_HEAD_INIT,
164 "m086964a2c5561b842ea4c5d4a203a715",
165 NULL,
166 -1,
167 MyMethods,
168 };
169
170 PyMODINIT_FUNC PyInit_m086964a2c5561b842ea4c5d4a203a715(void) {
171 PyObject *m = PyModule_Create(&moduledef);
172 return m;
173 }
174
===============================
clang: warning: argument unused during compilation: '-undefined dynamic_lookup'
clang: warning: argument unused during compilation: '-undefined dynamic_lookup'
clang: warning: argument unused during compilation: '-undefined dynamic_lookup'
clang: warning: argument unused during compilation: '-undefined dynamic_lookup'
clang: warning: argument unused during compilation: '-undefined dynamic_lookup'
clang: warning: argument unused during compilation: '-undefined dynamic_lookup'
clang: warning: argument unused during compilation: '-undefined dynamic_lookup'
ld: library not found for -lcudnn
clang: error: linker command failed with exit code 1 (use -v to see invocation)
['nvcc', '-shared', '-O3', '-L/Users/morgado/anaconda/lib/', '-use_fast_math', '-arch=sm_30', '-m64', '-Xcompiler', '-fno-math-errno,-Wno-unused-label,-Wno-unused-variable,-Wno-write-strings,-DCUDA_NDARRAY_CUH=m11b90075e2397c684f9dc0f7276eab8f,-D NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION,-fPIC', '-Xlinker', '-rpath,/Users/morgado/.theano/compiledir_Darwin-14.4.0-x86_64-i386-64bit-i386-3.4.3-64/cuda_ndarray', '-Xlinker', '-rpath,/usr/local/cuda/lib', '-I/Users/morgado/anaconda/lib/python3.4/site-packages/numpy/core/include', '-I/Users/morgado/anaconda/include/python3.4m', '-I/Users/morgado/anaconda/lib/python3.4/site-packages/theano/sandbox/cuda', '-o', '/Users/morgado/.theano/compiledir_Darwin-14.4.0-x86_64-i386-64bit-i386-3.4.3-64/tmp578d03tw/m086964a2c5561b842ea4c5d4a203a715.so', 'mod.cu', '-lcudnn', '-lcudart', '-Xcompiler', '-undefined,dynamic_lookup', '-Xlinker', '-pie']
ERROR (theano.gof.opt): SeqOptimizer apply <theano.sandbox.cuda.dnn.NoCuDNNRaise object at 0x10ebb14e0>
ERROR (theano.gof.opt): Traceback:
ERROR (theano.gof.opt): Traceback (most recent call last):
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/gof/opt.py", line 196, in apply
sub_prof = optimizer.optimize(fgraph)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/gof/opt.py", line 82, in optimize
ret = self.apply(fgraph, *args, **kwargs)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/sandbox/cuda/dnn.py", line 1682, in apply
if not dnn_available():
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/sandbox/cuda/dnn.py", line 75, in dnn_available
v = version()
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/sandbox/cuda/dnn.py", line 206, in version
theano.Mode(optimizer=None))
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/compile/function.py", line 266, in function
profile=profile)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/compile/pfunc.py", line 511, in pfunc
on_unused_input=on_unused_input)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/compile/function_module.py", line 1466, in orig_function
defaults)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/compile/function_module.py", line 1324, in create
input_storage=input_storage_lists)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/gof/link.py", line 519, in make_thunk
output_storage=output_storage)[:3]
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/gof/vm.py", line 897, in make_all
no_recycling))
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/sandbox/cuda/__init__.py", line 259, in make_thunk
compute_map, no_recycling)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/gof/op.py", line 739, in make_thunk
output_storage=node_output_storage)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/gof/cc.py", line 1073, in make_thunk
keep_lock=keep_lock)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/gof/cc.py", line 1015, in __compile__
keep_lock=keep_lock)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/gof/cc.py", line 1442, in cthunk_factory
key=key, lnk=self, keep_lock=keep_lock)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/gof/cmodule.py", line 1076, in module_from_key
module = lnk.compile_cmodule(location)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/gof/cc.py", line 1354, in compile_cmodule
preargs=preargs)
File "/Users/morgado/anaconda/lib/python3.4/site-packages/theano/sandbox/cuda/nvcc_compiler.py", line 423, in compile_str
'for cmd', ' '.join(cmd))
Exception: ('The following error happened while compiling the node', <theano.sandbox.cuda.dnn.DnnVersion object at 0x11549af98>(), '\n', 'nvcc return status', 1, 'for cmd', 'nvcc -shared -O3 -L/Users/morgado/anaconda/lib/ -use_fast_math -arch=sm_30 -m64 -Xcompiler -fno-math-errno,-Wno-unused-label,-Wno-unused-variable,-Wno-write-strings,-DCUDA_NDARRAY_CUH=m11b90075e2397c684f9dc0f7276eab8f,-D NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION,-fPIC -Xlinker -rpath,/Users/morgado/.theano/compiledir_Darwin-14.4.0-x86_64-i386-64bit-i386-3.4.3-64/cuda_ndarray -Xlinker -rpath,/usr/local/cuda/lib -I/Users/morgado/anaconda/lib/python3.4/site-packages/numpy/core/include -I/Users/morgado/anaconda/include/python3.4m -I/Users/morgado/anaconda/lib/python3.4/site-packages/theano/sandbox/cuda -o /Users/morgado/.theano/compiledir_Darwin-14.4.0-x86_64-i386-64bit-i386-3.4.3-64/tmp578d03tw/m086964a2c5561b842ea4c5d4a203a715.so mod.cu -lcudnn -lcudart -Xcompiler -undefined,dynamic_lookup -Xlinker -pie', '[<theano.sandbox.cuda.dnn.DnnVersion object at 0x11549af98>()]'
Seems like clang is not getting the cudnn library even when I specifically told it to check that path.
In your profile add:
export LIBRARY_PATH=$CUDA_ROOT/lib:$CUDA_ROOT/lib64:$LIBRARY_PATH
Including the lib on LIBRARY_PATH worked for me.
I had the same problem and could fix it by setting
export DYLD_LIBRARY_PATH=/Developer/NVIDIA/CUDA-7.5/lib:$DYLD_LIBRARY_PATH
in my .bashrc and running
sudo update_dyld_shared_cache
afterwards.
One handy feature of Scala is lazy val, where the evaluation of a val is delayed until it's necessary (at first access).
Of course, a lazy val must have some overhead - somewhere Scala must keep track of whether the value has already been evaluated and the evaluation must be synchronized, because multiple threads might try to access the value for the first time at the same time.
What exactly is the cost of a lazy val - is there a hidden boolean flag associated with a lazy val to keep track if it has been evaluated or not, what exactly is synchronized and are there any more costs?
In addition, suppose I do this:
class Something {
lazy val (x, y) = { ... }
}
Is this the same as having two separate lazy vals x and y or do I get the overhead only once, for the pair (x, y)?
This is taken from the scala mailing list and gives implementation details of lazy in terms of Java code (rather than bytecode):
class LazyTest {
lazy val msg = "Lazy"
}
is compiled to something equivalent to the following Java code:
class LazyTest {
public int bitmap$0;
private String msg;
public String msg() {
if ((bitmap$0 & 1) == 0) {
synchronized (this) {
if ((bitmap$0 & 1) == 0) {
synchronized (this) {
msg = "Lazy";
}
}
bitmap$0 = bitmap$0 | 1;
}
}
return msg;
}
}
It looks like the compiler arranges for a class-level bitmap int field to flag multiple lazy fields as initialized (or not) and initializes the target field in a synchronized block if the relevant xor of the bitmap indicates it is necessary.
Using:
class Something {
lazy val foo = getFoo
def getFoo = "foo!"
}
produces sample bytecode:
0 aload_0 [this]
1 getfield blevins.example.Something.bitmap$0 : int [15]
4 iconst_1
5 iand
6 iconst_0
7 if_icmpne 48
10 aload_0 [this]
11 dup
12 astore_1
13 monitorenter
14 aload_0 [this]
15 getfield blevins.example.Something.bitmap$0 : int [15]
18 iconst_1
19 iand
20 iconst_0
21 if_icmpne 42
24 aload_0 [this]
25 aload_0 [this]
26 invokevirtual blevins.example.Something.getFoo() : java.lang.String [18]
29 putfield blevins.example.Something.foo : java.lang.String [20]
32 aload_0 [this]
33 aload_0 [this]
34 getfield blevins.example.Something.bitmap$0 : int [15]
37 iconst_1
38 ior
39 putfield blevins.example.Something.bitmap$0 : int [15]
42 getstatic scala.runtime.BoxedUnit.UNIT : scala.runtime.BoxedUnit [26]
45 pop
46 aload_1
47 monitorexit
48 aload_0 [this]
49 getfield blevins.example.Something.foo : java.lang.String [20]
52 areturn
53 aload_1
54 monitorexit
55 athrow
Values initialed in tuples like lazy val (x,y) = { ... } have nested caching via the same mechanism. The tuple result is lazily evaluated and cached, and an access of either x or y will trigger the tuple evaluation. Extraction of the individual value from the tuple is done independently and lazily (and cached). So the above double-instantiation code generates an x, y, and an x$1 field of type Tuple2.
With Scala 2.10, a lazy value like:
class Example {
lazy val x = "Value";
}
is compiled to byte code that resembles the following Java code:
public class Example {
private String x;
private volatile boolean bitmap$0;
public String x() {
if(this.bitmap$0 == true) {
return this.x;
} else {
return x$lzycompute();
}
}
private String x$lzycompute() {
synchronized(this) {
if(this.bitmap$0 != true) {
this.x = "Value";
this.bitmap$0 = true;
}
return this.x;
}
}
}
Note that the bitmap is represented by a boolean. If you add another field, the compiler will increase the size of the field to being able to represent at least 2 values, i.e. as a byte. This just goes on for huge classes.
But you might wonder why this works? The thread-local caches must be cleared when entering a synchronized block such that the non-volatile x value is flushed into memory. This blog article gives an explanation.
Scala SIP-20 proposes a new implementation of lazy val, which is more correct but ~25% slower than the "current" version.
The proposed implementation looks like:
class LazyCellBase { // in a Java file - we need a public bitmap_0
public static AtomicIntegerFieldUpdater<LazyCellBase> arfu_0 =
AtomicIntegerFieldUpdater.newUpdater(LazyCellBase.class, "bitmap_0");
public volatile int bitmap_0 = 0;
}
final class LazyCell extends LazyCellBase {
import LazyCellBase._
var value_0: Int = _
#tailrec final def value(): Int = (arfu_0.get(this): #switch) match {
case 0 =>
if (arfu_0.compareAndSet(this, 0, 1)) {
val result = 0
value_0 = result
#tailrec def complete(): Unit = (arfu_0.get(this): #switch) match {
case 1 =>
if (!arfu_0.compareAndSet(this, 1, 3)) complete()
case 2 =>
if (arfu_0.compareAndSet(this, 2, 3)) {
synchronized { notifyAll() }
} else complete()
}
complete()
result
} else value()
case 1 =>
arfu_0.compareAndSet(this, 1, 2)
synchronized {
while (arfu_0.get(this) != 3) wait()
}
value_0
case 2 =>
synchronized {
while (arfu_0.get(this) != 3) wait()
}
value_0
case 3 => value_0
}
}
As of June 2013 this SIP hasn't been approved. I expect that it's likely to be approved and included in a future version of Scala based on the mailing list discussion. Consequently, I think you'd be wise to heed Daniel Spiewak's observation:
Lazy val is *not* free (or even cheap). Use it only if you absolutely
need laziness for correctness, not for optimization.
I've written a post with regard to this issue https://dzone.com/articles/cost-laziness
In nutshell, the penalty is so small that in practice you can ignore it.
given the bycode generated by scala for lazy, it can suffer thread safety problem as mentioned in double check locking http://www.javaworld.com/javaworld/jw-05-2001/jw-0525-double.html?page=1