Why doesn't COM use static empty BSTR?

When distributing an empty one BSTR, either SysAllocString(L""), or SysAllocStringLen(str, 0), you always get a new one BSTR(at least according to the test I did). BSTRusually not shared (like Java / .NET overflow) as they are mutable, but the empty string is essentially immutable.

My question is (finally): why does COM not use trivial optimization, always returning the same line when creating an empty one BSTR(and ignoring it in SysFreeString)? Is there a good reason not to do this (because my reasoning is wrong), or simply because it was not considered important enough?

+3
source share
3 answers

I would suggest (and yes, this is just an assumption) that this optimization was not considered important enough to be implemented.

While for many things from past Windows memory, consumption was a major factor in the design of the API (see Raymond Chen's articles), as opposed to interpreting strings in Java or .NET, these advantages are pretty small because they apply to only one string. which is just six bytes. And how many empty lines that the program should store in memory at any given time? Is this number large enough to guarantee this optimization or is it actually negligible?

+2
source

, COM, ++, Java .. , new , ( / ) . , , , IdentityHashMap Java. , / .

COM- NULL BSTR . ( MSXML, , , .: -P)

+4

COM, BSTR , Windows.

BSTR , , / BSTR. . SysReAllocString. , , BSTR .

SysReAllocString @MSDN

, SysReAllocString BSTR, , SysReAllocString BSTR - . . .

, BSTR , . , , . , , , BSTR. BSTR, , , -. - , , , BSTR BSTR . , , "BSTR, , , ", (. BSTR ).

, COM-, NULL- (0-) BSTR , , COM- . , , . , , BSTR , , , NULL. , / .

. , BSTR SysStringLen 0, . , forehand, bstr5 bstr6 .

:

int _tmain(int argc, _TCHAR* argv[])
{
  BSTR bstr1 = SysAllocString(L"");
  BSTR bstr2 = SysAllocStringLen(NULL, 0);
  BSTR bstr3 = SysAllocStringLen(NULL, 1);
  *bstr3 = '\0';
  BSTR bstr4 = SysAllocStringLen(L"some string", 0);
  BSTR bstr5 = SysAllocStringByteLen((LPCSTR)L"", 1);
  BSTR bstr6 = SysAllocStringByteLen((LPCSTR)L"", 2);
  BSTR bstr7 = SysAllocStringByteLen("", 1);
  BSTR bstr8 = SysAllocStringByteLen("\0\0", 2);
  BSTR bstr9 = SysAllocStringByteLen(NULL, 0);
  BSTR bstr10 = SysAllocStringByteLen(NULL, 1);

  _tprintf(_T("L\"\"-sourced BSTR\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr1, SysStringLen(bstr1), SysStringByteLen(bstr1));
  _tprintf(_T("NULL BSTR with no alloc length\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr2, SysStringLen(bstr2), SysStringByteLen(bstr2));
  _tprintf(_T("NULL BSTR with 1 OLECHAR alloc length\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr3, SysStringLen(bstr3), SysStringByteLen(bstr3));
  _tprintf(_T("L\"some string\"-sourced BSTR with no alloc length\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr4, SysStringLen(bstr4), SysStringByteLen(bstr4));
  _tprintf(_T("L\"\"-sourced BSTR with 1 byte alloc length\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr5, SysStringLen(bstr5), SysStringByteLen(bstr5));
  _tprintf(_T("L\"\"-sourced BSTR with 2 byte alloc length\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr6, SysStringLen(bstr6), SysStringByteLen(bstr6));
  _tprintf(_T("\"\"-sourced BSTR with 1 byte alloc length\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr7, SysStringLen(bstr7), SysStringByteLen(bstr7));
  _tprintf(_T("\"\\0\\0\"-sourced BSTR with 2 byte alloc length\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr8, SysStringLen(bstr8), SysStringByteLen(bstr8));
  _tprintf(_T("NULL-sourced BSTR with 0 byte alloc length\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr9, SysStringLen(bstr9), SysStringByteLen(bstr9));
  _tprintf(_T("NULL-sourced BSTR with 1 byte alloc length\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr10, SysStringLen(bstr10), SysStringByteLen(bstr10));

  SysFreeString(bstr1);
  SysFreeString(bstr2);
  SysFreeString(bstr3);
  SysFreeString(bstr4);
  SysFreeString(bstr5);
  SysFreeString(bstr6);
  SysFreeString(bstr7);
  SysFreeString(bstr8);
  SysFreeString(bstr9);
  SysFreeString(bstr10);

  return 0;
}

, .

L""-sourced BSTR
        BSTR=0x00175bdc, length 0, 0 bytes
NULL BSTR with no alloc length
        BSTR=0x00175c04, length 0, 0 bytes
NULL BSTR with 1 OLECHAR alloc length
        BSTR=0x00175c2c, length 1, 2 bytes
L"some string"-sourced BSTR with no alloc length
        BSTR=0x00175c54, length 0, 0 bytes
L""-sourced BSTR with 1 byte alloc length
        BSTR=0x00175c7c, length 0, 1 bytes
L""-sourced BSTR with 2 byte alloc length
        BSTR=0x00175ca4, length 1, 2 bytes
""-sourced BSTR with 1 byte alloc length
        BSTR=0x00175ccc, length 0, 1 bytes
"\0\0"-sourced BSTR with 2 byte alloc length
        BSTR=0x00175cf4, length 1, 2 bytes
NULL-sourced BSTR with 0 byte alloc length
        BSTR=0x00175d1c, length 0, 0 bytes
NULL-sourced BSTR with 1 byte alloc length
        BSTR=0x00175d44, length 0, 1 bytes

, BSTR. , , . BSTR. , , , , BSTR .

int _tmain(int argc, _TCHAR* argv[])
{
  BSTR bstr1 = SysAllocString(L"hello world!");

  _tprintf(_T("L\"hello world!\"-sourced BSTR\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr1, SysStringLen(bstr1), SysStringByteLen(bstr1));

  _tprintf(_T("resizing bstr1 to source L\"\"\r\n"));
  SysReAllocString(&bstr1, L"");
  _tprintf(_T("L\"\"-sourced reallocated BSTR\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr1, SysStringLen(bstr1), SysStringByteLen(bstr1));

  _tprintf(_T("resizing bstr1 to source L\"hello!\"\r\n"));
  SysReAllocString(&bstr1, L"hello!");
  _tprintf(_T("L\"\"-sourced reallocated BSTR\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr1, SysStringLen(bstr1), SysStringByteLen(bstr1));

  _tprintf(_T("resizing bstr1 to source L\"hello world!+\"\r\n"));
  SysReAllocString(&bstr1, L"hello world!+");
  _tprintf(_T("L\"\"-sourced reallocated BSTR\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"),
    bstr1, SysStringLen(bstr1), SysStringByteLen(bstr1));

  SysFreeString(bstr1);

  return 0;
}

(Windows XP) . , - BSTR .

L"hello world!"-sourced BSTR
        BSTR=0x00175bdc, length 12, 24 bytes
resizing bstr1 to source L""
L""-sourced reallocated BSTR
        BSTR=0x00175bdc, length 0, 0 bytes
resizing bstr1 to source L"hello!"
L"hello!"-sourced reallocated BSTR
        BSTR=0x00175bdc, length 6, 12 bytes
resizing bstr1 to source L"hello world!+"
L"hello world!+"-sourced reallocated BSTR
        BSTR=0x00175bdc, length 13, 26 bytes

, , (L ""). , BSTR , . , , BSTR. , , .

:

int _tmain(int argc, _TCHAR* argv[])
{
  BSTR bstr1 = SysAllocString(L"");

  _tprintf(_T("L\"\"-sourced BSTR\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr1, SysStringLen(bstr1), SysStringByteLen(bstr1));

  _tprintf(_T("resizing bstr1 to source L\"hello world!\"\r\n"));
  SysReAllocString(&bstr1, L"hello world!");
  _tprintf(_T("L\"hello world!\"-sourced reallocated BSTR\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr1, SysStringLen(bstr1), SysStringByteLen(bstr1));

  _tprintf(_T("resizing bstr1 to source L\"hello!\"\r\n"));
  SysReAllocString(&bstr1, L"hello!");
  _tprintf(_T("L\"hello!\"-sourced reallocated BSTR\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"), 
    bstr1, SysStringLen(bstr1), SysStringByteLen(bstr1));

  _tprintf(_T("resizing bstr1 to source L\"hello world!+\"\r\n"));
  SysReAllocString(&bstr1, L"hello world!+");
  _tprintf(_T("L\"hello world!+\"-sourced reallocated BSTR\r\n")
    _T("\tBSTR=0x%8.8x, length %d, %d bytes\r\n"),
    bstr1, SysStringLen(bstr1), SysStringByteLen(bstr1));

  SysFreeString(bstr1);

  return 0;
}

:

L""-sourced BSTR
        BSTR=0x00175bdc, length 0, 0 bytes
resizing bstr1 to source L"hello world!"
L"hello world!"-sourced reallocated BSTR
        BSTR=0x00175bdc, length 12, 24 bytes
resizing bstr1 to source L"hello!"
L"hello!"-sourced reallocated BSTR
        BSTR=0x00175bdc, length 6, 12 bytes
resizing bstr1 to source L"hello world!+"
L"hello world!+"-sourced reallocated BSTR
        BSTR=0x00175bdc, length 13, 26 bytes
+2

Source: https://habr.com/ru/post/1724324/


All Articles