Skip to content

Commit fa3518b

Browse files
authored
src: remove duplicate buffer info calls (#1354)
The constructor of the Uint8Array has called `napi_get_typed_array_info`, which should be identical to `napi_get_buffer_info`. Remove the duplicated `napi_get_buffer_info` call.
1 parent 16a18c0 commit fa3518b

File tree

2 files changed

+11
-38
lines changed

2 files changed

+11
-38
lines changed

Diff for: napi-inl.h

+11-33
Original file line numberDiff line numberDiff line change
@@ -2635,7 +2635,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env, size_t length) {
26352635
napi_status status =
26362636
napi_create_buffer(env, length * sizeof(T), &data, &value);
26372637
NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
2638-
return Buffer(env, value, length, static_cast<T*>(data));
2638+
return Buffer(env, value);
26392639
}
26402640

26412641
#ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
@@ -2645,7 +2645,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env, T* data, size_t length) {
26452645
napi_status status = napi_create_external_buffer(
26462646
env, length * sizeof(T), data, nullptr, nullptr, &value);
26472647
NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
2648-
return Buffer(env, value, length, data);
2648+
return Buffer(env, value);
26492649
}
26502650

26512651
template <typename T>
@@ -2669,7 +2669,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env,
26692669
delete finalizeData;
26702670
NAPI_THROW_IF_FAILED(env, status, Buffer());
26712671
}
2672-
return Buffer(env, value, length, data);
2672+
return Buffer(env, value);
26732673
}
26742674

26752675
template <typename T>
@@ -2694,7 +2694,7 @@ inline Buffer<T> Buffer<T>::New(napi_env env,
26942694
delete finalizeData;
26952695
NAPI_THROW_IF_FAILED(env, status, Buffer());
26962696
}
2697-
return Buffer(env, value, length, data);
2697+
return Buffer(env, value);
26982698
}
26992699
#endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
27002700

@@ -2711,7 +2711,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env, T* data, size_t length) {
27112711
#ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
27122712
}
27132713
NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
2714-
return Buffer(env, value, length, data);
2714+
return Buffer(env, value);
27152715
#endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
27162716
}
27172717

@@ -2745,7 +2745,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env,
27452745
delete finalizeData;
27462746
NAPI_THROW_IF_FAILED(env, status, Buffer());
27472747
}
2748-
return Buffer(env, value, length, data);
2748+
return Buffer(env, value);
27492749
#endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
27502750
}
27512751

@@ -2781,7 +2781,7 @@ inline Buffer<T> Buffer<T>::NewOrCopy(napi_env env,
27812781
delete finalizeData;
27822782
NAPI_THROW_IF_FAILED(env, status, Buffer());
27832783
}
2784-
return Buffer(env, value, length, data);
2784+
return Buffer(env, value);
27852785
#endif
27862786
}
27872787

@@ -2805,42 +2805,20 @@ inline void Buffer<T>::CheckCast(napi_env env, napi_value value) {
28052805
}
28062806

28072807
template <typename T>
2808-
inline Buffer<T>::Buffer() : Uint8Array(), _length(0), _data(nullptr) {}
2808+
inline Buffer<T>::Buffer() : Uint8Array() {}
28092809

28102810
template <typename T>
28112811
inline Buffer<T>::Buffer(napi_env env, napi_value value)
2812-
: Uint8Array(env, value), _length(0), _data(nullptr) {}
2813-
2814-
template <typename T>
2815-
inline Buffer<T>::Buffer(napi_env env, napi_value value, size_t length, T* data)
2816-
: Uint8Array(env, value), _length(length), _data(data) {}
2812+
: Uint8Array(env, value) {}
28172813

28182814
template <typename T>
28192815
inline size_t Buffer<T>::Length() const {
2820-
EnsureInfo();
2821-
return _length;
2816+
return ByteLength() / sizeof(T);
28222817
}
28232818

28242819
template <typename T>
28252820
inline T* Buffer<T>::Data() const {
2826-
EnsureInfo();
2827-
return _data;
2828-
}
2829-
2830-
template <typename T>
2831-
inline void Buffer<T>::EnsureInfo() const {
2832-
// The Buffer instance may have been constructed from a napi_value whose
2833-
// length/data are not yet known. Fetch and cache these values just once,
2834-
// since they can never change during the lifetime of the Buffer.
2835-
if (_data == nullptr) {
2836-
size_t byteLength;
2837-
void* voidData;
2838-
napi_status status =
2839-
napi_get_buffer_info(_env, _value, &voidData, &byteLength);
2840-
NAPI_THROW_IF_FAILED_VOID(_env, status);
2841-
_length = byteLength / sizeof(T);
2842-
_data = static_cast<T*>(voidData);
2843-
}
2821+
return reinterpret_cast<T*>(const_cast<uint8_t*>(Uint8Array::Data()));
28442822
}
28452823

28462824
////////////////////////////////////////////////////////////////////////////////

Diff for: napi.h

-5
Original file line numberDiff line numberDiff line change
@@ -1535,11 +1535,6 @@ class Buffer : public Uint8Array {
15351535
T* Data() const;
15361536

15371537
private:
1538-
mutable size_t _length;
1539-
mutable T* _data;
1540-
1541-
Buffer(napi_env env, napi_value value, size_t length, T* data);
1542-
void EnsureInfo() const;
15431538
};
15441539

15451540
/// Holds a counted reference to a value; initially a weak reference unless

0 commit comments

Comments
 (0)